Simple python solution using dict.


  • 1
    C
    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: int
            """
            d = {}
            for c in s: # count for each letter, O(n)
                d[c] = d.get(c, 0) + 1
            # subtract the "number of odd values minus 1" or zero from the string length, O(n)
            return len(s) - max(len(tuple(v for v in d.values() if v % 2)) - 1, 0)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.