straight-forward c++ solution


  • 0
    T
    public:
        int longestPalindrome(string s) {
            // get # of odd freq
            // return s.size() - (#-1)
            int freq[128] = {0}, odd = 0;
            for (char ch:s) ++freq[ch];
            for (int i = 0; i < 128; ++i) odd += freq[i]%2 ? 1:0;
            return s.size() - max(0,odd - 1);
        }
    };
    

Log in to reply
 

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