Leetcode: Palindrome Permutation

Question

Given a string, determine if a permutation of the string could form a palindrome.

For example,
“code” -> False, “aab” -> True, “carerac” -> True.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?
Show Tags
Show Similar Problems


My Solution

class Solution(object):
    def canPermutePalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """

        if s=='':
            return True

        dict = {}
        for elem in s:
            if elem not in dict:
                dict[elem] = 1
            else:
                dict[elem] += 1

        count = 0
        for value in dict.values():
            if value%2==1:
                count += 1

        return count==1 or count==0

你可能感兴趣的:(leetcode)