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 ...
随机推荐
- SpringMVC核心类DispatcherServlet
配置DispatcherServlet 要使用SpringMVC,必须在web.xml中配置好这个DispatcherServlet类 <!-- spring框架必须定义ContextLoade ...
- 关于ASCII、GB231、GBK、UTF-8/UTF8、ANSI、unicode的学习笔记
继续上次的学习内容,写一些自己学习的笔记吧!总是觉得没有笔记的学习总是不那么踏实,我承认自己是个记忆力很差的人,特别羡慕那些可以把自己学过的东西记得很牢靠的人.哎!可惜我不是,那只能做出来点东西,就算 ...
- 教你怎么把安卓应用软件放到系统根目录system/app下
安卓手机有时候安装的软件多了,用着久了就会出现卡机,死机的现象,流畅度大大的减弱了,实在是影响使用体验.对于这种情况,有的人会经常清理后台程序,可是次数多了,提速的效果也不太明显.那么,到底怎么做才能 ...
- 【 Regular Expression Matching 】cpp
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Netsharp快速入门(之19) 平台常用功能(插件操作)
作者:秋时 暗影 转载须说明出处 6.2 插件操作 6.2.1 停用/启用 1.在平台工具-插件管理,右击对应的插件可以使用启用和停用功能.插件停用后会把所有相关的页签.程序集.服务全部停 ...
- 防止IE7,8进入怪异模式
在页头添加 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- 1566: [NOI2009]管道取珠 - BZOJ
Description Input第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. 第三行 ...
- AngularJs学习笔记--Forms
原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms 控件(input.select.textarea)是用户输入数据的一种方式.Form(表单) ...
- PHP第一课:开发环境配置
最近在学php,大概了解了一下php的语法结构,以及一些php及基础的知识.由此想到了要亲手试一试:以为以前是学java的用的 ide是myeclipse,所以对eclipse软件布局有特别的钟爱. ...
- topcoder 642
A:直接拆开字符串写就好了. 今天的题目比较容易一些: B:题目大意: 求最少的翻转次数,每次翻转i是对应 y%i==0都会翻转. 球所有翻转为off状态的最小次数: 从最小idx开始,依次做就好了, ...