Skip to content

Commit 4542d27

Browse files
refactor 131
1 parent 601acc0 commit 4542d27

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

src/main/java/com/fishercoder/solutions/_131.java

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

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.
710
811
Return all possible palindrome partitioning of s.
912
@@ -13,62 +16,52 @@
1316
[
1417
["aa","b"],
1518
["a","a","b"]
16-
]*/
19+
]
20+
21+
*/
1722
public class _131 {
1823

24+
public static class Solution1 {
1925
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+
}
3136
}
37+
}
3238

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 + ", ");
3842
}
3943
System.out.println();
44+
}
45+
System.out.println();
4046

41-
backtracking(s, 0, dp, new ArrayList(), result);
47+
backtracking(s, 0, dp, new ArrayList(), result);
4248

43-
return result;
49+
return result;
4450
}
4551

4652
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);
7163
}
64+
}
7265
}
73-
66+
}
7467
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._131;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _131Test {
13+
private static _131.Solution1 solution1;
14+
private static List<List<String>> expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _131.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
expected = new ArrayList();
24+
expected.add(Arrays.asList("a", "a", "b"));
25+
expected.add(Arrays.asList("aa", "b"));
26+
assertEquals(expected, solution1.partition("aab"));
27+
}
28+
}

0 commit comments

Comments
 (0)