Fast, Concise and One Pass C++ solution


  • 0
    class Solution {
    public:
        int longestPalindrome(string s) {
            vector<int> vec(52, 0);
            int counts = 0;
            for (auto c : s) {
                int index = c < 'a' ? c - 'A' + 26 : c - 'a';
                if (++vec[index] & 1) counts++;
                else counts--; 
            }
            return s.size() - max(0, counts - 1);    
        }
    };
    

Log in to reply
 

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