VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-if-string-is-k-palindrome-or-not/

⇱ String is K-Palindrome or Not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

String is K-Palindrome or Not

Last Updated : 11 May, 2026

Given a string s, 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: s = "abcdecba", k = 1
Output: true
Explanation: String can become palindrome by removing 1 character i.e. either d or e.

Input: s = "abcdeca", k = 2
Output: true
Explanation: Can become palindrome by removing 2 characters b and e.

Input: s= "acdcb", k = 1
Output: false
Explanation: String can not become palindrome by removing only one character.

[Naive Approach] Edit Distance Recursion and Reverse – O(2 ^ n) Time and O(n) Space

If we carefully analyze the problem, it is basically a variation of Edit Distance. We can modify the Edit Distance problem to consider the given string and its reverse as input and the only operation allowed is deletion. To form a palindrome, the same character has to deleted from both s and its reverse. Therefore, for a string to be k-palindrome, total deletions <= 2*k should hold true.

  • If the last characters of the two strings are same, we ignore last characters and get count for remaining strings. So we recur for lengths m-1 and n-1 where m is length of s1 and n is length of s2.
  • If the last characters are not same, remove last char from s1 and recur for m-1 and n. Then remove last char from s2 and recur for m and n-1. We take the minimum of two operations and add 1 to it.

Output
True

[Expected Approach 1] Edit Distance Bottom Up and Reverse – O(n²) Time and O(n²) Space

We mainly optimize the above recursive solution using a dp table of size (m+1)x(n+1) where dp[i][j] represents total deletions to match prefixes of lengths i and j. At the end, we return return true if dp[m][n] <= 2*k.

  • Reverse the original string.
  • Build a DP table where each cell dp[i][j] stores minimum deletions required to match prefixes of lengths i and j.
  • If characters match, move diagonally; otherwise take minimum deletion operation
  • Check if final deletions required are less than or equal to 2 * k



Output
True

[Efficient Approach] Using Longest Palindromic Subsequence (LPS) – O(n²) Time and O(n²) Space

The idea to find Longest Palindromic Subsequence (LPS) and subtract its length from total string length to find the number of deletions. We compute LPS using LCS between the string and its reverse. If the number of characters not included in the LPS is at most k, then the string can be converted into a palindrome by removing at most k characters. Thus, we only need to check whether n - LPS <= k.

  • Reverse the original string and compute LCS between both strings
  • Use dynamic programming to build the LCS table
  • The LCS length represents the longest palindromic subsequence
  • If n - LPS <= k, return true otherwise false



Output
True
Comment