难度: Easy
原题连接
内容描述
Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code"
Output: false
Example 2:
Input: "aab"
Output: true
Example 3:
Input: "carerac"
Output: true
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(N)******
数一下只出现奇数次数的字母有多少个,如果少于或者等于1个,都可以组成Palindrome
beats 100%
class Solution:
def canPermutePalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
tmp = collections.Counter(s)
res = 0
for i in tmp.values():
if i & 1 == 1:
res += 1
return res <= 1