File tree 5 files changed +67
-0
lines changed
algorithms/LongestPalindrome
5 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,7 @@ All solutions will be accepted!
108
108
| 110| [ Balanced Binary Tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/description/ ) | [ java/py/js] ( ./algorithms/BalancedBinaryTree ) | Easy|
109
109
| 70| [ Climbing Stairs] ( https://leetcode-cn.com/problems/climbing-stairs/description/ ) | [ java/py/js] ( ./algorithms/ClimbingStairs ) | Easy|
110
110
| 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|
111
112
112
113
# Database
113
114
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Longest Palindrome
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments