Given a string, find out if its characters can be rearranged to form a palindrome.
Example
For inputString = "aabb"
, the output should bepalindromeRearranging(inputString) = true
.
We can rearrange "aabb"
to make "abba"
, which is a palindrome.
我的解答:
def palindromeRearranging(inputString):
for s in inputString:
if inputString.count(s) % 2 == 0:
inputString = inputString.replace(s,'')
if len(inputString) == 0 or len(inputString) == 1:
return True
elif len(inputString) % 2 == 1:
return inputString.count(inputString[0]) == len(inputString)
else:
return False
膜拜大佬:
def palindromeRearranging(inputString):
return sum([inputString.count(i)%2 for i in set(inputString)]) <= 1