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.
Input : String - abcdecba, k = 1 Output : Yes String can become palindrome by remo- -ving 1 character i.e. either d or e) Input : String - abcdeca, K = 2 Output : Yes Can become palindrome by removing 2 characters b and e. Input : String - acdcb, K = 1 Output : No String can not become palindrome by removing only one character.
If we carefully analyze the problem, the task is to transform the given string into its reverse by removing at most K characters from it. The problem is basically a variation of Edit Distance. We can modify the Edit Distance problem to consider given string and its reverse as input and only operation allowed is deletion. Since given string is compared with its reverse, we will do at most N deletions from first string and N deletions from second string to make them equal. Therefore, for a string to be k-palindrome, 2*N <= 2*K should hold true.
Below are the detailed steps of algorithm –
Process all characters one by one staring from either from left or right sides of both strings. Let us traverse from the right corner, there are two possibilities for every pair of character being traversed.
- If last characters of 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 str1 and n is length of str2.
- If last characters are not same, we consider remove operation on last character of first string and last character of second string, recursively compute minimum cost for the operations and take minimum of two values.
- Remove last char from str1: Recur for m-1 and n.
- Remove last char from str2: Recur for m and n-1.
Below is Naive recursive implementation of above approach.
The time complexity of above solution is exponential. In worst case, we may end up doing O(2n) operations. The worst case happens string contains all distinct characters.
This problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, re-computations of same subproblems can be avoided by constructing a temporary array that stores results of subproblems .
Below is Bottom-up implementation of above recursive approach :
Time complexity of above solution is O(m x n). We can improve time complexity by making use of the fact that only k deletions are allowed. Auxiliary space used is O(m x n).
Alternate Approach :
1) Find Length of the Longest Palindromic Subsequence
2) If the difference between lengths of original string and the longest palindromic subsequence is less than or equal k, return true. Else return false.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if string is K-Palindrome or not | Set 2
- Find if string is K-Palindrome or not using all characters exactly once
- Check given string is oddly palindrome or not | Set 2
- Find the smallest window in a string containing all characters of another string
- Find number of times a string occurs as a subsequence in given string
- Find a palindromic string B such that given String A is a subsequense of B
- Find the count of palindromic sub-string of a string in its sorted form
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Check if any anagram of a string is palindrome or not
- Check if two same sub-sequences exist in a string or not
- Check if there exists any sub-sequence in a string which is not palindrome
- Check given string is oddly palindrome or not
- Check if a given string is Even-Odd Palindrome or not
- Check if both halves of a string are Palindrome or not
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Find n-th lexicographically permutation of a string | Set 2
- Find all palindromic sub-strings of a given string | Set 2
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Find whether a subarray is in form of a mountain or not
- Remove characters from the first string which are present in the second string