Find if string is K-Palindrome or not

PROBLEM :

A string is k pallindrome if it can be transformed into a palindrome on removing at most k characters from it. Your task is to complete the function is_k_pallin which takes two arguments a string str and a number N . Your function should return true if the string is k pallindrome else it should return false.

Input:
The first line of input is an integer T denoting the number of test cases . Then T test cases follow . Each test case  contains a string str and an integer N separated by space.

Output:
The output will be 1 if the string is  k pallindrome else 0 .

Constraints:
1<=T<=100
1<=length of str<=100
1<=N<=20

Example:
Input
2
abcdecba 1
acdcb  1
Output
1
0

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*You are required to complete this function*/

int K_Palindrome(string ,string ,int ,int ) ;
int min(int a,int b) ;

bool is_k_pallin(string s,int k)
{
    string r;
int l,i ;
r=s ;
reverse(r.begin(), r.end());
l=s.length() ;

i=K_Palindrome(s,r,l,l) ;
if(i<=(k*2))
return 1 ;
else
return 0 ;
}

int K_Palindrome(string str1,string str2,int l1,int l2)
{
int mat[l1+1][l2+1] ;
int i,j ;

for(i=0;i<=l1;i++)
mat[0][i]=i ;

for(j=0;j<=l2;j++)
mat[j][0]=j ;

for(i=1;i<=l1;i++)
{
for(j=1;j<=l2;j++)
{
if(str1[i-1]==str2[j-1])
mat[i][j]=mat[i-1][j-1] ;
else
mat[i][j]=1+min(mat[i][j-1],mat[i-1][j]) ;
}
}
return mat[l1][l2] ;
}

int min(int a,int b)
{
return a<b?a:b ;
}

--------------------------------------------------------------------------------
COMPLETE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std ;
#include<string.h>
#include<bits/stdc++.h>

bool is_k_pallin(string ,int ) ;
int K_Palindrome(string ,string ,int ,int ) ;
int min(int a,int b) ;

int main()
{
int k ;
string s ;
cin>>s ;
cin>>k ;

if(is_k_pallin(s,k))
cout<<"Yes " ;
else
cout<<"Nope " ;
return 0 ;
}

bool is_k_pallin(string s,int k)
{
string r;
int l,i ;
r=s ;
reverse(r.begin(), r.end());
l=s.length() ;

i=K_Palindrome(s,r,l,l) ;
if(i<=(k*2))
return true ;
else
return false ;
}

int K_Palindrome(string str1,string str2,int l1,int l2)
{
int mat[l1+1][l2+1] ;
int i,j ;

for(i=0;i<=l1;i++)
mat[0][i]=i ;

for(j=0;j<=l2;j++)
mat[j][0]=j ;

for(i=1;i<=l1;i++)
{
for(j=1;j<=l2;j++)
{
if(str1[i-1]==str2[j-1])
mat[i][j]=mat[i-1][j-1] ;
else
mat[i][j]=1+min(mat[i][j-1],mat[i-1][j]) ;
}
}

for(i=0;i<=l1;i++)
{
for(j=0;j<=l2;j++)
cout<<mat[i][j]<<" " ;
cout<<endl ;
}

return mat[l1][l2] ;
}

int min(int a,int b)
{
return a<b?a:b ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )