requirements
Given a string, determine whether the string can be rearranged to form a palindrome string.
Example 1:
Input: "code" Output: falseCopy the code
Example 2:
Input: "aab" Output: trueCopy the code
Example 3:
Input: "carerac" Output: trueCopy the code
The core code
class Solution:
def canPermutePalindrome(self, s: str) - >bool:
record = dict(a)for i,char in enumerate(s):
record[char] = record.get(char,0) + 1
odd_cnt = 0
for key,val in record.items():
if val % 2:
odd_cnt += 1
if odd_cnt > 1:
return False
return True
Copy the code
The key to a palindrome is that a single letter can have one in the center or none. All other elements are multiples of 2.