Longest Palindromic Subsequence
Longest Palindromic Subsequence
In Longest Palindromic Subsequence problem we are given a string and are asked to find length of the longest palindromic subsequence. In this article we provide a C++ implementation with explanation of the solution.
Longest Palindromic Subsequence Problem Description
In this problem we are given a input string consisting of english alphabets. We have to return the length a longest palindromic subsequence possible. A subsequence is a string that is formed by removing any number of characters (including 0) from the input string.
Input:
- Single line input containing an input string.
Output:
- Longest length required.
Longest Palindromic Subsequence Solution
Solution 1 (Memoization):
- Break Down the Problem – In this step we try to break down the problem into smaller problems and assume recursion will solve the smaller problems.
F(i,j) = Length of longest Palidromic Subsequence starting from i till j index.
Now there are two cases possible:
F(i,j) = 2+ F(i+1,j-1) { if s[i]==s[j]}
Or
F(i,j) = max(F(i+1,j),F(i,j-1)) { if s[i]!=s[j]}
- Find the base case – What can be base case? It’s easy find the solution to smallest known problem. Here we know solution for length 1 ie (i==j) and length 2 ie (i+1==j).
- Check for optimal substructure and overlapping sub problems – It is easy to see that the problem can be broken down into smaller problems. The optimal substructure and overlapping sub problems properties can be visualized below.
Code
Output
Enter input string :azbyncdcwbmaqx
Longest length is 7
Solution (Bottom Up Dynamic Programming)
In above solution we created a function that recursively solves smaller problems. Here we create a lps table and solve problems of smaller length first. In other words we find longest palindromic subsequence for sub strings of length = 1, then for length = 2 and so on.
Code
Output
Enter input string :azbyncdcwbmaqx
Longest length is 7
Login/Signup to comment