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 ...
随机推荐
- ubuntu server下建立分区表/分区/格式化/自动挂载(转)
link:http://www.thxopen.com/linux/2014/03/30/Linux_parted.html 流程为:新建分区-->格式化分区-->挂载分区 首先弄明白分区 ...
- [转]crontab命令指南
原文链接:http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html 前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划 ...
- 使用Jaxb2进行xml与bean的转义时Date的format设置
参考http://jackyrong.iteye.com/blog/1826699 JAXB转换JAVA OBJECT到XML的时候,对java.util.Date的转换有些要注意的地方 输出的格式为 ...
- Leetcode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [BZOJ2788][Poi2012]Festival
2788: [Poi2012]Festival Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 187 Solved: 91[Submit][Statu ...
- Thymeleaf分页
网上找到的例子回来测试一下 <div class="table-pagination"> <ul class="pagination"> ...
- Centos rsync+inotify 实现数据同步备份
最近公司做了一
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...
- 关于Repeater嵌套绑定的问题
前台代码: <div id="firstpane" class="menu_list"> <asp:Repeat ...
- java基础--java静态代码块和静态方法的区别、static用法
转载自: http://blog.sina.com.cn/s/blog_afddb8ff0101aqs9.html 静态代码块:有些代码必须在项目启动的时候就执行,这种代码是主动执行的(当类被载入时, ...