题目:

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. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  2. Android -- 经验分享

    目录                                                                                             代码中安装 ...

  3. bzoj 2697 贪心

    就贪心就行了,首先可以看成n个格子,放物品,那么 一个物品假设放3个,放在1,k,n处,那么价值和放在1,n 是一样的,所以一个物品只放两个就行了,价值大的应该尽量放 在两边,那么排序之后模拟就行了 ...

  4. mongo 1067错误

    对mongo进行错误的操作导致mongo服务异常关闭,当重启mongo服务时出现1067错误此时在data目录下产生mongod.lock文件,可以讲此文件删除,然后重启就可以了 Please mak ...

  5. Spring Mvc 返回机制

    转自:http://jianzh5.iteye.com/blog/1909234 Spring Mvc 有如下的几种返回方式: ModelAndView, Model, ModelMap, Map, ...

  6. Mobile Web 调试指南(2):远程调试

    原文:http://blog.jobbole.com/68606/ 原文出处: 阿伦孟的博客(@allenm ) 第一篇中讲解了如何让手机来请求我们开发电脑上的源码,做到了这步后,我们可以改完代码立即 ...

  7. 使用时间戳引入css、js文件

    前言 最近在一家创业公司实习,主要负责新版官网和商家平台管理系统的前端开发和维护,每次测试都要上传文件到ftp服务器端测试,初期由于更新修改比较频繁,每次都是直接上传覆盖css.js.php文件,链接 ...

  8. 三、Android NDK编程预备之Java jni入门创建C/C++共享库

    转自: http://www.eoeandroid.com/thread-264971-1-1.html 应网友回复,答应在两天前要出一篇创建C/C++共享库的,但由于清明节假期,跟朋友出去游玩,丢手 ...

  9. Linux多线程之互斥

    题目 共要卖票20张,由命令行输入窗口数,由线程模拟窗口.每卖掉一张票,屏幕显示由几号窗口所卖,一并显示剩余票数 思路 由于票数 ticket_cnt 是全局变量,因此每当一个线程将其减一(卖出一张票 ...

  10. [转载] poll()函数

    原地址:http://baike.baidu.com/view/2997591.htm   poll()函数:这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数,下面是这个函 ...