题目:

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

For example,
"code" -> False, "aab" -> True, "carerac" -> 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?

链接: http://leetcode.com/problems/palindrome-permutation/

题解:

判断一个String是否可以组成一个Palindrome。我们只需要计算单个字符的个数就可以了,0个或者1个都是可以的,超过1个则必不能成为Palindrome。双数的字符我们可以用Set来even out。

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
public boolean canPermutePalindrome(String s) {
if(s == null) {
return false;
}
Set<Character> set = new HashSet<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(set.contains(c)) {
set.remove(c);
} else {
set.add(c);
}
} return set.size() <= 1;
}
}

二刷:

Java:

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null) {
return false;
}
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!set.add(c)) {
set.remove(c);
}
}
return set.size() <= 1;
}
}

使用Bitmap:

public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() == 0) {
return false;
}
int[] bitArr = new int[256];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
bitArr[c]++;
}
boolean foundSingleChar = false;
for (int i = 0; i < 256; i++) {
if (bitArr[i] % 2 != 0) {
if (foundSingleChar) {
return false;
} else {
foundSingleChar = true;
}
}
}
return true;
}
}

简写后的bitmap,因为没有set的remove(),所以速度更快一些,当然这是我们假定字符都属于ascii的前提下。

public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() == 0) {
return false;
}
int[] bitArr = new int[256];
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
bitArr[c]++;
count = bitArr[c] % 2 != 0 ? count + 1 : count - 1;
}
return count <= 1;
}
}

Python:

来自StefanPochmann

class Solution(object):
def canPermutePalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
return sum(v % 2 for v in collections.Counter(s).values()) < 2

三刷:

对于unicode还是用HashSet比较好。 题目可以假定Alphabet只有ASCII所以我们也可以用bitmap。

Java:

public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null) return false;
if (s.length() <= 1) return true;
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (!set.add(s.charAt(i))) set.remove(s.charAt(i));
}
return set.size() <= 1;
}
}

Update:

public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null) return false;
Set<Character> set = new HashSet<>(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!set.add(c)) set.remove(c);
}
return set.size() < 2;
}
}

Reference:

https://leetcode.com/discuss/71076/5-lines-simple-java-solution-with-explanation

https://leetcode.com/discuss/70848/3-line-java-functional-declarative-solution

https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java

266. Palindrome Permutation的更多相关文章

  1. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

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

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

  3. [LeetCode#266] Palindrome Permutation

    Problem: Given a string, determine if a permutation of the string could form a palindrome. For examp ...

  4. 266. Palindrome Permutation 重新排列后是否对称

    [抄题]: Given a string, determine if a permutation of the string could form a palindrome. For example, ...

  5. LeetCode 266. Palindrome Permutation (回文排列)$

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

  6. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...

  7. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

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

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

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

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

随机推荐

  1. xml之XSLT

     1.XSLT是什么  XSLT是XSL的子集,XSL是样式表.XSLT的作用:将XML文档转化成HTML,做的是中间转换者. 而主要需要学习的是XSLT(XSLTransformation).  2 ...

  2. Android Studio 单刷《第一行代码》系列 02 —— 日志工具 LogCat

    前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...

  3. 延迟加载 ERROR org.hibernate.LazyInitializationException:42 - could not initialize proxy - ...

    no Session问题,即延迟加载 延迟加载的问题是指当我们调用完action中的某个方法,在jsp页面要显示我们想要的信息的时候,发现在dao中打开的session已经关闭了. 如下图,第一个箭头 ...

  4. 华为HG8240光猫-破解-联通-2016-telnet-http

    序 我与大家想法基本一致,拿到联通的光猫后,心想它应该是个路由器吧,如果让它自己拨号上网就好了,即省一台路由器,又省电了.抱着这个想法,在2013年里,我搜罗了不少文章,经过Q群,搜索,询问,阅读,理 ...

  5. 好书推荐——《Soft Skill》

    这本书不是一本简单的叙述程序员职业规划和如何提高能力的书. 他论述了如何做一个高产,快乐,幸福的程序员,包括职业生涯,理财,学习,健身,信仰等各个方面的内容. 推荐给每一位伟大的拯救宇宙的程序员! 书 ...

  6. python 解析web接口的json数据

    实例1-使用urllib2 #utf-8 import urllib2 import json url="http://xxx.com" #获取json格式的字符串 page=ur ...

  7. js判断手机还是pc并跳转相关页面

    <script type="text/javascript"> function GetRequest() { var url = location.search; / ...

  8. idea新建项目完整过程

    参看下面博客 http://www.cnblogs.com/cnjava/archive/2013/01/29/2881654.html 突然,感觉idea其实挺麻烦的: 一.junit test做起 ...

  9. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...

  10. html5 webApp常用Meta标签

    Html5 webApp常用Meta标签 <meta charset="UTF-8"> <meta name="viewport" conte ...