409. Longest Palindrome

来源:互联网 发布:两个数据库数据同步 编辑:程序博客网 时间:2024/06/07 20:08

题目:

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:"abccccdd"Output:7Explanation:One longest palindrome that can be built is "dccaccd", whose length is 7.

思路:

观察回文的结构可以发现,回文个数=偶数个回文个数(奇数个减一) + 1(如果存在奇数个回文)

代码:

class Solution {public:    int longestPalindrome(string s) {        map<char,int> mp;        if(!s.size())            return 0;        for(int i=0;i<s.size();i++)        {            if(mp.find(s[i])==mp.end())                mp[s[i]] = 1;            else                mp[s[i]]++;        }        int len=0,flag=0;        map<char,int>::iterator itr = mp.begin();        itr = mp.begin();        while(itr!=mp.end())        {            if(itr->second%2==0)                len+=itr->second;            else if(itr->second%2)            {                len+=itr->second-1;                flag =1;            }            itr++;        }        if(!flag) {            return len;        }         else            return len+1;    }};