1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public: int longestPalindrome(string s) { std::unordered_map<char, int> cntMap; for (auto c : s) cntMap[c]++; int len = 0; bool hasOdd = false; for (auto &entry : cntMap) { int cnt = entry.second; if (cnt % 2 == 0) len += cnt; else { len += cnt - 1; hasOdd = true; } } if (hasOdd) len++; return len; } }; |