Skip to content

Commit d8a6a18

Browse files
committed
add longest-palindrome script.
1 parent 750b1e5 commit d8a6a18

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

409-longest-palindrome.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const longestPalindrome = function(s) {
6+
const hash = {};
7+
let c;
8+
for (let i = 0; i < s.length; i++) {
9+
c = s.charAt(i);
10+
if (hash.hasOwnProperty(c)) {
11+
hash[c] += 1;
12+
} else {
13+
hash[c] = 1;
14+
}
15+
}
16+
let res = 0;
17+
let val;
18+
for (let k in hash) {
19+
if (hash.hasOwnProperty(k)) {
20+
val = hash[k];
21+
res += Math.floor(val / 2) * 2;
22+
if (res % 2 === 0 && val % 2 === 1) {
23+
res += 1;
24+
}
25+
}
26+
}
27+
28+
return res;
29+
};

0 commit comments

Comments
 (0)