-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathpallindromicPartition.java
More file actions
60 lines (49 loc) · 1.12 KB
/
Copy pathpallindromicPartition.java
File metadata and controls
60 lines (49 loc) · 1.12 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
package DynamicProgramming;
import java.util.*;
public class pallindromicPartition {
public static int pp(String str) {
// code here
int n = str.length();
int[][] dp = new int[n][n];
for (int diagonal = 0; diagonal < n; diagonal++) {
for (int i = 0, j = diagonal; j < n; i++, j++) {
if (i == j) {
dp[i][j] = 0;
} else if (j - i == 1) {
if (str.charAt(i) == str.charAt(j)) {
dp[i][j] = 0;
} else {
dp[i][j] = 1;
}
} else {
if (isPallindrome(str.substring(i, j + 1))) {
dp[i][j] = 0;
} else {
dp[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
dp[i][j] = Math.min(dp[i][j], 1 + dp[i][k] + dp[k + 1][j]);
// System.out.println(dp[i][j]);
}
}
}
}
}
return dp[0][n - 1];
}
static boolean isPallindrome(String s) {
int start = 0;
int end = s.length() - 1;
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(pp("geek"));
}
}