Leetcode 409 - Longest palindrome

Note:

  • Include all letters that appear even times.
  • For letters that appear odd numbers. Find the largest odd num.
  • If there is other odd nums, minus 1 from each odd num then add them to the res.

Question:

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
2
3
4
Input: s = "abccccdd"
Output: 7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Code:

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
/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function(s) {
let map = new Map();
for (const char of s) {
if (!map.has(char)) {
map.set(char, 1);
} else {
map.set(char, map.get(char) + 1);
}
}
let evenNumSum = 0;
let oddNumsCount = 0;
let oddNumsSum = 0;
let longestOdd = 0;
for (const [key, val] of map) {
if (val % 2 === 1) {
longestOdd = Math.max(longestOdd, val);
oddNumsCount++;
oddNumsSum += val;
} else {
evenNumSum += val;
}
}
return evenNumSum + (oddNumsCount > 1 ? (oddNumsSum - oddNumsCount + 1) : longestOdd);
};