Leetcode刷题记录——面试题 01.04. 回文排列
程序员文章站
2024-03-04 09:41:41
...
我们用一个字典存储每个字母出现的次数
若字典中有超过一个字母的次数为奇数,则false,否则为true
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
thisdict = {}
for letter in s:
if letter not in thisdict:
thisdict[letter] = 1
else:
thisdict[letter] += 1
mid = 0
for value in thisdict:
if thisdict[value] & 1 == 1:
mid += 1
if mid >1:
return False
else:
return True