Given a string of size ‘n’. The task is to remove or delete minimum number of characters from the string so that the resultant string is palindrome.
Note: The order of characters should be maintained.
Input : aebcbda Output : 2 Remove characters 'e' and 'd' Resultant string will be 'abcba' which is a palindromic string Input : geeksforgeeks Output : 8
A simple solution is to remove all subsequences one by one and check if remaining string is palindrome or not. Time complexity of this solution is exponential.
An efficient approach uses the concept of finding the length of the longest palindromic subsequence of a given sequence.
-->str is the given string. -->n length of str -->len be the length of the longest palindromic subsequence of str -->// minimum number of deletions min = n - len
Minimum number of deletions = 8
Time Complexity : O(n2)
This article is contributed by Ayush Jauhari. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum number of deletions to make a sorted sequence
- Minimum number of deletions and insertions to transform one string into another
- Count minimum swap to make string palindrome
- Minimum characters to be added at front to make string palindrome
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Find minimum number of merge operations to make an array palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum number of deletions so that no two consecutive are same
- Minimum number of additons to make the string balanced
- Minimum number of swaps required to make the string K periodic