Longest Common Subsequence
Longest Common Subsequence
In Longest Common Subsequence we have to find the common subsequence of two strings. It a very famous question and it is asked many time in coding tests and interviews. In this article we provide a c++ implementation with Explanation to the problem.
Longest Common Subsequence Problem Description
In this problem we are given two input strings consisting of English alphabets. We have to return the length of longest common subsequence possible. A subsequence is a string that is formed by removing any number of characters (including 0) from the input string.
Input:
- First line containing input string1.
- Second line containing input string2.
Output:
- Longest length required.
Longest Common 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 common subsequence of s1 till i and s2 till j.
Now there are two cases possible:
F(i,j) = 1+ F(i-1,j-1) { if s1[i]==s2[j]}
Or
F(i,j) = max(F(i-1,j),F(i,j-1)) { if s1[i]!=s2[j]}
- Find the base case – What can be base case? It’s easy, find the solution to smallest known problem. Here we know solution when we reach the end of any strings ie we have no characters to be match we simply return 0.
- 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
axbca
aybza
3
Solution (Bottom Up Dynamic Programming)
In above solution we created a function that recursively solves smaller problems. Here we create a dp table and solve for each i,j starting from (0,0) to (s1.length(),s2.length()). Refer to code for more details.
Code
Output
axbca
aybza
3
Login/Signup to comment