forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromePartitiion.java
More file actions
115 lines (81 loc) · 2.23 KB
/
PalindromePartitiion.java
File metadata and controls
115 lines (81 loc) · 2.23 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//PALINDROME PARTITIONING
//You are given a string. You need to partition that string such that each substrings after partitioning would be palindromic string. You have to do this work with minimum number of partitioning.
//Input Format:
//First line contains integer t which is number of test case. For each test case, it contains a string S
//Output Format:
//Print the minimum number of partitioning.
//Sample Input:
//1
//ababbbabbababa
//Sample Output:
//3
public class PalindromePartitiion {
public static void main(String[] args) {
String str = "ababbbabbababacdbdccc";
System.out
.println(palindromePartitionTD(str, 0, str.length() - 1, new int[str.length() + 1][str.length() + 1]));
System.out.println(palindromePartitionBU(str));
}
public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static int palindromePartitionTD(String str, int si, int ei, int[][] strg) {
String ss = str.substring(si, ei + 1);
if (isPalindrome(ss)) {
return 0;
}
if (strg[si][ei] != 0) {
return strg[si][ei];
}
int min = Integer.MAX_VALUE;
for (int k = si; k < ei; k++) {
int fc = palindromePartitionTD(str, si, k, strg);
int sc = palindromePartitionTD(str, k + 1, ei, strg);
int ans = fc + sc;
if (ans < min) {
min = ans;
}
}
strg[si][ei] = min + 1;
return min + 1;
}
public static int palindromePartitionBU(String str) {
int n = str.length();
int[][] strg = new int[n][n];
for (int slide = 1; slide <= n - 1; slide++) {
for (int si = 0; si <= n - slide - 1; si++) {
int ei = si + slide;
if (isPalindrome(str.substring(si, ei + 1))) {
strg[si][ei] = 0;
} else {
int min = Integer.MAX_VALUE;
for (int k = si; k < ei; k++) {
int fc = strg[si][k];
int sc = strg[k + 1][ei];
int ans = fc + sc;
if (ans < min) {
min = ans;
}
}
strg[si][ei] = min + 1;
}
}
}
// for (int[] val : strg) {
// for (int val1 : val) {
// System.out.print(val1 + " ");
// }
// System.out.println();
// }
return strg[0][n - 1];
}
}