Gary Lai
  • Home
  • About
Sign in Subscribe

409. Longest Palindrome

Last updated on  Aug 2, 2024

409. Longest Palindrome

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
Previous 210. Course Schedule II
Next 724. Find Pivot Index
Gary Lai © 2025
  • Sign up
Powered by Ghost