Skip to content

Commit 6b44f9d

Browse files
committed
Longest palindrome.
1 parent 639aac6 commit 6b44f9d

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

Easy/405.Convert a Number to Hexadecimal.playground/Contents.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Darwin
21
/**
32
Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
43

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
3+
4+
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
5+
6+
 
7+
8+
Example 1:
9+
Input: s = "abccccdd"
10+
Output: 7
11+
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
12+
13+
Example 2:
14+
Input: s = "a"
15+
Output: 1
16+
Explanation: The longest palindrome that can be built is "a", whose length is 1.
17+
 
18+
19+
Constraints:
20+
- 1 <= s.length <= 2000
21+
- s consists of lowercase and/or uppercase English letters only.
22+
*/
23+
class Solution {
24+
func longestPalindrome(_ s: String) -> Int {
25+
if s.count == 1 { return 1 }
26+
var map = [Character: Int]()
27+
for c in s {
28+
if var count = map[c] {
29+
count += 1
30+
map[c] = count
31+
} else {
32+
map[c] = 1
33+
}
34+
}
35+
var hasSingle = false
36+
map = map.filter { $1 > 1 }
37+
var count = 0
38+
for e in map {
39+
if e.value % 2 == 0 {
40+
count += e.value
41+
} else {
42+
if !hasSingle {
43+
hasSingle = true
44+
count += e.value
45+
} else {
46+
count += (e.value - 1)
47+
}
48+
}
49+
}
50+
return hasSingle ? count : count + 1 <= s.count ? count + 1 : s.count
51+
}
52+
}
53+
54+
let s = Solution()
55+
let r = s.longestPalindrome("aabbba")
56+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/409.Longest Palindrome.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
79. [Binary Watch](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/401.Binary%20Watch.playground/Contents.swift)
8484
80. [Sum of Left Leaves](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/404.Sum%20of%20Left%20Leaves.playground/Contents.swift)
8585
81. [Convert a Number to Hexadecimal](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/405.Convert%20a%20Number%20to%20Hexadecimal.playground/Contents.swift)
86+
82. [Longest Palindrome](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/409.Longest%20Palindrome.playground/Contents.swift)
8687

8788
#### Medium
8889

0 commit comments

Comments
 (0)