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 ...
随机推荐
- Hadoop学习笔记(1)
Doug Cutting Lucene(索引引擎)---Nutch(搜索Data抓取)---Hadoop 1997:Lucene 2003:GFS 2004:NDFS\MapReduce\Nutch ...
- lua 获取文件名和扩展名
local str = "aaa.bbb.bbb.txt" --获取文件名 function getFileName(str) local idx = str:match(&quo ...
- 记忆用户设置-提升程序的体验VB/C#
有时候,设计的程序有很多的控件,甚至多达近百个,尤其是一些工控软件等,程序运行所需的各种参数都是由用户通过这些控件设置而来,那么记录用户的设置就显得十分必要.如果程序出现异常,起码重新打开可以不用再一 ...
- ffmpeg常用命令
windows http://www.cnblogs.com/wainiwann/p/4128154.html ubuntu http://blog.csdn.net/hellowxwworld/ar ...
- Selenium_用selenium webdriver实现selenium RC中的类似的方法
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...
- Winform 窗体最小化隐藏在桌面右下角:转
ICO文件要放到 bin\Debug 下 1.给主窗体添加 NotifyIcon 控件 2.窗体加载事件里 private void MainF_Load(object sender, EventAr ...
- Html Mailto标签详细使用方法
Html中mailto标签是一个非常实用的贴近用户体验的标签,大多情况下人们都在这样使用 <a href="mailto:example@phplamp.com">ex ...
- 递归merge排序
package sort; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scan ...
- winform对话框控件
(1)ColorDialog 用户自定义颜色控件 点击颜色按键,改变richTextBox1中字体的颜色 private void button1_Click(object sender, E ...
- Linux_linux中profile、bashrc、bash_profile之间的区别和联系(转)
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置. 英文描述为: # /etc/p ...