-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathPalindromePartitioningII.java
More file actions
41 lines (33 loc) · 981 Bytes
/
PalindromePartitioningII.java
File metadata and controls
41 lines (33 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Solution {
// TC : O(n2)
// SC: O(n2)
public int minCut(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int n = s.length();
boolean[][] dp = buildMatrix(s, n);
int[] totalCuts = new int[n];
for (int j = 0; j < n; j++) {
int cut = j;
for (int i = 0; i <= j; i++) {
if (dp[i][j]) {
cut = Math.min(cut, i == 0 ? 0 : totalCuts[i - 1] + 1);
}
}
totalCuts[j] = cut;
}
return totalCuts[n - 1];
}
boolean[][] buildMatrix(String s, int n) {
boolean[][] dp = new boolean[n][n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
}
}
}
return dp;
}
}