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 removing 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 (or b and d). Input : String - acdcb, K = 1 Output : No String can not become palindrome by removing only one character.
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 the implementation of above idea –
Time complexity of above solution is O(n2).
Auxiliary space used by the program is O(n2). 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 firstname.lastname@example.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.
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 1
- 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
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- 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
- Queries to check if string B exists as substring in string A
- Longest sub string of 0's in a binary string which is repeated K times
- Split the string into minimum parts such that each part is in the another string
- Smallest String consisting of a String S exactly K times as a Substring
- Find if a string is interleaved of two other strings | DP-33
- Find all distinct palindromic sub-strings of a given string
- Find minimum number of steps to reach the end of String
- Find all possible ways to Split the given string into Primes
- Count All Palindrome Sub-Strings in a String | Set 1
- Sum of all substrings of a string representing a number | Set 1
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Minimum number of deletions to make a string palindrome | Set 2
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2