题目:

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. matlab 画不同图案的柱状图

    function applyhatch(h,patterns,colorlist) %APPLYHATCH Apply hatched patterns to a figure % APPLYHATC ...

  2. 随机的30道四则运算题(简单的c)

    #include <stdio.h>#include <stdlib.h>#include <time.h> int main(void){ int i = 0; ...

  3. pxe+preseed安装配置

    Ubuntu Server 部署手册 pxe+tftp+vsftp+apache2 ========================================================== ...

  4. apache与tomcat负载集群的3种方法

    花了两天时间学习apache与tomcat的集成方法,现在把学习成果记录下来. apache与tomcat负载集群集成方法有3种jk.jk_proxy.http_proxy 本次集成使用的软件版本: ...

  5. SQL Server性能优化(3)使用SQL Server Profiler查询性能瓶颈

    关于SQL Server Profiler的使用,网上已经有很多教程,比如这一篇文章:SQL Server Profiler:使用方法和指标说明.微软官方文档:https://msdn.microso ...

  6. 《IT小小鸟》阅读心得

    我是一个不爱看书的人,认为看那些又臭又长废话连篇的书是在浪费时间,但我不认为在那么多的书中没有好书,在一次的职业生涯规划中老师推荐了这本书,一开始认为不值得一看,但还是拿了起来,读了有不少的感触. 书 ...

  7. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  8. 【BZOJ】【3156】防御准备

    DP/斜率优化 斜率优化的裸题…… sigh……又把$10^6$当成10W了……RE了N发 这题还是很水的 当然逆序也能做……不过还是整个反过来比较顺手 反转后的a[0]=反转前的a[n],以此类推直 ...

  9. 【BZOJ】【2648】SJY摆棋子&【BZOJ】【2716】【Violet 3】天使玩偶

    KD-Tree 传说中的kd树...前去膜拜了一下……写道模板题>_< 写kdtree的一些感想: 插入的时候是像可持久化线段树一样直接在最后开新节点,然后更新它所在的块.. 然而其实也是 ...

  10. 利用Jquery实现http长连接(LongPoll) {转}

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JqueryAjaxLongPo ...