leetcode 409. Longest Palindrome

    int longestPalindrome(string s) {
        unordered_map<char, int> m;
        for (char i : s)
            m[i]++;
        
        bool flag = false;
        int ret = 0;
        for (auto i : m) {
            auto res = i.second;
            if (res % 2 == 1) {
                ret = ret + res - 1;
                flag = true;
            }
            else
                ret = ret + res;
        }
        return flag ? ret + 1 : ret;
    }

 

posted on 2018-02-18 14:07  willaty  阅读(94)  评论(0编辑  收藏  举报

导航