Leet Code Daily 2024-06-04 - Longest Palindrome
Code
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char, int> cnt_chars;
for (const auto& c : s) {
cnt_chars[c]++;
}
int cnt_pairs = 0;
bool has_odd = false;
for (const auto& p : cnt_chars) {
cnt_pairs += p.second / 2;
has_odd = has_odd || p.second % 2 == 1;
}
return (has_odd ? 1 : 0) + cnt_pairs * 2;
}
};