问题描述
给定一个包含大写字母和小写字母的字符串 s ,返回通过这些字母构造成的最长的回文串 。
在构造过程中,请注意区分大小写 。比如 "Aa" 不能当做一个回文字符串。
样例输入
s = "abccccdd"s = "a"s = "bb"
样例输出
7
1
2
核心代码
class Solution {
public:
int longestPalindrome(string s) {
map<char, int> hashmap;
int ans = 0;
bool odd = true;
for (char c: s){
hashmap[c]++;
}
typedef map<char, int>::iterator iter;
iter it = hashmap.begin();
while (it != hashmap.end()){
if (it->second % 2 == 0){
ans += it->second;
}
else {
ans += it->second - 1;
if (odd){
ans += 1;
odd = false;
}
}
it++;
}
return ans;
}
};