Longest Palindrome

Google

https://leetcode.com/problems/longest-palindrome/description/

class Solution {
public:
    int longestPalindrome(string s) {
        if(s.empty())
        {
            return 0;
        }

        map<char, int> hash;
        int maxLength = 1;
        int nDividedBy2 = 0;

        for(char c : s)
        {
            hash[c] += 1;
            if(hash[c]%2 == 0) 
            {
                maxLength += 2;
                nDividedBy2 += 1;
            }
            else
            {
                if(hash[c] > 2) {
                    nDividedBy2 -= 1;
                }
            }
        }

        return maxLength - (nDividedBy2==hash.size()?1:0);
    }
};

results matching ""

    No results matching ""