> 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%的用户


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/409.longest_palindrome.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
