Given a string, determine if a permutation of the string could form a palindrome.

Example 1:

  1. Input: "code"
  2. Output: false

Example 2:

  1. Input: "aab"
  2. Output: true

Example 3:

  1. Input: "carerac"
  2. Output: true

Hint:

  1. Consider the palindromes of odd vs even length. What difference do you notice?
  2. Count the frequency of each character.
  3. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

这道题让我们判断一个字符串的全排列有没有是回文字符串的,那么根据题目中的提示,我们分字符串的个数是奇偶的情况来讨论,如果是偶数的话,由于回文字符串的特性,每个字母出现的次数一定是偶数次,当字符串是奇数长度时,只有一个字母出现的次数是奇数,其余均为偶数,那么利用这个特性我们就可以解题,我们建立每个字母和其出现次数的映射,然后我们遍历 HashMap,统计出现次数为奇数的字母的个数,那么只有两种情况是回文数,第一种是没有出现次数为奇数的字母,再一个就是字符串长度为奇数,且只有一个出现次数为奇数的字母,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. bool canPermutePalindrome(string s) {
  4. unordered_map<char, int> m;
  5. int cnt = ;
  6. for (auto a : s) ++m[a];
  7. for (auto a : m) {
  8. if (a.second % == ) ++cnt;
  9. }
  10. return cnt == || (s.size() % == && cnt == );
  11. }
  12. };

那么我们再来看一种解法,这种方法用到了一个 HashSet,我们遍历字符串,如果某个字母不在 HashSet 中,我们加入这个字母,如果字母已经存在,我们删除该字母,那么最终如果 HashSet 中没有字母或是只有一个字母时,说明是回文串,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. bool canPermutePalindrome(string s) {
  4. unordered_set<char> st;
  5. for (auto a : s) {
  6. if (!st.count(a)) st.insert(a);
  7. else st.erase(a);
  8. }
  9. return st.empty() || st.size() == ;
  10. }
  11. };

再来看一种 bitset 的解法,这种方法也很巧妙,我们建立一个 256 大小的 bitset,每个字母根据其 ASCII 码值的不同都有其对应的位置,然后我们遍历整个字符串,遇到一个字符,就将其对应的位置的二进制数 flip 一下,就是0变1,1变0,那么遍历完成后,所有出现次数为偶数的对应位置还应该为0,而出现次数为奇数的时候,对应位置就为1了,那么我们最后只要统计1的个数,就知道出现次数为奇数的字母的个数了,只要个数小于2就是回文数,参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. bool canPermutePalindrome(string s) {
  4. bitset<> b;
  5. for (auto a : s) {
  6. b.flip(a);
  7. }
  8. return b.count() < ;
  9. }
  10. };

类似题目:

Longest Palindromic Substring

Valid Anagram

Palindrome Permutation II

Longest Palindromic Substring

参考资料:

https://leetcode.com/problems/palindrome-permutation/

https://leetcode.com/problems/palindrome-permutation/discuss/69574/1-4-lines-Python-Ruby-C%2B%2B-C-Java

https://leetcode.com/problems/palindrome-permutation/discuss/69582/Java-solution-wSet-one-pass-without-counters.

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Palindrome Permutation 回文全排列的更多相关文章

  1. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  2. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  9. LeetCode:验证回文串【125】

    LeetCode:验证回文串[125] 题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: ...

随机推荐

  1. The Road To Hadoop(网盘系统的实现)

    因为毕业设计的原因,得从零开始学习hadoop.虽然接触Hadoop也有一段时间了,但是没有一个完整的时间段去学习,在公司实习的同时,只能利用零零碎碎的时间学习,今天完成了第一个版本的基于Hadoop ...

  2. context:component-scan" 的前缀 "context" 未绑定。

    SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...

  3. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  4. [JSP]JSP 简介

    1.1 概述 1.1.1 什么是 JSP? JSP 全称是 Java Server Pages,是一种动态网页开发技术. 它与 PHP.ASP.ASP.NET 等语言类似,运行在服务端的语言. JSP ...

  5. 代码的坏味道(11)——霰弹式修改(Shotgun Surgery)

    坏味道--霰弹式修改(Shotgun Surgery) 霰弹式修改(Shotgun Surgery) 类似于 发散式变化(Divergent Change) ,但实际上完全不同.发散式变化(Diver ...

  6. 【MVVM DEV】DataColumn中的TextBox与ComboBox的并存

    一.前言       在WPF编程中,有时候我们使用DataGrid会需要在一个DataColumn中既有TextBox,也要有ComboBox或者TextBlock等其他数据显示样式. 这个时候我们 ...

  7. Cocoapods无法使用/安装失败/失效解决方法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #666666 } sp ...

  8. .NET Core 2.0版本预计于2017年春季发布

    英文原文: NET Core 2.0 Planned for Spring 2017 微软项目经理 Immo Landwerth 公布了即将推出的 .NET Core 2.0 版本的细节,该版本预计于 ...

  9. js url.slice(star,end) url.lastIndexOf('/') + 1, -4

    var url = '"http://60.195.252.25:15518/20151228/XXSX/作三角形的高.mp4")' document.title = url.sl ...

  10. transformjs玩转星球

    如你所见.这篇就是要讲下使用transformjs制作星球的过程.你也可以无视文章,直接去看源码和在线演示: 源码 | 在线演示 代码100行多一点,直接看也没有什么压力.下面分几步讲解下. 生成球上 ...