-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestPalindrome.js
More file actions
39 lines (37 loc) · 1.04 KB
/
longestPalindrome.js
File metadata and controls
39 lines (37 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
////////////////////////////////////////////////Longest Palindrome///////////////////////////////////////////
// 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.
// Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
// Example 1:
//
// Input: s = "abccccdd"
// Output: 7
// Explanation:
// One longest palindrome that can be built is "dccaccd", whose length is 7.
// Example 2:
//
// Input: s = "a"
// Output: 1
// Example 3:
//
// Input: s = "bb"
// Output: 2
/**
* @param {string} s
* @return {number}
*/
const longestPalindrome = function(s) {
let count = 0, m = s.split("").sort();
for(let i = 0; i < m.length-1; i++) {
if(m[i] === m[i + 1]) {
count += 2;
m.splice(i,2);
i--;
}
}
if(m.length > 0) count++;
return count;
};
// console.log(longestPalindrome("abccccdd"));
// console.log(longestPalindrome("a"));
// console.log(longestPalindrome("bb"));