[Python] 1 Line Solution


  • 0
    S

    First using set to reduce the circle times, then using .count() to count the appear times. no matter the times is odd or even, the // can calculate how many pairs properly.
    Normal cases 2 * pairs + 1 can get the result, but if the String is full of characters that appear even times, the result should be the length of string itself. In this case we get min of them.

    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        return min(len(s), 2 * sum(s.count(chr) // 2 for chr in set(s)) + 1)
    
    

Log in to reply
 

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