Techie Delight
Coding made easy
Given a string, find minimum number of deletions required to convert it into a palindrome.

For example, consider the string ACBCDBAA. The minimum number of deletions required are 3.
B C B A A   or   A B C A   —>   A B C B A

The idea is to use recursion to solve this problem. The idea is compare the last character of the string X[i..j] with its first character. There are two possibilities –

1. If the last character of the string is same as the first character, no deletion is needed and we recur for the remaining substring X[i+1, j-1]

2. If last character of string is different from the first character, then we return one plus minimum of the two values we get by
This yields the below recursive relation –

Below solution finds the minimum number of deletions required to convert a sequence X into a palindrome recursively by using above relations.

C++

Download   Run Code

Output:

The minimum number of deletions required are 3

Java

Download   Run Code

Output:

The minimum number of deletions required are 3

Python

Download   Run Code

Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

The worst case time complexity of above solution is exponential O(2n) and auxiliary space used by the program is O(1). The worst case happens when there is no repeated character present in X and each recursive call will end up in two recursive calls.

The problem has an optimal substructure. We have seen that the problem can be broken down into smaller subproblems which can further be broken down into yet smaller subproblems, and so on. The problem also exhibits overlapping subproblems we will end up solving the same subproblem over and over again. Let us consider recursion tree for sequence of length 6 having all distinct characters (say ABCDEF).
LPS problem

As we can see, the same sub-problems (highlighted in same color) are getting computed again and again. We know that problems having optimal substructure and overlapping subproblems can be solved by dynamic programming, in which subproblem solutions are Memoized rather than computed again and again. This is illustrated below –

C++

Download   Run Code

Output:

The minimum number of deletions required are 3

Java

Download   Run Code

Output:

The minimum number of deletions required are 3

Python

Download   Run Code

Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

The time complexity of above solution is O(n2) and auxiliary space used by the program is O(n2).

This problem is also a classic variation of Longest Common Subsequence (LCS) problem. The idea is to find the Longest Palindromic Subsequence of given string. Then the minimum number of deletions required will be size of the string minus size of the longest palindromic subsequence. We can easily find the longest palindromic Subsequence by taking Longest Common Subsequence of a given string with its reverse i.e. call LCS(X, reverse(X)).

C++

Download   Run Code

Output:

The minimum number of deletions required are 3

Java

Download   Run Code

Output:

The minimum number of deletions required are 3

Python

Download   Run Code

Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

Download   Run Code
Output:

The minimum number of deletions required are 3

The time complexity of above solution is O(n2) and auxiliary space used by the program is O(n2).

Exercise: Write bottom-up solution for above recursive top-down version.

1 Star2 Stars3 Stars4 Stars5 Stars (40 votes, average: 5.00 out of 5)
Loading…
Thanks for reading.
Guess there’s a typo in the explanation of the recurrence relation. It should be T[i..j] = 1 + min ( T[i-1..j], T[i+1…j] ).
Thanks a lot for bringing this to our notice. We have updated the article. Happy coding 🙂

This will result in incorrect output for, case when input is ACBCDBA, per your program output is 4, it should be 2.
Though idea is good, you should be checking for next and previous character at every step before incrementing the flag.
another simple idea can find LCS of string and its reverse string.
and output s.length()-LCSvalue

I think you mean to say longest palindromic subsequence not the substring.
Thanks a lot for bringing this to our notice. We have updated the typo. Happy coding 🙂
The space complexity would be O(N) if we use unordered map.
O(N^2) is a mistake I guess admin ?

source