409. Longest Palindrome

来源:互联网 发布:淘宝图片多少像素 编辑:程序博客网 时间:2024/04/30 04:12

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:
7

Explanation:
One longest palindrome that can be built is “dccaccd”, whose length is 7.

class Solution {public:    int longestPalindrome(string s) {        map<char,int> maps;        map<char,int>::iterator it;        int count=0;        for(char i:s)        {            maps[i]++;        }        for(it=maps.begin();it!=maps.end();it++)        {            while(it->second>1)            {                count+=2;                it->second-=2;            }        }        int res=0;        for(it=maps.begin();it!=maps.end();it++)        {            if(it->second>0)            {                res++;            }        }       if(res>0) count++;       return count;    }};
0 0
原创粉丝点击