---
title: "409. Longest Palindrome"
url: "https://laigary.com/interview/coding/409-longest-palindrome"
type: "note"
section: "coding"
date: "2023-03-19"
updated: "2024-08-02"
tags: ["Palindrome", "Hash Table"]
---

# 409. Longest Palindrome

[409\. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)

```python
from collections import Counter

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        counter = Counter(s)
        
        r = 0
        for c in counter:
            r += counter[c] // 2 * 2
            if r % 2 == 0 and counter[c] % 2 == 1:
                r += 1
        return r
```
