classSolution{public:intlongestPalindrome(string s){
unordered_map<char,int> hash;for(auto&c : s) hash[c]++;int res =0, center =0;for(auto&x : hash){if(x.second %2==0)
res += x.second;else{
res += x.second -1;
center =1;}}return res + center;}};
思路二:用数组统计每个字符出现的个数,然后统计出现个数中的奇数个数
如果出现个数中没有奇数,则原始字符串就是回文串;
如果出现个数中有奇数,则原始字符串长度去掉所有奇数个数加上中间的单独字符就是回文串长度。
classSolution{public:intlongestPalindrome(string s){
vector<int>vec(256);//vec数组下标是字符,数组中存储每个字符出现的个数for(char&c : s) vec[c]++;int res =0;for(int&x : vec) res +=(x %2);return res ==0? s.length():(s.length()- res +1);}};