O(n) python solution


  • 0
    Y
    class Solution(object):
    def canPermutePalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        
        # if s contains more than one char which appears odd times, then any permutation of s can't be a palindrome
        
        d = {}
        for char in s:
            d[char] = d.get(char, 0) + 1
        
        oddCount = 0
        for k in d.keys():
            if d[k] % 2 == 1:
                oddCount += 1
            if oddCount > 1:
                break
        
        return oddCount <= 1

Log in to reply
 

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