K-Palindrome | Practice | GeeksforGeeks

A string is k palindrome 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_palin which takes two arguments a string str and a number N . Your function should return true if the string is k palindrome 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 palindrome else 0 .

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

Example:
Input
2
abcdecba 1
acdcb  1
Output
1
0
0
abhishekvicky123453 months ago
/*t=string length-Longest Palindromic sub-sequence;
if(t≤k) return 1 else return 0 */ 
 
/*JAVA SOLUTION*/
 
class GfG{public int lps(String s){String rev="";for(int i=s.length()-1;i>=0;i--)rev=rev+s.charAt(i);int[][] ary=new int[s.length()+1][s.length()+1];for(int i=0;i<=s.length();i++){    for(int j=0;j<=s.length();j++)    {        if(i==0 || j==0)ary[i][j]=0;        else if(s.charAt(i-1)==rev.charAt(j-1))        ary[i][j]=ary[i-1][j-1]+1;        else if(s.charAt(i-1)!=rev.charAt(j-1))        ary[i][j]=Math.max(ary[i][j-1],ary[i-1][j]);    }}return ary[s.length()][s.length()];}   boolean is_k_palin(String str, int k)   {int t=str.length()-lps(str);if(t<=k)return true;return false;   }}
0
ghoshghoshbishal4 months ago
No DP used → .(All test cases pased, but I'm not sure it is fully correct or not)
 
boolean is_k_palin(String str, int k)   {       if(str.length() == 1) return true;       int start = 0;       int end = str.length()-1;       while(start <= end){           if(str.charAt(start) != str.charAt(end)){               if(k > 0){                   k--;                   if(str.charAt(start) == str.charAt(end-1))                       start--;                   else                       end++;               }               else return false;           }           start++;           end--;       }       return true;   }
0
ANAS MALVAT8 months ago
ANAS MALVAT
https://uploads.disquscdn.c...
0
Imran Wahid9 months ago
Imran Wahid
Easy C++ solution https://uploads.disquscdn.c...
0
Sakshi Agarwal10 months ago
Sakshi Agarwal
bool is_k_palin(string s,int k){//Your code hereint n = s.size();int dp[n+1][n+1];memset(dp,0,sizeof(dp));string str = s;reverse(str.begin(),str.end());for(int i=1;i<=n;i++){    for(int j=1;j<=n;j++){        if(s[i-1]==str[j-1])        dp[i][j]=dp[i-1][j-1]+1;        else        dp[i][j] = max(dp[i-1][j],dp[i][j-1]);    }}int palin = n-dp[n][n];if(palin<=k)return 1;elsereturn 0; }
0
Kartik pant10 months ago
Kartik pant
boolean find(int i, int j, String s, int k)    {        if(k < 0) return false;         if(i >= j) return k >= 0? true : false;         boolean res = false;        if(s.charAt(i) == s.charAt(j))        {            res = res || find(i+1,j-1,s,k);        }else{            res = res || find(i+1,j,s,k-1) || find(i,j-1,s,k-1);        }        return res;    }      boolean is_k_palin(String str, int k)    {return find(0,str.length()-1,str,k);    }
0
ayush yadav11 months ago
ayush yadav
simple top down approach in C++bool is_k_palin(string s,int k){    string S = s;    reverse(S.begin(),S.end());    int n = s.size();    int t[n+1][n+1];    for(int i=0;i<=n;i++)    {        t[0][i]=0;        t[i][0]=0;    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            if(S[i-1]==s[j-1])            {                t[i][j] = t[i-1][j-1] + 1;            }            else            {                t[i][j]  = max(t[i-1][j] , t[i][j-1]);            }        }    }    return (n-t[n][n]<=k)?1:0;}
0
G Vishal1 year ago
G Vishal
https://ide.geeksforgeeks.o...https://uploads.disquscdn.c...
0
Deepak Verma1 year ago
Deepak Verma
Bottom up DP approach without LCS
int dp(string str) {    int len = str.length();    int tab[len][len];
    for(int i=len-1;i>=0;i--) {        for(int j=0;j<len;j++) {="" if(i="">= j) tab[i][j] = 0;            else if(str[i] == str[j])                tab[i][j] = tab[i+1][j-1];            else                 tab[i][j] = 1 + min(tab[i+1][j], tab[i][j-1]);        }    }    return tab[0][len-1];
}
0
Saket Srivastava1 year ago
Saket Srivastava
Execution Time : 0.01sint lcs(string s1,string s2){    int n = s1.length();    int t[n+1][n+1];    for(int i=0;i<=n;i++){        for(int j=0;j<=n;j++){            if(i==0||j==0){                t[i][j]=0;            }else if(s1[i-1]==s2[j-1]){                t[i][j]=1+t[i-1][j-1];            }else{                t[i][j]=max(t[i-1][j],t[i][j-1]);            }        }    }    return t[n][n];}bool is_k_palin(string s,int k){   string s2;  for(int n = s.length()-1; n >= 0; n--){    s2.push_back(s[n]);  }    int x = lcs(s,s2);    if((s.length()-x)<=k){        return 1;    }else{        return 0;    }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.