409.最长回文串
链接:409.最长回文串
难度:Easy
标签:贪心、哈希表、字符串
简介:给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 。
题解 1 - cpp
- 编辑时间:2022-03-15
- 内存消耗:6.6MB
- 编程语言:cpp
- 解法介绍:统计字符的奇偶性。
class Solution {
   public:
    int longestPalindrome(string s) {
        unordered_map<char, int> m;
        for (auto &c : s) m[c]++;
        int ans = 0, odd = 0;
        for (auto &item : m) {
            if (item.second & 1) {
                odd = 1;
                item.second -= 1;
            }
            ans += item.second;
        }
        return ans + odd;
    }
};