LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/
题目:
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.
Example 1:
Input:"aabb"
Output:["abba", "baab"]
Example 2:
Input:"abc"
Output:[]
题解:
先判断是否palindrome, 若不是返回空的res.
若是,先判断是否有一个奇数位,若有,改char放中间. 然后两边分别加.
Note: check to find single char after updating the map.
Don't forget to minus its count by 1 after appending it to item.
Time Complexity: O(2^n). n = s.length().
Space: O(n). stack space.
AC Java:
class Solution {
public List<String> generatePalindromes(String s) {
List<String> res = new ArrayList<>();
if(s == null || s.length() == 0){
return res;
}
int [] count = new int[256];
int n = s.length();
int po = -1;
for(int i = 0; i<n; i++){
count[s.charAt(i)]++;
}
int oneCount = 0;
for(int i = 0; i<256; i++){
oneCount += count[i]%2;
if(count[i]%2 == 1){
po = i;
}
}
if(oneCount > 1){
return res;
}
String init = "";
if(po != -1){
init += (char)po;
count[po]--;
}
dfs(count, init, n, res);
return res;
}
private void dfs(int [] count, String cur, int n, List<String> res){
if(cur.length() == n){
res.add(cur);
return;
}
for(int i = 0; i<count.length; i++){
if(count[i] > 0){
count[i] -= 2;
dfs(count, (char)i+cur+(char)i, n, res);
count[i] += 2;
}
}
}
}
LeetCode Palindrome Permutation II的更多相关文章
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
随机推荐
- hive 记事本
hive 0.12 load data overwrite 直接覆盖了数据,不进回收站..... 手动load data 不加overwrite
- IOS关于UIViewController之间的切换
IOS关于UIViewController之间的切换 1.NavigationController切换UIViewController的两种方式 方法一右侧进入 1 SecondViewControl ...
- POJ - Ubiquitous Religions
Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰. 你知道在你的大学里有n个学生(0 < n <= 50000) .你无法询问 ...
- 【noiOj】p8207(233)
07:和为给定数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入 共三行:第一行是整数n(0 & ...
- BZOJ3105: [cqoi2013]新Nim游戏 博弈论+线性基
一个原来写的题. 既然最后是nim游戏,且玩家是先手,则希望第二回合结束后是一个异或和不为0的局面,这样才能必胜. 所以思考一下我们要在第一回合留下线性基 然后就是求线性基,因为要取走的最少,所以排一 ...
- BZOJ4503: 两个串
Description 兔子们在玩两个串的游戏.给定两个字符串S和T,兔子们想知道T在S中出现了几次, 分别在哪些位置出现.注意T中可能有“?”字符,这个字符可以匹配任何字符. Input 两行两个字 ...
- Linux常见练习题
1./dev/hda5在Linux中表示什么? A. IDE0接口上从盘 B. IDE0接口上主盘的逻辑分区 C. IDE0接口上主盘的第五个分区 D.IDE0接口上从盘的扩展分区 ...
- 自己签发免费ssl证书
自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书 这里说下Linux 系统怎么通过openssl命令生成 证书. 首先执行如下命令生成一个keyopenssl genrsa ...
- 关闭CentOS不必要的开机启动项
命令行: for i in `chkconfig --list |grep 3:on|awk '{print $1}' |grep -Ev "network|sshd|sysstat|ude ...
- 状态压缩 DP
D - Hie with the Pie Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536 ...