1. 程式人生 > >409.最長迴文串

409.最長迴文串

給定一個包含大寫字母和小寫字母的字串,找到通過這些字母構造成的最長的迴文串。

在構造過程中,請注意區分大小寫。比如 "Aa" 不能當做一個迴文字串。

注意:
假設字串的長度不會超過 1010。

示例 1:

輸入:"abccccdd"
輸出:7
解釋:
我們可以構造的最長的迴文串是"dccaccd", 它的長度是 7。

class Solution {
public:
    int longestPalindrome(string s) {
        map<char,int> maps;
        int result=0;
        bool single=false;
        for(auto e:s)
        {
            maps[e]++;
        }
        for(auto i=maps.begin();i!=maps.end();i++)
        {
            if(i->second%2==1)
            {
                result=result+i->second-1;
                single=true;
            }
            else
                result=result+i->second;   
        }
        return single?result+1:result;
    }
};