C++ Solution, using map


  • 0
    J
    class Solution {
    public:
        int longestPalindrome(string s) {
            map<char,int> mp;
            bool odd=false; // to examine if there is odd numbers
            int res=0;
            for(ch : s) mp[ch]++;
            for (auto it=mp.begin(); it!=mp.end(); ++it){
                res += it->second/2*2;
                if (it->second%2) odd = true;
            }
            return odd ? res+1 : res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.