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 ...
随机推荐
- ostream类重载的operator<<()函数
ostream类重载了operator<<()以识别不同的类型,如: int short long unsigned int unsigned short unsigned long f ...
- 11.3Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频上传的功能,研究相关的代码782 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.783 实现视频浏览的功能 王宇杰 负 ...
- android 开发Parcelable 怎么传值ArrayList
public class TradeEntity implements Parcelable{ public String id; //有关进度条的参数 ArrayList<TradeState ...
- 阿里云无线&前端团队是如何基于webpack实现前端工程化的
背景 前端经历了初期的野蛮生长(切图,写简单的特效)——为了兼容浏览器兼容性而出现的各种类库(JQUERY,YUI等——mv*(饱暖思淫欲,代码多了,也就想到怎样组织代码结构,backbone,ang ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- Vim配置文件(Vimrc)
嘛……后面的比赛基本都是在NOI Linux下进行了,windows下的开发环境基本都不能用了>_>果断转了vim,记录一下vim的配置文件- set nu syntax on filet ...
- XSS的原理分析与解剖(二)
0×01 前言: 上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. ...
- Unity3D 批量图片资源导入设置
原地址:http://blog.csdn.net/asd237241291/article/details/8433548 创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 QQ群:[] 本 ...
- oracle impdp的table_exists_action详解
oracle impdp的table_exists_action详解 分类: [oracle]--[备份与恢复]2012-01-06 22:44 9105人阅读 评论(0) 收藏 举报 tableac ...
- hdu 4850 Wow! Such String!(字符串处理,yy)
题目 参考了博客http://blog.csdn.net/u013368721/article/details/37575165 //用visit[26][26][26][26]来判断新家新区的子母河 ...