---
# System prepended metadata

title: 409. Longest Palindrome

---

# 409. Longest Palindrome
[source](https://leetcode.com/problems/longest-palindrome/description/)
> Date: 12/29
> Array

### Mimi's solution
time complexity:  $O(n)$
```cpp
class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<char, int> m;
        for (char c: s)
            if (m.find(c) == m.end())
                m[c] = 1;
            else m[c] += 1;
            
        int sum = 0;
        for (auto it: m) {
            sum += it.second / 2 * 2;
            if (sum % 2 == 0 && it.second % 2 == 1)
                sum += 1;            
        }   
        return sum; 
    }
};
```