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