class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char,int> cnt;
int rst = 0;
bool flag = false;
for(char c : s)
++cnt[c];
for(auto p : cnt){
if(p.second%2 == 0)
rst += p.second;
else{
rst += p.second-1;
flag = true;
}
}
return flag ? rst+1 : rst;
}
};
Leetcode 409. 最长回文串
最新推荐文章于 2025-12-17 23:41:09 发布
本文介绍了一种求解字符串中最长回文子串的高效算法。通过使用unordered_map统计字符出现次数,算法能快速计算出最长回文子串的长度,特别关注了奇数次字符的处理,确保结果中包含最多的一个奇数次字符。
177

被折叠的 条评论
为什么被折叠?



