原题链接在这里:https://leetcode.com/problems/palindrome-permutation/

题目:

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

For example,
"code" -> False, "aab" -> True, "carerac" -> True.

题解:

看能否配对出现.

Time Complexity: O(n). Space: O(n).

AC Java:

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

可以用bitMap

Time Complexity: O(n). Space: O(256).

 public class Solution {
public boolean canPermutePalindrome(String s) {
if(s == null || s.length() <= 1){
return true;
}
int [] map = new int[256];
for(int i = 0; i<s.length(); i++){
map[s.charAt(i)]++;
}
int count = 0;
for(int i = 0; i<256; i++){
if(count == 0 && map[i]%2 == 1){ //第一次出现frequency为奇数的char
count++;
}else if(map[i] % 2 == 1){ //第二次出现frequency为奇数的char
return false;
}
}
return true;
}
}

类似Longest Palindrome.

跟上Palindrome Permutation II.

LeetCode Palindrome Permutation的更多相关文章

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

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

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

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

  3. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  4. [LeetCode] Palindrome Permutation I & II

    Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...

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

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

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

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

  7. [LeetCode#267] Palindrome Permutation II

    Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

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

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

  9. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. 笔记本做wifi热点

    你可以开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网.点开始 所有程序 命令提示符右键管理员身份运行命令提示符 运行命令:ne ...

  2. 我的Linux对拍脚本

    本文用于Linux下bash的对拍脚本: brute为本目录的暴力程序.. pro为优化过的程序 mak造数据的.. #!/bin/bash while(true)do ./mak printf &q ...

  3. 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点

    Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...

  4. BZOJ2453维护队列&&BZOJ2120数颜色

    2016-05-28 11:20:22 共同的思路: 维护某种颜色上一次在哪里出现pre,可以知道当pre<询问的l时更新答案 块内按照pre排序 修改的时候重新O(n)扫一遍,如果和之前的不一 ...

  5. MongoDB设置访问权限、设置用户

    MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...

  6. 11877 The Coco-Cola Store

    题目:    11877  The Coco-Cola Store Once upon a time, there is a special coco-cola store. If you retur ...

  7. 如何用bat批处理编译swf项目

    平时用FB等IDE编译多模块的游戏项目时,除了添加移除模块的操作很繁琐外,编译速度也非常之慢.而用bat来编译swf项目,速度非常快,稳定. 在此分享自己工作用的bat,每次运行会重新编译主模块Gam ...

  8. 1063. Set Similarity (25)

    1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  9. Qweb Pdf 中添加 图片

    具体方法如下: <img t-if="company.logo" t-att-src="'data:image/png;base64,%s' % company.l ...

  10. 手动创建oem

    [oracle@std bin]$ /u02/app/product//db_1/bin/emca -config dbcontrol db -repos create STARTED EMCA at ...