Simple/Lazy/Intuitive Approach Python O(n)


  • 0
    L
     def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: int
            """
            count=0
            a=Counter(s)
            l=len(s)
            for i in a.items():
                if i[1]%2!=0 and l%2==0:
                    a[i[0]]-=1
                    l-=1
                elif i[1]%2!=0 and l%2==1:
                    count+=1
                    if count==2:
                        a[i[0]]-=1
                        l-=1
                        count=1
            return l
    

Log in to reply
 

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