字符串可以构造的最长回文子串,按照定义求解即可
C++ 代码
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char,int> h;
for(char c:s)h[c]++;
bool has_odd = false;
int res = 0;
for(auto iter : h) {
int cnt = iter.second;
if(cnt & 1){
has_odd = true;
res += cnt - 1;
} else res += cnt;
}
if(has_odd)res += 1;
return res;
}
};