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. 面向对象之ajax

    1.Ajax发送请求的几个步骤 1. 创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest();//IE6 使用var xhr= new ActiveXO ...

  2. axios跨域问题

    最近遇到一个很奇怪的问题,在帮助测试妹子做一个小项目的时候,遇到了一个很棘手的问题,axios请求的时候报404,请求type是options,我当时的第一反应就是跨域问题,果然在console里面还 ...

  3. Xcode 工程文件“.xcodeproj”文件夹解析

    项目.xcodeproj 文件夹底下一般有4个文件: project.pbxproj 文件 xcuserdata 文件夹 xcshareddata 文件夹 project.xcworkspace 文件 ...

  4. 利用CompletableFuture优化程序的执行效率

    一.线程池的Future模式 在了解java8的CompletableFuture之前,先通过Future来解决一个问题,看个例子: 假设现在有一个网站,首页有顶部Banner位.左边栏.右边栏.用户 ...

  5. Array flat的实现

    if (!Array.prototype.flat) { Array.prototype.flat = function (num = 1) { if (!Number(num) || Number( ...

  6. 什么是Java代码的编译与反编译?(转)

    转自:http://java.tedu.cn/ask/203119.html Java代码的编译与反编译 一.什么是编译 1.利用编译程序从源语言编写的源程序产生目标程序的过程. 2.用编译程序产生目 ...

  7. python跳出多重循环

    # -*- coding=utf-8 -*- """ 如何结束多重循环,在单层循环中,可以用break跳出循环,那两层,三层呢? """ # ...

  8. Window10安装Django,并创建第一个Django项目

    1.在cmd中输入pip install Django==1.11.7,安装的版本为:1.11.7. 2.安装完成后输入: >>> import django >>> ...

  9. ASP.NET站点部署相关

    汤姆大叔的部署指南:http://www.cnblogs.com/TomXu/archive/2011/11/25/2263050.html 中文目录: 部署介绍 --(英文版连接) 部署SQL Se ...

  10. DateTime compare

    DateTime t1 = new DateTime(100); DateTime t2 = new DateTime(20); if (DateTime.Compare(t1, t2) > 0 ...