-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinimumCutsForPalindromes.java
86 lines (70 loc) · 2.79 KB
/
MinimumCutsForPalindromes.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Given a string, a partitioning of the string is a palindrome partitioning if every substring of the partition
is a palindrome. Determine the fewest cuts needed for a palindrome partitioning of a given string.
Assumptions
The given string is not null
Examples
“a | babbbab | bab | aba” is a palindrome partitioning of “ababbbabbababa”.
The minimum number of cuts needed is 3.
*/
public class MinimumCutsForPalindromes {
//common method shared by multiple solutions
public int minCuts(String s) {
if (s.length() <= 1) return 0;
return minCuts(s.toCharArray(), s.length());
}
//Improved dp solution
public int minCuts(char[] A, int n) {// TC: O(n), SC: O(n*n)
int[] cuts = new int[n]; // result memorization
boolean[][] pal = new boolean[n][n]; // check whether substring is pal or not
for (int i = 0; i < n; i++) {
cuts[i] = i;
for (int j = 0; j <= i; j++)
if (A[i] == A[j] && (j+1>i-1 || pal[j+1][i-1])) {
pal[j][i] = true;
cuts[i] = j == 0 ? 0 : Math.min(cuts[i], cuts[j-1] + 1);
}
}
return cuts[n - 1];
}
// Naive DP solution, slow
public int naive(char[] A, int n) {
int[] M = new int[n];
for (int i = 0; i < n; i++) {
M[i] = i;
for (int j = 0; j <= i; j++)
if (isPalindrome(A, j, i))
M[i] = j == 0 ? 0 : Math.min(M[i], M[j-1] + 1);
}
return M[n - 1];
}
private boolean isPalindrome(char[] A, int l, int r) {
while (l <= r) if (A[l++] != A[r--]) return false;
return true;
}
//Naive dp solution ends here
//Naive dp solution 2, which uses isPalindrome method above too
public int naive2(char[] A, int n) { // TC: O(n), SC: O(n)
int[] M = new int[n + 1];
M[0] = -1;
for (int i = 1; i <= n; i++) {
M[i] = i-1;
for (int j = 0; j < i; j++)
if (isPalindrome(A, j, i - 1))
M[i] = Math.min(M[i], M[j] + 1);
}
return M[n];
}
// Naive solution 2 ends here
public static void main(String[] args) {
MinimumCutsForPalindromes mcp = new MinimumCutsForPalindromes();
String s1 = "aaaaaabbabb";
System.out.println(mcp.minCuts(s1)); // 1
String s2 = "ababbbabbababa";
System.out.println(mcp.minCuts(s2)); // 3
System.out.println(mcp.naive(s1.toCharArray(), s1.length())); // 1
System.out.println(mcp.naive(s2.toCharArray(), s2.length())); // 3
System.out.println(mcp.naive2(s1.toCharArray(), s1.length())); // 1
System.out.println(mcp.naive2(s2.toCharArray(), s2.length())); // 3
}
}