409.Longest Palindrome
示例 1:
输入:
"abccccdd"
输出:
7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char,int> ch;
for(auto c :s)
ch[c]++;
int flag=0;
int res=0;
for(auto &c: ch)
{
if(c.second %2 ==0) res+=c.second;
else {
res+=c.second-1;
flag=1;
}
}
return res+flag;
}
};Last updated