力扣第409题 最长回文串

力扣第409题 最长回文串

class Solution {
    public:
    int longestPalindrome(string s) {
        map<char, int> m_Map;
        for (int i = 0; i < s.size(); i++)
        {
            ++m_Map[s[i]];
        }
        bool state = false;
        int res = 0;
        for (map<char, int>::iterator itor = m_Map.begin(); itor != m_Map.end(); itor++)
        {
            if ((*itor).second & 1)
            {
                state = true;
                res += (*itor).second - 1;
            }
            else
            {
                res += (*itor).second;
            }
        }
        if (state)
            res++;
        return res;
    }
};

posted on 2020-03-22 23:54  woodjay  阅读(91)  评论(0编辑  收藏  举报

导航