> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/409.longest_palindrome.md).

# 409.Longest Palindrome

**409.Longest Palindrome**

难度:Easy

> 给定一个包含大写字母和小写字母的字符串，找到通过这些字母构造成的最长的回文串。

在构造过程中，请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。

注意: 假设字符串的长度不会超过 1010。

```
示例 1:

输入:
"abccccdd"

输出:
7

解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
```

偶数个可以直接加，如果有奇数个字符串，最后只能加一个。

```
class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<char,int> ch;
        for(auto c :s)
            ch[c]++;
        int flag=0;
        int res=0;
        for(auto &c: ch)
        {
            if(c.second %2 ==0) res+=c.second;
            else {
                res+=c.second-1;
                flag=1;
            }
        }
        return res+flag;
    }
};
```

> 执行用时 :4ms, 在所有 C++ 提交中击败了97.53%的用户\
> 内存消耗 :8.9MB, 在所有 C++ 提交中击败了79.27%的用户
