标签:set return open pre als else hide lap 解答
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.
我的解答:
1 def palindromeRearranging(inputString): 2 for s in inputString: 3 if inputString.count(s) % 2 == 0: 4 inputString = inputString.replace(s,‘‘) 5 if len(inputString) == 0 or len(inputString) == 1: 6 return True 7 elif len(inputString) % 2 == 1: 8 return inputString.count(inputString[0]) == len(inputString) 9 else: 10 return False
膜拜大佬:
def palindromeRearranging(inputString): return sum([inputString.count(i)%2 for i in set(inputString)]) <= 1
Code Signal_练习题_palindromeRearranging
标签:set return open pre als else hide lap 解答
原文地址:https://www.cnblogs.com/YD2018/p/9363225.html