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

Examples:

Input :String - abcdecba, k = 1Output :Yes String can become palindrome by removing 1 character i.e. either d or eInput :String - abcdeca, K = 2Output :Yes Can become palindrome by removing 2 characters b and e (or b and d).Input :String - acdcb, K = 1Output :No String can not become palindrome by removing only one character.

We have discussed a DP solution in previous post where we saw that the problem is basically a variation of Edit Distance problem. In this post, another interesting DP solution is discussed.

The idea is to find the longest palindromic subsequence of the given string. If the difference between longest palindromic subsequence and the original string is less than equal to k, then the string is k-palindrome else it is not k-palindrome.

For example, longest palindromic subsequence of string **abcdeca** is **acdca**(or aceca). The characters which do not contribute to longest palindromic subsequence of the string should be removed in order to make the string palindrome. So on removing b and d (or e) from abcdeca, string will transform into a palindrome.

Longest palindromic subsequence of a string can easily be found using LCS. Following is the two step solution for finding longest palindromic subsequence that uses LCS.

- Reverse the given sequence and store the reverse in another array say rev[0..n-1]
- LCS of the given sequence and rev[] will be the longest palindromic sequence.

Below is C++ implementation of above idea –

## CPP

`// C++ program to find if given string is K-Palindrome ` `// or not ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Returns length of LCS for X[0..m-1], Y[0..n-1] */` `int` `lcs( string X, string Y, ` `int` `m, ` `int` `n ) ` `{ ` ` ` `int` `L[m + 1][n + 1]; ` ` ` ` ` `/* Following steps build L[m+1][n+1] in bottom up ` ` ` `fashion. Note that L[i][j] contains length of ` ` ` `LCS of X[0..i-1] and Y[0..j-1] */` ` ` `for` `(` `int` `i = 0; i <= m; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j <= n; j++) ` ` ` `{ ` ` ` `if` `(i == 0 || j == 0) ` ` ` `L[i][j] = 0; ` ` ` `else` `if` `(X[i - 1] == Y[j - 1]) ` ` ` `L[i][j] = L[i - 1][j - 1] + 1; ` ` ` `else` ` ` `L[i][j] = max(L[i - 1][j], L[i][j - 1]); ` ` ` `} ` ` ` `} ` ` ` `// L[m][n] contains length of LCS for X and Y ` ` ` `return` `L[m][n]; ` `} ` ` ` `// find if given string is K-Palindrome or not ` `bool` `isKPal(string str, ` `int` `k) ` `{ ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Find reverse of string ` ` ` `string revStr = str; ` ` ` `reverse(revStr.begin(), revStr.end()); ` ` ` ` ` `// find longest palindromic subsequence of ` ` ` `// given string ` ` ` `int` `lps = lcs(str, revStr, n, n); ` ` ` ` ` `// If the difference between longest palindromic ` ` ` `// subsequence and the original string is less ` ` ` `// than equal to k, then the string is k-palindrome ` ` ` `return` `(n - lps <= k); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `string str = ` `"abcdeca"` `; ` ` ` `int` `k = 2; ` ` ` `isKPal(str, k) ? cout << ` `"Yes"` `: cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# if given string is K-Palindrome ` `# or not ` ` ` `# Returns length of LCS ` `# for X[0..m-1], Y[0..n-1] ` `def` `lcs(X, Y, m, n ): ` ` ` ` ` `L ` `=` `[[` `0` `]` `*` `(n` `+` `1` `) ` `for` `_ ` `in` `range` `(m` `+` `1` `)] ` ` ` ` ` `# Following steps build ` ` ` `# L[m+1][n+1] in bottom up ` ` ` `# fashion. Note that L[i][j] ` ` ` `# contains length of ` ` ` `# LCS of X[0..i-1] and Y[0..j-1] ` ` ` `for` `i ` `in` `range` `(m` `+` `1` `): ` ` ` `for` `j ` `in` `range` `(n` `+` `1` `): ` ` ` `if` `not` `i ` `or` `not` `j: ` ` ` `L[i][j] ` `=` `0` ` ` `elif` `X[i ` `-` `1` `] ` `=` `=` `Y[j ` `-` `1` `]: ` ` ` `L[i][j] ` `=` `L[i ` `-` `1` `][j ` `-` `1` `] ` `+` `1` ` ` `else` `: ` ` ` `L[i][j] ` `=` `max` `(L[i ` `-` `1` `][j], L[i][j ` `-` `1` `]) ` ` ` ` ` `# L[m][n] contains length ` ` ` `# of LCS for X and Y ` ` ` `return` `L[m][n] ` ` ` `# find if given string is ` `# K-Palindrome or not ` `def` `isKPal(string, k): ` ` ` ` ` `n ` `=` `len` `(string) ` ` ` ` ` `# Find reverse of string ` ` ` `revStr ` `=` `string[::` `-` `1` `] ` ` ` ` ` `# find longest palindromic ` ` ` `# subsequence of ` ` ` `# given string ` ` ` `lps ` `=` `lcs(string, revStr, n, n) ` ` ` ` ` `# If the difference between ` ` ` `# longest palindromic ` ` ` `# subsequence and the original ` ` ` `# string is less ` ` ` `# than equal to k, then ` ` ` `# the string is k-palindrome ` ` ` `return` `(n ` `-` `lps <` `=` `k) ` ` ` `# Driver program ` `string ` `=` `"abcdeca"` `k ` `=` `2` ` ` `print` `(` `"Yes"` `if` `isKPal(string, k) ` `else` `"No"` `) ` ` ` `# This code is contributed ` `# by Ansu Kumari. ` |

*chevron_right*

*filter_none*

Output:

Yes

**Time complexity** of above solution is O(n^{2}).

**Auxiliary space** used by the program is O(n^{2}). It can further be reduced to O(n) by using Space Optimized Solution of LCS.

Thanks to **Ravi Teja Kaveti** for suggesting above solution.

This article is contributed by **Aditya Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find number of times a string occurs as a subsequence in given string
- Find length of longest subsequence of one string which is substring of another string
- Find if string is K-Palindrome or not | Set 1
- Find if a string is interleaved of two other strings | DP-33
- Find all distinct palindromic sub-strings of a given string
- Minimal moves to form a string by adding characters or appending string itself
- Print all possible ways to convert one string into another string | Edit-Distance
- Check if it is possible to transform one string to another
- Sum of all substrings of a string representing a number | Set 1
- Number of subsequences in a string divisible by n
- Maximum weight transformation of a given string
- Count All Palindrome Sub-Strings in a String | Set 1
- Count All Palindromic Subsequence in a given String
- Maximum difference of zeros and ones in binary string
- Rearrange a string in sorted order followed by the integer sum