The Longest Palindromic Subsequence (LPS) problem is finding the longest subsequences of a string that is also a palindrome.

The problem differs from the problem of finding the longest palindromic substring. Unlike substrings, subsequences are not required to occupy consecutive positions within the original string.

 
For example, consider the sequence ABBDCACB.

The length of the longest palindromic subsequence is 5
The longest palindromic subsequence is BCACB

Practice this problem

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

  1. If the string’s last character is the same as the first character, include the first and last characters in palindrome and recur for the remaining substring X[i+1, j-1].
  2. If the last character of the string is different from the first character, return the maximum of the two values we get by
    • Removing the last character and recursing for the remaining substring X[i, j-1].
    • Removing the first character and recursing for the remaining substring X[i+1, j].

This yields the following recursive relation to finding the length of the longest repeated subsequence of a sequence X:

           | 1                                 (if i = j)
LPS[i…j] = | LPS[i+1…j-1] + 2                  (if X[i] = X[j])
           | max (LPS[i+1…j], LPS[i…j-1])      (if X[i] != X[j])

The algorithm can be implemented as follows in C++, Java, and Python. The solution finds the length of the longest repeated subsequence of sequence X recursively using the above relations.

C++


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

Java


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

Python


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

The worst-case time complexity of the above solution is exponential O(2n), where n is the length of the input string. The worst case happens when there is no repeated character present in X (i.e., LPS length is 1), and each recursive call will end up in two recursive calls. This also requires additional space for the call stack.
 

 
The LPS problem has an optimal substructure and also exhibits overlapping subproblems. Let’s consider the recursion tree for a sequence of length 6 having all distinct characters, say ABCDEF, whose LPS length is 1.

 
Longest Palindromic subsequence

 
As we can see, the same subproblems (highlighted in the same color) are getting computed repeatedly. We know that problems with optimal substructure and overlapping subproblems can be solved by dynamic programming, where subproblem solutions are memoized rather than computed and again. This method is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

Java


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

Python


Download  Run Code

Output:

The length of the longest palindromic subsequence is 5

The time complexity of the above solution is O(n2) and requires O(n2) extra space, where n is the length of the input string.

 
However, both the above-discussed methods only find the longest palindromic subsequence length but does not print the longest palindromic subsequence itself.

How can we print Longest Palindromic subsequence?

The longest palindromic subsequence problem is a classic variation of the Longest Common Subsequence (LCS) problem. The idea is to find LCS of the given string with its reverse, i.e., call LCS(X, reverse(X)) and the longest common subsequence will be the longest palindromic subsequence.

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The length of the longest palindromic subsequence is 5
The longest palindromic subsequence is BCACB

The time complexity of the above solution is O(n2) and requires O(n2) extra space, where n is the length of the input string.

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