-
Notifications
You must be signed in to change notification settings - Fork 34
/
MinimumCutsForPalindromicPartition.java
72 lines (57 loc) · 2.86 KB
/
MinimumCutsForPalindromicPartition.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
/*
https://www.techiedelight.com/find-minimum-cuts-needed-palindromic-partition-string/
https://youtu.be/WPr1jDh3bUQ
*/
//Bottom Up Dynamic Programming
//Pre Computation - Bottom Up DP to find if substring is a palindrome or not
public void findAllPlindromes(String s, boolean isPalin[][]){
//We only use the values above the principal diagonal
//isPalin[i][j] stores if s[i..j] is a palindrome or not
//we start from the end because we require the value of (i+1).
for(int i = s.length() - 1 ; i >= 0 ; i--){
for(int j = i ; j < s.length() ; j++){
//single character is always a palindrome
if(i == j) isPalin[i][j] = true;
//if the border characters match --> check the inner string
else if(s.charAt(i) == s.charAt(j)){
//if the length of the substring being considered is 2 --> directly put true
//else check if the inner string [i+1 ... j-1] is a palindrome or not
isPalin[i][j] = (j-i == 1) ? true : isPalin[i+1][j-1];
}
//when the border characters don't match --> not possible to be a palindrome
else isPlain[i][j] = false;
}
}
}
public int minCutsPlainPartition(String s, boolean isPlain[][]){
int n = s.length();
//cuts[i] indicates the minimum cuts required in s[0...i] to form palindrome partition
int cuts[] = new int[n];
for(int i=0 ; i < n ; i++){
//check if the current substring is partition --> if it is a palindrome we need 0 cuts
if(isPlain[0][i]) cuts[i] = 0;
else{
int minCuts = Integer.MAX_VALUE;
//we partition the substring s[0...i] at j into two substrings --> s[0...j] and s[j+1...i]
//since we need to make palindromic partitions, we check if the 2nd substring s[j+1...i] is a palindrome or not
//we are basically CHOOSING A PARTITION(j) WHICH KEEPS THE 2nd SUBSTRING AS IT IS(bacuse it is a palindrome) AND
//DECOMPOSES THE 1ST SUBSTRING INTO SUBPROBLEMS TO FIND MINIMUM CUTS
for(int j=0 ; j < i ; j++){ //j creates the partition
//1. Check if 2nd substring is a palindrome
//2. solve the subproblem --> we are using the bottom up appraoch, subproblem is already solved
// subproblem is s[0...j] --> we solve it by considering cuts[j]
//3. Consider all valid partitions and chose the cheapest
if(isPalin[j+1][i] && minCuts > cuts[j]+1){
minCuts = cuts[j] + 1;
}
}
cuts[i] = minCuts;
}
}
//result for minimum cuts of s[0...n-1]
return cuts[n-1];
}
/*
Time Complexity - O(n^2)
Space Complexity - O(n^2) [n^2 to store isPlain and n to store cuts --> isPlain is more]
*/