Skip to content

Commit 4606c54

Browse files
authored
Create MinimumCutsForPalindromicPartition.java
1 parent 6a28c53 commit 4606c54

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
https://www.techiedelight.com/find-minimum-cuts-needed-palindromic-partition-string/
3+
https://youtu.be/WPr1jDh3bUQ
4+
*/
5+
6+
//Bottom Up Dynamic Programming
7+
8+
//Pre Computation - Bottom Up DP to find if substring is a palindrome or not
9+
public void findAllPlindromes(String s, boolean isPalin[][]){
10+
//We only use the values above the principal diagonal
11+
//isPalin[i][j] stores if s[i..j] is a palindrome or not
12+
13+
//we start from the end because we require the value of (i+1).
14+
for(int i = s.length() - 1 ; i >= 0 ; i--){
15+
for(int j = i ; j < s.length() ; j++){
16+
17+
//single character is always a palindrome
18+
if(i == j) isPalin[i][j] = true;
19+
20+
//if the border characters match --> check the inner string
21+
else if(s.charAt(i) == s.charAt(j)){
22+
//if the length of the substring being considered is 2 --> directly put true
23+
//else check if the inner string [i+1 ... j-1] is a palindrome or not
24+
isPalin[i][j] = (j-i == 1) ? true : isPalin[i+1][j-1];
25+
}
26+
27+
//when the border characters don't match --> not possible to be a palindrome
28+
else isPlain[i][j] = false;
29+
}
30+
}
31+
32+
}
33+
34+
public int minCutsPlainPartition(String s, boolean isPlain[][]){
35+
int n = s.length();
36+
37+
//cuts[i] indicates the minimum cuts required in s[0...i] to form palindrome partition
38+
int cuts[] = new int[n];
39+
40+
for(int i=0;i<n;i++){
41+
//check if the current substring is partition --> if it is a palindrome we need 0 cuts
42+
if(isPlain[0][i]) cuts[i] = 0;
43+
else{
44+
int minCuts = Integer.MAX_VALUE;
45+
46+
//we partition the substring s[0...i] at j into two substrings --> s[0...j] and s[j+1...i]
47+
//since we need to make palindromic partitions, we check if the 2nd substring s[j+1...i] is a palindrome or not
48+
//we are basically CHOOSING A PARTITION(j) WHICH KEEPS THE 2nd SUBSTRING AS IT IS(bacuse it is a palindrome) AND
49+
//DECOMPOSES THE 1ST SUBSTRING INTO SUBPROBLEMS TO FIND MINIMUM CUTS
50+
for(int j=0;j<i;j++){ //j creates the partition
51+
52+
//1. Check if 2nd substring is a palindrome
53+
//2. solve the subproblem --> we are using the bottom up appraoch, subproblem is already solved
54+
// subproblem is s[0...j] --> we solve it by considering cuts[j]
55+
//3. Consider all valid partitions and chose the cheapest
56+
if(isPalin[j+1][i] && minCuts > cuts[j]+1){
57+
minCuts = cuts[j] + 1;
58+
}
59+
}
60+
cuts[i] = minCuts;
61+
}
62+
}
63+
64+
//result for minimum cuts of s[0...n-1]
65+
return cuts[n-1];
66+
}
67+
68+
/*
69+
Time Complexity - O(n^2)
70+
Space Complexity - O(n^2) [n^2 to store isPlain and n to store cuts --> isPlain is more]
71+
*/
72+

0 commit comments

Comments
 (0)