Code Signal_练习题_palindromeRearranging

Posted yd2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Code Signal_练习题_palindromeRearranging的主要内容,如果未能解决你的问题,请参考以下文章

Code Signal_练习题_arrayChange

Code Signal_练习题_Minesweeper

Code Signal_练习题_stringsRearrangement

Code Signal_练习题_firstDigit

Code Signal_练习题_digitDegree

Code Signal_练习题_matrixElementsSum