Print all palindromic partitions of a string
Given a string s, partition s such that every string of the partition is a palindrome. Return all
possible palindrome partitioning of s.
Example :
Input : s = "bcc"
Output : [ [b c c] [cc] ]
Input : s = "geeks"
Output : [ [g e e k s] [ee] ]
PROGRAM
public class Sk1 {
public void subpal(String str){
int count=0; //count is for the times the loop has to be executed
int y=0; //variable 'y' is used for accessing the matrix diagonalwise
int len=str.length();
int[][] mat = new int[len][len];
while(count<str.length())
{
for(int i=0,j=y;i<len;i++,j++)
{
if(i==j)
mat[i][j]=1;
else if(str.charAt(i)==str.charAt(
{
if(i+1==j)
mat[i][j]=1;
else
{
if(mat[i+1][j-1]==1)
mat[i][j]=1;
else
mat[i][j]=0;
}
}
else
mat[i][j]=0;
}
len--;
count++;
y++;
}
for(int e=0;e<str.length();e++)
{
for(int f=0;f<str.length();f++)
{
System.out.print(mat[e][f]+" ");
}
System.out.println();
}
int c=0;
for(int k=0;k<str.length();k++){
for(int l=0;l<str.length();l++){
if(mat[k][l]==1)
c++;
}
}
System.out.println(c);
count=0;y=0;len=str.length();
while(count<str.length())
{
for(int i=0,j=y;i<len;i++,j++)
{
if(mat[i][j]==1){
for(int k=i;k<=j;k++){
System.out.print(str.charAt(k)
}
System.out.println(" ");
}
}
len--;
count++;
y++;
}
}
public static void main(String[] args) {
String s ="abaab";
Sk1 x = new Sk1();
x.subpal(s);
}
}
Comments
Post a Comment