-
Notifications
You must be signed in to change notification settings - Fork 93
/
PalindromePartitionMinCut.java
58 lines (51 loc) · 1.4 KB
/
PalindromePartitionMinCut.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
package questions.virendra;
public class PalindromePartitionMinCut {
public static int partition(String s) {
int n = s.length();
boolean palindrome[][] = new boolean[n][n]; //boolean table
//Trivial case: single letter palindromes
for (int i = 0; i < n; i++) {
palindrome[i][i] = true;
}
//Finding palindromes of two characters.
for (int i = 0; i < n-1; i++) {
if (s.charAt(i) == s.charAt(i+1)) {
palindrome[i][i+1] = true;
}
}
//Finding palindromes of length 3 to n
for (int curr_len = 3; curr_len <= n; curr_len++) {
for (int i = 0; i < n-curr_len+1; i++) {
int j = i+curr_len-1;
if (s.charAt(i) == s.charAt(j) //1. The first and last characters should match
&& palindrome[i+1][j-1]) //2. Rest of the substring should be a palindrome
{
palindrome[i][j] = true;
}
}
}
int[] cuts = new int[n];
for(int i=0; i<n; i++)
{
int temp = Integer.MAX_VALUE;
if(palindrome[0][i])
cuts[i] = 0;
else
{
for(int j=0; j<i; j++)
{
if((palindrome[j+1][i]) && temp > cuts[j] + 1)
{
temp = cuts[j] + 1;
}
}
cuts[i] = temp;
}
}
return cuts[n-1];
}
public static void main(String args[])
{
System.out.println(partition("aab"));
}
}