Simple Python solution using Counter


  • 0
    C

    just need to counter whether any letter appears odd or even times

        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: int
            """
            strs = collections.Counter(s)
            ret, single = 0, 0
            for i in strs.values():
                if i%2 == 0:
                    ret += i
                else:
                    ret += i/2 * 2
                    single = 1
            return ret + single
    '''

Log in to reply
 

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