---
# System prepended metadata

title: 0409. Longest Palindrome
tags: [Leetcode, Easy, HashMap, Palindrome]

---

# 0409. Longest Palindrome
###### tags: `Leetcode` `HashMap` `Palindrome` `Easy`
Link: https://leetcode.com/problems/longest-palindrome/
## 思路 $O(N)$ $O(1)$
因为即使是用map存的，map的key的个数有限
所以用array存
## Code
```java=
class Solution {
    public int longestPalindrome(String s) {
        int[] cnt = new int[128];
        for(int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            cnt[c]++;
        }
        int len = 0;
        boolean single = false;
        for(int i=0; i<128; i++){
            len += cnt[i]-(cnt[i]%2);
            if(cnt[i]%2==1) single = true;
        }
        return len+(single?1:0);
    }
}
```