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.

我的解答:

 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

Code Signal_练习题_palindromeRearranging的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

随机推荐

  1. [ActionScript 3.0] 幻灯片效果实例

    package com.fylibs.components.effects { import com.fylibs.utils.LoaderQueues; import com.tweener.tra ...

  2. [Flex] 组件Tree系列 —— 打开和关闭节点

    mxm: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:打开和关闭节点--> < ...

  3. mxonline实战4,用户登陆页面2和用户注册1

            一. 基于类来定义view.py diango中使用基于类来定义views的功能,其实更加方便,因为这样可继承一些定义好的基类,来减少我们的代码量   1. 使用基于类的方法,来重新定 ...

  4. Hibernate 查询数据库中的数据

    1.Criteria介绍 Criteria与Session绑定,其生命周期跟随着Session结束而结束,使用Criteria时进行查询时,每次都要于执行时期动态建立物件,并加入各种查询条件,随着Se ...

  5. Wi-Fi科普讲稿

    Wi-Fi 从入门到?? 组员:deleted 什么是Wi-Fi Wi-Fi 在中文里又称作"无线热点",是Wi-Fi联盟制造商的商标做为产品的品牌认证,是一个创建于IEEE 80 ...

  6. Typecho V1.1反序列化导致代码执行分析

    0x00  前言     今天在Seebug的公众号看到了Typecho的一个前台getshell分析的文章,然后自己也想来学习一下.保持对行内的关注,了解最新的漏洞很重要. 0x01  什么是反序列 ...

  7. java无符号Byte

    1.无符号byte, 实现了将byte(-128~127) 转换为 (0~255) class UnsignedByte { private short value; private byte raw ...

  8. Mac下使用tree命令

    Mac下没有tree命令,但是可以通过brew进行安装,命令如下: brew install tree 装好后tree的用法和linux下的保持一致.参考:http://www.cnblogs.com ...

  9. LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression 解决办法

    根据用户输入的起始日期,查询以起始日期开始的前20条记录,在ASP.NET MVC的Controller代码中这样写: var Logs = db.Log.Take(20); if (!string. ...

  10. (转)Oracle与DB2在数据库高可用技术上的相同与差异探讨

    原文:http://www.talkwithtrend.com/Article/178339 数据库建设过程中,高可用是每一个企业数据中心数据库建设过程中至关重要的一个关注点,直接关系到业务连续性和稳 ...