|
5 | 5 | import java.util.List;
|
6 | 6 | import java.util.Map;
|
7 | 7 |
|
8 |
| -/** |
9 |
| - * Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. |
10 |
| -
|
11 |
| - For example: |
12 |
| -
|
13 |
| - Given s = "aabb", return ["abba", "baab"]. |
14 |
| -
|
15 |
| - Given s = "abc", return []. |
16 |
| -
|
17 |
| - Hint: |
18 |
| -
|
19 |
| - If a palindromic permutation exists, we just need to generate the first half of the string. |
20 |
| - To generate all distinct permutations of a (half of) string, use a similar approach from: _46 II or Next Permutation. |
21 |
| - */ |
22 | 8 | public class _267 {
|
23 | 9 |
|
24 |
| - public List<String> generatePalindromes(String s) { |
25 |
| - int odd = 0; |
26 |
| - String mid = ""; |
27 |
| - List<String> res = new ArrayList(); |
28 |
| - List<Character> list = new ArrayList(); |
29 |
| - Map<Character, Integer> map = new HashMap(); |
30 |
| - |
31 |
| - // step 1. build character count map and count odds |
32 |
| - for (int i = 0; i < s.length(); i++) { |
33 |
| - char c = s.charAt(i); |
34 |
| - map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1); |
35 |
| - odd += map.get(c) % 2 != 0 ? 1 : -1; |
36 |
| - } |
| 10 | + public static class Solution1 { |
| 11 | + public List<String> generatePalindromes(String s) { |
| 12 | + int odd = 0; |
| 13 | + String mid = ""; |
| 14 | + List<String> res = new ArrayList(); |
| 15 | + List<Character> list = new ArrayList(); |
| 16 | + Map<Character, Integer> map = new HashMap(); |
| 17 | + |
| 18 | + // step 1. build character count map and count odds |
| 19 | + for (int i = 0; i < s.length(); i++) { |
| 20 | + char c = s.charAt(i); |
| 21 | + map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1); |
| 22 | + odd += map.get(c) % 2 != 0 ? 1 : -1; |
| 23 | + } |
37 | 24 |
|
38 |
| - // cannot form any palindromic string |
39 |
| - if (odd > 1) { |
40 |
| - return res; |
41 |
| - } |
| 25 | + // cannot form any palindromic string |
| 26 | + if (odd > 1) { |
| 27 | + return res; |
| 28 | + } |
42 | 29 |
|
43 |
| - // step 2. add half count of each character to list |
44 |
| - for (Map.Entry<Character, Integer> entry : map.entrySet()) { |
45 |
| - char key = entry.getKey(); |
46 |
| - int val = entry.getValue(); |
| 30 | + // step 2. add half count of each character to list |
| 31 | + for (Map.Entry<Character, Integer> entry : map.entrySet()) { |
| 32 | + char key = entry.getKey(); |
| 33 | + int val = entry.getValue(); |
47 | 34 |
|
48 |
| - if (val % 2 != 0) { |
49 |
| - mid += key; |
50 |
| - } |
| 35 | + if (val % 2 != 0) { |
| 36 | + mid += key; |
| 37 | + } |
51 | 38 |
|
52 |
| - for (int i = 0; i < val / 2; i++) { |
53 |
| - list.add(key); |
| 39 | + for (int i = 0; i < val / 2; i++) { |
| 40 | + list.add(key); |
| 41 | + } |
54 | 42 | }
|
55 |
| - } |
56 | 43 |
|
57 |
| - // step 3. generate all the permutations |
58 |
| - getPerm(list, mid, new boolean[list.size()], new StringBuilder(), res); |
| 44 | + // step 3. generate all the permutations |
| 45 | + getPerm(list, mid, new boolean[list.size()], new StringBuilder(), res); |
59 | 46 |
|
60 |
| - return res; |
61 |
| - } |
62 |
| - |
63 |
| - // generate all unique permutation from list |
64 |
| - void getPerm(List<Character> list, String mid, boolean[] used, StringBuilder sb, |
65 |
| - List<String> res) { |
66 |
| - if (sb.length() == list.size()) { |
67 |
| - // form the palindromic string |
68 |
| - res.add(sb.toString() + mid + sb.reverse().toString()); |
69 |
| - sb.reverse(); |
70 |
| - return; |
| 47 | + return res; |
71 | 48 | }
|
72 | 49 |
|
73 |
| - for (int i = 0; i < list.size(); i++) { |
74 |
| - // avoid duplication |
75 |
| - if (i > 0 && list.get(i) == list.get(i - 1) && !used[i - 1]) { |
76 |
| - continue; |
| 50 | + // generate all unique permutation from list |
| 51 | + void getPerm(List<Character> list, String mid, boolean[] used, StringBuilder sb, |
| 52 | + List<String> res) { |
| 53 | + if (sb.length() == list.size()) { |
| 54 | + // form the palindromic string |
| 55 | + res.add(sb.toString() + mid + sb.reverse().toString()); |
| 56 | + sb.reverse(); |
| 57 | + return; |
77 | 58 | }
|
78 | 59 |
|
79 |
| - if (!used[i]) { |
80 |
| - used[i] = true; |
81 |
| - sb.append(list.get(i)); |
82 |
| - // recursion |
83 |
| - getPerm(list, mid, used, sb, res); |
84 |
| - // backtracking |
85 |
| - used[i] = false; |
86 |
| - sb.deleteCharAt(sb.length() - 1); |
| 60 | + for (int i = 0; i < list.size(); i++) { |
| 61 | + // avoid duplication |
| 62 | + if (i > 0 && list.get(i) == list.get(i - 1) && !used[i - 1]) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (!used[i]) { |
| 67 | + used[i] = true; |
| 68 | + sb.append(list.get(i)); |
| 69 | + // recursion |
| 70 | + getPerm(list, mid, used, sb, res); |
| 71 | + // backtracking |
| 72 | + used[i] = false; |
| 73 | + sb.deleteCharAt(sb.length() - 1); |
| 74 | + } |
87 | 75 | }
|
88 | 76 | }
|
89 | 77 | }
|
90 |
| - |
91 | 78 | }
|
0 commit comments