267. 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.
For example:
Given s = "aabb", return ["abba", "baab"].
Given s = "abc", return [].
Hint:
- If a palindromic permutation exists, we just need to generate the first half of the string.
- To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.
链接: http://leetcode.com/problems/palindrome-permutation-ii/
题解:
求一个字符串能生成的all palindromic permutaitons。这个题目又写得很长。 总的来说我们要考虑以下一些点:
- 给定s是否能生成Palindrome
- 生成的Palindrome的奇偶性,是奇数的话中间的字符是哪一个
- 利用permutation II的原理,计算左半部string
- 根据Palindrome的奇偶性判断是否要加上中间的独立字符
- 根据计算出的左半部string,计算右半部string,合并,加入到结果集中
- 复杂度的计算(留给二刷了)
Time Complexity - O(2n), Space Complexity - O(2n)
public class Solution {
private boolean isOddPalindrome;
private String singleChar;
public List<String> generatePalindromes(String s) {
List<String> res = new ArrayList<>();
String evenPalindromeString = generateEvenPalindromeString(s);
if(evenPalindromeString.length() == 0) {
if(this.isOddPalindrome)
res.add(singleChar);
return res;
}
char[] arr = evenPalindromeString.toCharArray();
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
boolean[] visited = new boolean[arr.length];
generatePalindromes(res, sb, arr, visited);
return res;
}
private void generatePalindromes(List<String> res, StringBuilder sb, char[] arr, boolean[] visited) {
if(sb.length() == arr.length) { // we only get permutation of left part of the result palindrome
String s = sb.toString();
res.add(this.isOddPalindrome ? (s + this.singleChar + reverse(s)) : (s + reverse(s)) );
return;
}
for(int i = 0; i < arr.length; i++) {
if(visited[i] || (i > 0 && arr[i] == arr[i - 1] && !visited[i - 1])) { //skip duplicate
continue;
}
if(!visited[i]) {
visited[i] = true;
sb.append(arr[i]);
generatePalindromes(res, sb, arr, visited);
sb.setLength(sb.length() - 1);
visited[i] = false;
}
}
}
private String reverse(String s) { //reverse string
char[] arr = s.toCharArray();
int lo = 0, hi = s.length() - 1;
while(lo < hi) {
char c = arr[lo];
arr[lo++] = arr[hi];
arr[hi--] = c;
}
return String.valueOf(arr);
}
private String generateEvenPalindromeString(String s) { // get even chars of palindrome
Set<Character> set = new HashSet<>();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(set.contains(c)) {
set.remove(c);
sb.append(c); // append only even counted chars
} else {
set.add(c);
}
}
if(set.size() <= 1) {
if(set.size() == 1) {
for(char chr : set) {
this.singleChar = String.valueOf(chr);
}
this.isOddPalindrome = true;
}
return sb.toString();
} else {
return "";
}
}
}
Reference:
https://leetcode.com/discuss/53626/ac-java-solution-with-explanation
267. Palindrome Permutation II的更多相关文章
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [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 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
随机推荐
- C#中如何利用操作符重载和转换操作符
操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...
- hadoop日志太大
hadoop jobtracker日志太大在jobtracker服务器上的mapred-site.xml中添加以下参数: <property> <name>mapreduce. ...
- CPU大小端判断
两种方式:1.通过指针 2.通过联合体,联合体里面的数据都是按顺序存储的,而且不论联合体里面有多少数据类型,联合体长度是最长的数据类型的长度.不论初始化多少联合体里面的数据,有效的是最 ...
- 浅谈IT
在没有学计算机应用技术之前我对IT的认知度几乎为零,曾经还天真的认为IT就是白领,只要做上IT行业,以后便可高枕无忧.后来阴差阳错学了这个专业.通过一年的学习,虽然学艺不精但多少对IT行业了解的一知半 ...
- java中byte和blob互转
1. btye[]转blob byte[] bs = ... Blob blob = conn.createBlob(); blob.setBytes(1, bs); ps.setBlob(2, bl ...
- 如何在PL/SQL Developer 中设置 在select时 显示所有的数据
在执行select 时, 总是不显示所有的记录, 要点一下, 下面那个按钮才会显示所有的数据. 解决方法: Tools>Preferences>Window Types>SQ ...
- python开发中常用的框架
以下是15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Django 应该是最出名的 ...
- web服务器 应用 服务器
WEB服务器.应用程序服务器.HTTP服务器有何区别?IIS.Apache.Tomcat.Weblogic.WebSphere都各属于哪种服务器,这些问题困惑了很久,今天终于梳理清楚了: Web服务器 ...
- c# 发送消息到Email
/// <summary> /// 发送消息到Email /// </summary> /// <param name=&quo ...
- 国内Jquery CDN
新浪CDN: <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js">< ...