Simple C++ Solution


  • 0
    V

    Code:

    class Solution {
    public:
        int longestPalindrome(string s) {
            int ans = 0;
            bool odd = false;
            
            for(int i = 0; i < s.length(); i++)
                map[s[i]]++;
            
            
            for(std::pair<char, int> p : map) {
                ans += (p.second / 2) * 2;
                
                if(!odd) {
                    if(p.second % 2 != 0) {
                        ans++;
                        odd = true;
                    }
                }
            }
            
            return ans;
        }
    private:
        std::unordered_map<char, int> map;
    };
    

Log in to reply
 

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