-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpalindrome_partitioning.java
72 lines (64 loc) · 1.71 KB
/
palindrome_partitioning.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//Initial Template for Java
import java.io.*;
import java.util.*;
class palindrome_partitioning{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
String str = in.readLine();
Solution ob = new Solution();
System.out.println(ob.palindromicPartition(str));
}
}
}// } Driver Code Ends
//User function Template for Java
class Solution{
static int[][] dp = new int[501][501];
static int palindromicPartition(String s)
{
for(int [] rows:dp)
Arrays.fill(rows,-1);
return solve(s,0,s.length()-1);
}
public static int solve(String s,int i ,int j)
{
if(i>=j)
return 0;
if(palindrome(s,i,j))
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
dp[i][j] = Integer.MAX_VALUE;
for(int k = i;k<j;k++)
{ int l ,r;
if(dp[i][k]!=-1)
l = dp[i][k];
else
l = solve(s,i,k);
if(dp[k+1][j]!=-1)
r = dp[k+1][j];
else
r = solve(s,k+1,j);
int x = 1+l+r;
dp[i][j] = Math.min(x,dp[i][j]);
}
return dp[i][j];
}
public static boolean palindrome(String s,int i,int j)
{
if(i>j)
return false;
if(i==j)
return true;
while(i<j)
{
if(s.charAt(i)!=s.charAt(j))
return false;
i++;
j--;
}
return true;
}
}