K-Palindrome | Practice | GeeksforGeeks

Given a string str of length n, find if the string is K-Palindrome or not. A k-palindrome string transforms into a palindrome on removing at most k characters from it.

Example 1:
Input: str = "abcdecba"
n = 8, k = 1
Output: 1
Explaination: By removing 'd' or 'e' 
we can make it a palindrome.

Example 2:
Input: str = "abcdefcba"
n = 9, k = 1
Output: 0
Explaination: By removing a single 
character we cannot make it a palindrome.

Your Task:
You do not need to read input or print anything. Your task is to complete the function kPalindrome() which takes string str, n and k as input parameters and returns 1 if str is a K-palindrome else returns 0.

Expected Time Complexity: O(n*n)
Expected Auxiliary Space: O(n*n)

Constraints:
1 ≤ n, k ≤ 103
0
varshil3 weeks ago
        memo={}
       def ispalindrome(l,r,deletes):
           if (l,r,deletes) in memo:
               return memo[(l,r,deletes)]
           if deletes>c:
               return False
           if l>r:
               return True
           best=ispalindrome(l+1,r-1,deletes) if s[l]==s[r] else (ispalindrome(l,r-1,deletes+1) or ispalindrome(l+1,r,deletes+1))
           memo[(l,r,deletes)]=best
           return best
       N=len(s)
       if ispalindrome(0,N-1,0):
           return 1
       return 0
0
geminicode1 month ago
Please edit the expected time complexity as O(n*n) is giving TLE in python.
 
0
ankitparashxr4 months ago
time-0.1sec(java)
class Solution{   static int kPalindrome(String str, int n, int k)   {       if(n==1 || k>n)       {           return 1;       }            int count = 0;     if(n%2==0)     {         k = k+1;     }     for(int i =0;i<n;i++)     {         if(str.charAt(i)!=str.charAt(n-(i+1)))         {             count++;         }              }     if(count==k)     {         return 1;     }     return 0;   }}
0
ghoshghoshbishal4 months ago
No DP→ 
static int kPalindrome(String str, int n, int k)   {       if(str.length() == 1) return 1;       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 0;           }           start++;           end--;       }       return 1;   }
 
0
angadstyle984 months ago
java o(n) and constant space solution. Easy to understand public static int kPallindromeHelper(String s, int k) {       int count = 0, n = s.length();       for (int i = 0, j = n - 1; i <= j; i++, j--) {           if (s.charAt(i) != s.charAt(j)) count += 2;       }       // even length then remove count - 1 number of chars        // odd length then remove count number of chars       int ans = 0;       if ((n & 1) == 0) ans = count - 1;       else ans = count;
       if (ans <= k) return 1;       else return 0;   } 
0
samsasuke76 months ago
class Solution{public:   int kPalindrome(string s1, int n, int k)   {       // code here              int t[n+1][n+1];       string s2= s1;              if(s1.length()==1)           return 1;                  if(s1.length()<=k)           return 1;              reverse(s2.begin(), s2.end());              for(int i=0;i<n+1;i++){                      for(int j=0;j<n+1;j++){                              if(i==0 || j==0)                   t[i][j] = 0;                              }       }              for(int i=1;i<n+1;i++){                      for(int j=1;j<n+1;j++){                              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]);           }       }       //cout<<t[n][n]<<endl;       int size = n-t[n][n];       //cout<<size<<endl;              if(size-k == 0)           return 1;                  else        return 0;          }}; 
0
ransomsumit16 months ago
I don't know why people have used dp approach?
int kPalindrome(string str, int n, int k)
{
    int i = 0;
    int j=n-1;
    while(i<j)
    {
        if(str[i]==str[j])
        {
            i++;j--;
        }
        else
        {
            if(i+1<j && str[i+1]==str[j])
            {
                i++;
                k--;
            }
            else if(j-1>i && str[i]==str[j-1])
            {
                j--;
                k--;
            }
            else
            {
                if(i+1 == j)
                {
                    i++;
                    k--;
                }
                else
                {
                    i++;
                    j--;
                    k-=2;
                }
            }
        }
    }
    if(k<0) return 0;
    return 1;
}
0
Saurabh Sharma8 months ago
Saurabh Sharma
https://uploads.disquscdn.c... Simple Java Solution DP Using LCS
0
Saurabh Sharma
This comment was deleted.
0
Samundar Singh9 months ago
Samundar Singh
https://uploads.disquscdn.c...
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.