Given a string A, compute the minimum number of characters you need to delete to make resulting string a palindrome.
Input : baca Output : 1 Input : geek Output : 2
We have discussed one approach in below post.
Minimum number of deletions to make a string palindrome
Below approach will use modified Levenshtein distance. We consider modified Levensthein (considering only deletions) both original string and its reverse.
Time complexity: O()
Space complexity: O()
where is length of string
Why is it working?
To understand it we need to start from the very beginning of how we create dp, for example for word “geek”, it initially looks like this:
Both 1st row and 1st column are filled with number 1..4 as this is the number of modifications needed to create empty string, i.e:
 == 1, to create empty string from letter ‘g’ remove this one letter
 == 2, to create empty string from letters “ge”, remove both those letters, etc.
The same story for first column:
 == 1, to create empty string from letter ‘k’ remove this one letter
 == 2, to create empty string from letters “ke”, remove both those letters, etc.
Now, we are using dynamic programming approach to get the minimum number of modifications to get every other substring to become second substring, and at the end out dp looks like this:
So for example minimum number of modifications to get substring ‘gee’ from ‘kee’ is 2. So far so good but this algorithm is doing two things, it is both inserting and deleting characters, and we are only interested in number of removals. So let’s one more time take a look at our resulting array, for example at entry , this entry states:
 – to make string ‘g’ from string “keeg” we need to perform 3 modifications(which is just delete chars “kee”)
 – to make string “ge” from “kee” we need to perform 3 modifications also by removing from first string ‘g’ and from second ‘ke’
So basically every time we will be moving diagonally up, from left corner we will get number of removals to get the same substring backwards. Thing to notice here is that it is like having on string two pointers, one moving from beginning and other from end. Very important spot is that strings do not necessary has to have even number of characters, so this is the reason we also has to check upper and lower values in dp.
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.
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a sorted sequence
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Minimum number of deletions and insertions to transform one string into another
- 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 Appends needed to make a string palindrome
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Minimum characters to be added at front to make string palindrome
- Count minimum swap to make string palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Minimum removal to make palindrome permutation
- Minimum changes required to make each path in a matrix palindrome
- Remove a character from a string to make it a palindrome
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum changes required to make first string substring of second string
- Minimum steps to delete a string after repeated deletion of palindrome substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.