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.
- Minimum number of deletions to make a string palindrome | Set 2
- Ways to sum to N using array elements with repetition allowed
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Clustering/Partitioning an array such that sum of square differences is minimum
- Longest Non-palindromic substring
- Minimum number of Appends needed to make a string palindrome
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Minimum characters to be added at front to make string palindrome
- Maximum Product Cutting | DP-36
- Minimum insertions to form a palindrome | DP-28
- Largest Independent Set Problem | DP-26
- Longest Palindromic Substring | Set 1
- Longest Palindromic Subsequence | DP-12
- Matrix Chain Multiplication | DP-8
- Longest Common Subsequence | DP-4
Improved By : nitin mittal