Leetcode 409 Solution

This article provides solution to leetcode question 409 (longest-palindrome)

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

Solution

class Solution {
public:
    int longestPalindrome(string s) {
        map<char, int> m;

        for (auto it = s.begin(); it != s.end(); it++)
            m[*it]++;

        int total = 0;
        bool middle = false;

        for (auto it = m.begin(); it != m.end(); it++)
        {
            if (it->second % 2 == 1)
                middle = true;

            total += it->second - it->second % 2;
        }

        return middle ? total + 1 : total;
    }
};