266. Palindrome Permutation
题目:
Given a string, determine if a permutation of the string could form a palindrome.
For example,"code"
-> False, "aab"
-> True, "carerac"
-> True.
Hint:
- Consider the palindromes of odd vs even length. What difference do you notice?
- Count the frequency of each character.
- 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的更多相关文章
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- [LeetCode#266] Palindrome Permutation
Problem: Given a string, determine if a permutation of the string could form a palindrome. For examp ...
- 266. Palindrome Permutation 重新排列后是否对称
[抄题]: Given a string, determine if a permutation of the string could form a palindrome. For example, ...
- LeetCode 266. Palindrome Permutation (回文排列)$
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- 【leetcode】266. Palindrome Permutation
原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
随机推荐
- 30道四则运算题目---课堂作业--软件工程c++
问题:设计一程序,给二年级小学生随机产生四则运算题目. 一.设计思考问题: 1.四则运算需要俩个运算数和一个运算符. 2.如何产生随机数? 3.如何实现随机产生四则运算? 4.题目是否符合小学生学习范 ...
- VIMTUTOR《VIM教程》
=============================================================================== = 欢 迎 阅 ...
- mono for andorid 引用外部的dll问题
这几天玩mono for android 心想,咱c#终于可以开发移动应用了,心里那个美啊------------ 先开发个什么呢,想起来前几天看到微博里一个用姓名笔画来算两个人关系的小测试,开发个这 ...
- 最全的dedeCMS标签调用技巧和大全
1. 页面php方法获取字段 $refObj->Fields['id']; 2. 在页面上使用PHP连接数据库查询 {dede:php} $db = new DedeSql(false); $ ...
- win8 获取管理员权限
Win8 下动不动 就弹出要管理员权限什么....... 网上找到很多方法. 什么注册表什么..... 不行.. 以下这个方法可行. 按WIN+R,运行对话框中输入gpedit.msc,开启 ...
- TestDriven.Net
转: http://www.cnblogs.com/AlexLiu/archive/2008/12/01/1345002.html
- D3D11 Debug Layer的bug
在开发D3D应用程序时,我们会使用Debug Layer来调试应用程序,以确保我们的程序在最终发布时没有warnings和errors.不过最近在开发应用程序时遇到了这样的问题,就是我把多个网格模型的 ...
- XHProf的安装和使用(PHP性能测试神器)
XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮.XHProf安装和使用方法将在本章讲解.XHProf是PHP的PECL扩展.没有XDeBug那些耗费资源,更加 ...
- 压测2.0:云压测 + APM = 端到端压测解决方案
从压力测试说起 压力测试是确立系统稳定性的一种测试方法,通常在系统正常运作范围之外进行,以考察其功能极限和隐患.与功能测试不同,压测是以软件响应速度为测试目标的,尤其是针对在较短时间内大量并发用户的访 ...
- NodeJS介绍
1.概述: Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快速的.可扩展的网络应用.Node.j ...