|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 |
| -/**Given a string s, partition s such that every substring of the partition is a palindrome. |
| 6 | +/** |
| 7 | + * 131. Palindrome Partitioning |
| 8 | +
|
| 9 | + Given a string s, partition s such that every substring of the partition is a palindrome. |
7 | 10 |
|
8 | 11 | Return all possible palindrome partitioning of s.
|
9 | 12 |
|
|
13 | 16 | [
|
14 | 17 | ["aa","b"],
|
15 | 18 | ["a","a","b"]
|
16 |
| - ]*/ |
| 19 | + ] |
| 20 | +
|
| 21 | + */ |
17 | 22 | public class _131 {
|
18 | 23 |
|
| 24 | + public static class Solution1 { |
19 | 25 | public List<List<String>> partition(String s) {
|
20 |
| - List<List<String>> result = new ArrayList(); |
21 |
| - int n = s.length(); |
22 |
| - boolean[][] dp = new boolean[n][n]; |
23 |
| - for (int i = 0; i < n; i++) { |
24 |
| - for (int j = 0; j <= i; j++) { |
25 |
| - if (s.charAt(j) == s.charAt(i) && (j + 1 >= i - 1 || dp[j + 1][i - 1])) { |
26 |
| - // j+1 >= i-1 means j and i are adjance to each other or only one char apart from each other |
27 |
| - //dp[j+1][i-1] means its inner substring is a palindrome, so as long as s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome. |
28 |
| - dp[j][i] = true; |
29 |
| - } |
30 |
| - } |
| 26 | + List<List<String>> result = new ArrayList(); |
| 27 | + int n = s.length(); |
| 28 | + boolean[][] dp = new boolean[n][n]; |
| 29 | + for (int i = 0; i < n; i++) { |
| 30 | + for (int j = 0; j <= i; j++) { |
| 31 | + if (s.charAt(j) == s.charAt(i) && (j + 1 >= i - 1 || dp[j + 1][i - 1])) { |
| 32 | + // j+1 >= i-1 means j and i are adjance to each other or only one char apart from each other |
| 33 | + //dp[j+1][i-1] means its inner substring is a palindrome, so as long as s.charAt(j) == s.charAt(i), then dp[j][i] must be a palindrome. |
| 34 | + dp[j][i] = true; |
| 35 | + } |
31 | 36 | }
|
| 37 | + } |
32 | 38 |
|
33 |
| - for (boolean[] list : dp) { |
34 |
| - for (boolean b : list) { |
35 |
| - System.out.print(b + ", "); |
36 |
| - } |
37 |
| - System.out.println(); |
| 39 | + for (boolean[] list : dp) { |
| 40 | + for (boolean b : list) { |
| 41 | + System.out.print(b + ", "); |
38 | 42 | }
|
39 | 43 | System.out.println();
|
| 44 | + } |
| 45 | + System.out.println(); |
40 | 46 |
|
41 |
| - backtracking(s, 0, dp, new ArrayList(), result); |
| 47 | + backtracking(s, 0, dp, new ArrayList(), result); |
42 | 48 |
|
43 |
| - return result; |
| 49 | + return result; |
44 | 50 | }
|
45 | 51 |
|
46 | 52 | void backtracking(String s, int start, boolean[][] dp, List<String> temp,
|
47 |
| - List<List<String>> result) { |
48 |
| - if (start == s.length()) { |
49 |
| - List<String> newTemp = new ArrayList(temp); |
50 |
| - result.add(newTemp); |
51 |
| - } |
52 |
| - for (int i = start; i < s.length(); i++) { |
53 |
| - if (dp[start][i]) { |
54 |
| - temp.add(s.substring(start, i + 1)); |
55 |
| - backtracking(s, i + 1, dp, temp, result); |
56 |
| - temp.remove(temp.size() - 1); |
57 |
| - } |
58 |
| - } |
59 |
| - } |
60 |
| - |
61 |
| - |
62 |
| - public static void main(String... strings) { |
63 |
| - _131 test = new _131(); |
64 |
| - String s = "aab"; |
65 |
| - List<List<String>> result = test.partition(s); |
66 |
| - for (List<String> list : result) { |
67 |
| - for (String str : list) { |
68 |
| - System.out.print(str + ", "); |
69 |
| - } |
70 |
| - System.out.println(); |
| 53 | + List<List<String>> result) { |
| 54 | + if (start == s.length()) { |
| 55 | + List<String> newTemp = new ArrayList(temp); |
| 56 | + result.add(newTemp); |
| 57 | + } |
| 58 | + for (int i = start; i < s.length(); i++) { |
| 59 | + if (dp[start][i]) { |
| 60 | + temp.add(s.substring(start, i + 1)); |
| 61 | + backtracking(s, i + 1, dp, temp, result); |
| 62 | + temp.remove(temp.size() - 1); |
71 | 63 | }
|
| 64 | + } |
72 | 65 | }
|
73 |
| - |
| 66 | + } |
74 | 67 | }
|
0 commit comments