Find length of Longest Palindromic Subsequence (Recursion & DP)

Posted by N.K. Chauhan on Mar 31, 2024

Given a sequence "s", the task is to find the length of the longest palindromic subsequence in it.

A subsequence is a sequence that appears in the same relative order but is not necessarily contiguous.

A palindrome is a sequence of characters which reads the same backward as forward, such as "madam" or "racecar".

Input: "bbabcbcab", Output: 7

Input: "abcd", Output: 1


Solutions

Method 1: Memoization

The Memoization Technique is basically an extension to the recursive approach so that we can overcome the problem of calculating redundant cases and thus decrease time complexity.

We can see that in each recursive call only the value of "n" and "m" (length) changes, so we can store and reuse the result of a function(..n, m..) call using a "n * m" 2-D array.

The 2-D array will store a particular state (n, m) if we get it the first time.

Now, if we come across the same state (n, m) again, instead of calculating it in exponential complexity, we can directly return its result stored in the table in constant time.

Complexity

The time complexity of this solution is (n * m).

In addition, O(n * m) auxiliary space was used by the table.

Method 2: Recursion

This Print length of Longest Palindromic Subsequence (LPS) is a variation of the Longest Common Subsequence (LCS).

The only extra thing we need to do is to obtain a reverse string "s2" of the given string "s1". This will act as the second string for LCS.

The idea is to generate all subsequences of both "s1" and "s2" and print the longest matching subsequence.

Start from the last elements (n-1, m-1) of both subsequences:

If s1[n-1] == s2[m-1], this character will be part of the longest subsequences, so add it to the current subsequence and check for the remaining (0...n-2, 0...m-2) characters.

If s1[n-1] == s2[m-1], then check recursively for (0...n-2, 0...m-1) and (0...n-1, 0...m-2) and return the longest subsequence of these two.

If any of the subsequences is exhausted (m ==0, n ==0) return "0". This is the base condition.

Complexity

This solution is exponential O(2^(m+n)) in term of time complexity.

In addition, O(2^n) auxiliary stack space was used for the recursion stack.

Related


What is "Subset Sum Problem" ?

Equal subset sum partition problem

Print Shortest Common Super-sequence (SCS)

Unbounded knapsack (Recursion & Dynamic Programming)

Shortest Common Super-sequence (SCS)

Minimum number of deletions and insertions

Longest Common Subsequence (LCS)

What is the "Rod Cutting Problem" ?

Find length of the Longest Common Substring

Print Longest Common Subsequence (LCS)

Count number of subsets with given sum