题目:

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. java 图片处理

    /* * 图片处理类 */ package image; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.j ...

  2. 用C++编写一个随机产生多个两位数四则运算式子的简单程序

    一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...

  3. Linux 硬盘分区、分区、删除分区、格式化、挂载、卸载

    Linux 虽然一直都有在玩,但是对硬盘操作确实不是很熟悉今天有空,就整理了下. 1,创建分区 先查看下是否有磁盘没有分区 fdisk -l 其中第一个框和第二个框,是已经分好区的磁盘,第三个硬盘没有 ...

  4. jekyll : 使用github托管你的博客

    使用github托管你的博客 效果: http://wuya1234.github.io/blog/2013/11/09/start-github-blog/ 样式神马的还没整 电脑系统 我使用的是m ...

  5. MySQL自动化安装(双主多从读写分离)

    shell #!/bin/bash # Create by # version 1.0 # // # # check out lockfile whether or not exist IsInput ...

  6. 【数据结构】通用的最小堆(最大堆)D-ary Heap

    听说有一种最小(大)堆,不限于是完全二叉树,而是完全D叉树,名为D-ary Heap(http://en.wikipedia.org/wiki/D-ary_heap).D可以是1,2,3,4,100, ...

  7. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

    题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...

  8. 关于myeclipse代码提示的一些问题

    默认是  .xxx  输入点提示,要写注释 @xxx的时候怎么输入@后面有代码提示呢? Auto activation delay 是代码提示出现的速度  下面一行是出现代码提示的条件 我们在.后面加 ...

  9. bzoj 3170 manhattan距离

    首先将坐标系顺时针旋转45度,得到一个新的坐标系,这个坐标系 对应的坐标的manhattan距离就是原图中的距离,然后快排,利用前缀和 数组O(N)求所有的答案,然后找最小值就行了,总时间O(Nlog ...

  10. 简单shell脚本

      简单shell脚本备忘   #!/bin/sh num= ] do table_num=`printf %03d ${num}` echo album_info_${table_num} #mys ...