题目:

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:

  1. If a palindromic permutation exists, we just need to generate the first half of the string.
  2. 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。这个题目又写得很长。 总的来说我们要考虑以下一些点:

  1. 给定s是否能生成Palindrome
  2. 生成的Palindrome的奇偶性,是奇数的话中间的字符是哪一个
  3. 利用permutation II的原理,计算左半部string
  4. 根据Palindrome的奇偶性判断是否要加上中间的独立字符
  5. 根据计算出的左半部string,计算右半部string,合并,加入到结果集中
  6. 复杂度的计算(留给二刷了)

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的更多相关文章

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

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

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

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

  3. [LeetCode#267] Palindrome Permutation II

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

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

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

  5. LeetCode Palindrome Permutation II

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

  6. Palindrome Permutation II 解答

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

  7. [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II

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

  8. [Locked] Palindrome Permutation I & II

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

  9. [LeetCode] Palindrome Permutation I & II

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

随机推荐

  1. js如何获取select下拉框的value以及文本内容

    select下拉框在项目开发中是经常用到的,特别是在联级菜单方面的应用更为广泛.但是,对于一些初学者来说,如何获取下拉框子节点option的value值和文本内容,还是有一点难度的.其他的就不说了,现 ...

  2. 向Array中添加插入排序

    插入排序思路 从第二个元素开始和它前面的元素进行比较,如果比前面的元素小,那么前面的元素向后移动,否则就将此元素插入到相应的位置. 插入排序实现 Function.prototype.method = ...

  3. Java应用程序实现屏幕的"拍照"

    有时候,在Java应用程序开发中,如:远程监控或远程教学,常常需要对计算机的屏幕进行截取,由于屏幕截取是比较接近操作系统的操作,在Windows操作系统下,该操作几乎成了VC.VB等的专利,事实上,使 ...

  4. 数码管字符产生器GenSym 1.0发布

    本软件可以实现以下功能: 1.支持共阴极和共阳极数码管的字符代码的生成. 2.支持C语言和ASM语言方式产生字符串代码的序列. 3.可定制数码管的最高位和最低位的代码产生次序. 4.支持记忆功能,可以 ...

  5. curl Protocol 'http not supported or disabled in libcurl

    C:\Documents and Settings\ganiks.liu\Desktop\curl-7.37.0-win32\bin>curl -V curl 7.37.0 (i386-pc-w ...

  6. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  7. 【Tsinsen】【A1365】森林旅店

    KD-Tree 啊哈~检验了一下自己KD-Tree的学习情况,还算可以,模板至少是记下来了. 支持插入(所以要带重建),查询最近的P个点的距离. 然而题目并没有说是按怎样的顺序输出这P个点?...(事 ...

  8. 如何混合使用ARC和非ARC

    如果你的项目使用的非ARC模式,则为ARC模式的代码文件加入-fobjc-arc标签.如果你的项目使用的ARC模式,则为非ARC模式的代码文件加入 -fno-objc-arc标签.添加标签的方法: 1 ...

  9. JavaScript之数据类型讲解

    JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String.还有1种复杂数据类型——Object,Object本质上是由一 ...

  10. volatile关键字的使用

    (简要概括:volatile变量有两个作用:一个是告诉编译器不要进行优化:另一个是告诉系统始终从内存中取变量的地址,而不是从缓存中取变量的值) 一.前言 1.编译器优化介绍: 由于内存访问速度远不及C ...