原题网址: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.
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.
方法:深度优先搜索,生成前半段,后半段镜像复制。
public class Solution {
private void find(char[] chs, char center, int left, int right, char[] palindrome, List<String> results) {
if (left == right) {
palindrome[left] = center;
results.add(new String(palindrome));
return;
} else if (left > right) {
results.add(new String(palindrome));
return;
}
boolean[] used = new boolean[256];
for(int i=left; i<chs.length; i++) {
if (used[chs[i]]) continue;
used[chs[i]] = true;
char t = chs[left];
chs[left] = chs[i];
chs[i] = t;
palindrome[left] = chs[left];
palindrome[right] = chs[left];
find(chs, center, left+1, right-1, palindrome, results);
t = chs[left];
chs[left] = chs[i];
chs[i] = t;
}
}
public List<String> generatePalindromes(String s) {
List<String> results = new ArrayList<>();
char[] sa = s.toCharArray();
int[] f = new int[256];
for(int i=0; i<sa.length; i++) f[sa[i]] ++;
char center = ' ';
boolean single = false;
int count = 0;
for(int i=0; i<f.length; i++) {
if ((f[i] & 1) == 1) {
if (single) return results;
single = true;
center = (char)i;
f[i] --;
}
count += f[i];
}
if ((sa.length & 1) == 0 && single) return results;
if ((sa.length & 1) == 1 && !single) return results;
char[] chs = new char[count/2];
int size = 0;
for(int i=0; i<f.length; i++) {
for(int j=0; j<f[i]/2; j++) {
chs[size++] = (char)i;
}
}
find(chs, center, 0, sa.length-1, new char[sa.length], results);
return results;
}
}
used数组每次都要创建比较浪费空间和时间,如果每个深度使用字符从0~255进行循环,则不需要担心重复的问题,就可以通过计数器来维护使用状态。
public class Solution {
private void find(int[] frequency, int[] used, char center, char[] permutation, int step, List<String> results) {
if (step == permutation.length/2) {
if ((permutation.length & 1) == 1) permutation[step++] = center;
for(int i=0, j=permutation.length-1; i<j; i++, j--) permutation[j] = permutation[i];
results.add(new String(permutation));
return;
}
for(char ch=0; ch<frequency.length; ch++) {
if (frequency[ch] == 0 || used[ch] >= (frequency[ch]>>1)) continue;
used[ch] ++;
permutation[step] = ch;
find(frequency, used, center, permutation, step+1, results);
used[ch] --;
}
}
public List<String> generatePalindromes(String s) {
List<String> results = new ArrayList<>();
char[] sa = s.toCharArray();
boolean single = false;
char center = '\0';
int[] frequency = new int[256];
for(int i=0; i<sa.length; i++) frequency[sa[i]] ++;
for(char i=0; i<frequency.length; i++) {
if ((frequency[i] & 1) == 1) {
if (!single) {
single = true;
center = i;
} else {
return results;
}
}
}
find(frequency, new int[256], center, new char[sa.length], 0, results);
return results;
}
}