Code Signal_练习题_palindromeRearranging

Given a string, find out if its characters can be rearranged to form a palindrome.

Example

For inputString = "aabb", the output should be
palindromeRearranging(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
View Code

猜你喜欢

转载自www.cnblogs.com/YD2018/p/9363225.html
今日推荐