-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartition.java
More file actions
51 lines (45 loc) · 1.41 KB
/
PalindromePartition.java
File metadata and controls
51 lines (45 loc) · 1.41 KB
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
import java.util.Arrays;
public class PalindromePartition {
public static boolean isPalindrome(String s, int i, int j) {
while(i<j) {
if(s.charAt(i) != s.charAt(j)) return false;
i++;
j--;
}
return true;
}
public static int ppRec(String s, int i, int j) {
if(isPalindrome(s,i,j-1) || i>=j) return 0;
else{
int min = 505;
for(int k=i+1;k<j;k++) {
//System.out.println(k);
int res = 1 + ppRec(s, i, k) + ppRec(s, k, j);
min = Math.min(res, min);
}
return min;
}
}
static int dp[][];
public static int ppRecDp(String s, int i, int j) {
if(isPalindrome(s,i,j-1) || i>=j) return 0;
if(dp[i][j] != -1) return dp[i][j];
else{
int min = 505;
for(int k=i+1;k<j;k++) {
//System.out.println(k);
int res = 1 + ppRecDp(s, i, k) + ppRecDp(s, k, j);
min = Math.min(res, min);
}
dp[i][j] = min;
return min;
}
}
public static void main(String args[]) {
String s = "ababbbabbababa";
dp = new int[s.length()+1][s.length()+1];
for(int aa[]: dp) Arrays.fill(aa, -1);
System.out.println(ppRec(s, 0, s.length()));
System.out.println(ppRecDp(s,0,s.length()));
}
}