Skip to content

Commit 31b7b51

Browse files
refactor 132
1 parent 4542d27 commit 31b7b51

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.fishercoder.solutions;
22

3-
/**Given a string s, partition s such that every substring of the partition is a palindrome.
3+
/**
4+
* 132. Palindrome Partitioning II
5+
6+
Given a string s, partition s such that every substring of the partition is a palindrome.
47
58
Return the minimum cuts needed for a palindrome partitioning of s.
69
@@ -9,32 +12,35 @@
912
1013
*/
1114
public class _132 {
12-
/**This solution is cooler than Jiuzhang: https://discuss.leetcode.com/topic/32575/easiest-java-dp-solution-97-36*/
13-
14-
//cut[i] stands for the minimum number of cut needed to cut [0, i] into palindromes
15-
//we initiazlie cut[i] with its max possible value which is i, this is because a single char is naturally a palindrome, so, we'll cut this string into all single-char substrings, which is the max cuts needed
16-
17-
//dp[j][i] == true stands for s.substring(j,i) is a palindrome
18-
public int minCut(String s) {
15+
16+
/**This solution is cooler than Jiuzhang: https://discuss.leetcode.com/topic/32575/easiest-java-dp-solution-97-36*/
17+
18+
public static class Solution1 {
19+
//cut[i] stands for the minimum number of cut needed to cut [0, i] into palindromes
20+
//we initiazlie cut[i] with its max possible value which is i, this is because a single char is naturally a palindrome, so, we'll cut this string into all single-char substrings, which is the max cuts needed
21+
22+
//dp[j][i] == true stands for s.substring(j,i) is a palindrome
23+
public int minCut(String s) {
1924
int n = s.length();
2025
char[] c = s.toCharArray();
2126
boolean[][] dp = new boolean[n][n];
2227
int[] cut = new int[n];
2328

2429
for (int i = 0; i < n; i++) {
25-
cut[i] = i;
26-
for (int j = 0; j <= i; j++) {
27-
if (c[i] == c[j] && (j + 1 > i - 1 || dp[j + 1][i - 1])) {
28-
dp[j][i] = true;
29-
if (j == 0) {
30-
cut[i] = 0;
31-
} else {
32-
cut[i] = (cut[i] < cut[j - 1] + 1) ? cut[i] : cut[j - 1] + 1;
33-
}
34-
}
30+
cut[i] = i;
31+
for (int j = 0; j <= i; j++) {
32+
if (c[i] == c[j] && (j + 1 > i - 1 || dp[j + 1][i - 1])) {
33+
dp[j][i] = true;
34+
if (j == 0) {
35+
cut[i] = 0;
36+
} else {
37+
cut[i] = (cut[i] < cut[j - 1] + 1) ? cut[i] : cut[j - 1] + 1;
38+
}
3539
}
40+
}
3641
}
3742

3843
return cut[n - 1];
44+
}
3945
}
4046
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._132;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _132Test {
10+
private static _132.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _132.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.minCut("aab"));
20+
}
21+
}

0 commit comments

Comments
 (0)