Skip to content

Commit c622fc0

Browse files
committed
solve problem Longest Palindrome
1 parent 12e0f41 commit c622fc0

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ All solutions will be accepted!
108108
|110|[Balanced Binary Tree](https://leetcode-cn.com/problems/balanced-binary-tree/description/)|[java/py/js](./algorithms/BalancedBinaryTree)|Easy|
109109
|70|[Climbing Stairs](https://leetcode-cn.com/problems/climbing-stairs/description/)|[java/py/js](./algorithms/ClimbingStairs)|Easy|
110110
|448|[Find All Numbers Disappeared In An Array](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/description/)|[java/py/js](./algorithms/FindAllNumbersDisappearedInAnArray)|Easy|
111+
|409|[Longest Palindrome](https://leetcode-cn.com/problems/longest-palindrome/description/)|[java/py/js](./algorithms/LongestPalindrome)|Easy|
111112

112113
# Database
113114
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Longest Palindrome
2+
This problem is easy to solve
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int longestPalindrome(String s) {
3+
Map<Character, Integer> appearedMap = new HashMap<Character, Integer>();
4+
int count = 0;
5+
6+
for (char c : s.toCharArray()) {
7+
if (appearedMap.get(c) == null) {
8+
appearedMap.put(c, 1);
9+
} else {
10+
count += 2;
11+
appearedMap.remove(c);
12+
}
13+
}
14+
15+
if (s.length() > count) {
16+
count++;
17+
}
18+
19+
return count;
20+
}
21+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var longestPalindrome = function(s) {
6+
let appearedMap = {},
7+
count = 0
8+
9+
s.split('').forEach(c => {
10+
if (appearedMap[c] === undefined) {
11+
appearedMap[c] = 1
12+
} else {
13+
count += 2
14+
delete appearedMap[c]
15+
}
16+
})
17+
18+
if (s.length > count) {
19+
count++
20+
}
21+
22+
return count
23+
};
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def longestPalindrome(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
appeared_map = {}
8+
count = 0
9+
10+
for c in s:
11+
if appeared_map.get(c) == None:
12+
appeared_map[c] = 1
13+
else:
14+
count += 2
15+
del appeared_map[c]
16+
17+
if len(s) > count:
18+
count += 1
19+
20+
return count

0 commit comments

Comments
 (0)