码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_palindromeRearranging

时间:2018-07-24 23:47:05      阅读:246      评论:0      收藏:0      [点我收藏+]

标签: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 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

标签:set   return   open   pre   als   else   hide   lap   解答   

原文地址:https://www.cnblogs.com/YD2018/p/9363225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!