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

# 409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example `"Aa"` is not considered a palindrome here.

**Note:**\
Assume the length of given string will not exceed 1,010.

**Example:**

```
Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
```

#### 解题要点：

记录每个字符出现的频率，再遍历这个数组，把字符出现的频率次数加进最终返回值，如果是偶数，则可以直接回文；如果是奇数，-1以构成回文。最后如整串只要有出现奇数数字，则单独+1到返回值里。

```java
class Solution {
    public int longestPalindrome(String s) {
        int[] count = new int[128];
        for(char c : s.toCharArray()){
            count[c]++;
        }
        int res = 0, odd = 0;
        for(int cc : count){
            res += cc;
            if(cc % 2 == 1){
                res -= 1;
                odd += 1;
            }
        }
        int add = 0;
        if(odd > 0) add = 1;        
        return res + add;
    }
}
```
