0409.cpp (347B)
1 class Solution { 2 public: 3 int longestPalindrome(string s) { 4 unordered_map<char, int> um; 5 for (char c : s) 6 um[c]++; 7 8 int res = 0; 9 bool odd = false; 10 for (auto [c, v] : um) { 11 if (v % 2 && !odd) odd = true; 12 res += v / 2; 13 } 14 return res * 2 + odd; 15 } 16 };