Dynamic Programming – Longest Common Subsequence
Objective: Given two string sequences, write an algorithm to find the length of longest subsequence present in both of them.
These kind of dynamic programming questions are very famous in the interviews like Amazon, Microsoft, Oracle and many more.
What is Longest Common Subsequence: A longest subsequence is a sequence that appears in the same relative order, but not necessarily contiguous(not substring) in both the string.
Start comparing strings in reverse order one character at a time.
Now we have 2 cases –
- Both characters are same
- add 1 to the result and remove the last character from both the strings and make recursive call to the modified strings.
- Both characters are different
- Remove the last character of String 1 and make a recursive call and remove the last character from String 2 and make a recursive and then return the max from returns of both recursive calls. see example below
Case 1: String A: "ABCD", String B: "AEBD" LCS("ABCD", "AEBD") = 1 + LCS("ABC", "AEB") Case 2: String A: "ABCDE", String B: "AEBDF" LCS("ABCDE", "AEBDF") = Max(LCS("ABCDE", "AEBD"), LCS("ABCD", "AEBDF"))
In a given string of length n, there can be 2n subsequences can be made, so if we do it by recursion then Time complexity will O(2n) since we will solving sub problems repeatedly.
We will solve it in Bottom-Up and store the solution of the sub problems in a solution array and use it when ever needed, This technique is called Memoization. See the code for better explanation.
Print the Longest Common Subsequence:
Take a look into the LCS used in the code
Start from bottom right corner and track the path and mark the cell from which cell the value is coming and whenever you go diagonal ( means last character of both string has matched, so we reduce the length of both the strings by 1, so we moved diagonally), mark those cells, this is our answer.
Complete Code( Include Printing Result):
ACDA 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 2 2 2 0 1 2 2 2 2 0 1 2 2 3 3 0 1 2 2 3 3 0 1 2 2 3 4 LCS :4
Top Companies Interview Questions..-
If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.