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.


String A = "acbaed";
String B = "abcadf";

Longest Common Subsequence(LCS):     acad, Length: 4



Start comparing strings in reverse order one character at a time.

Now we have 2 cases –

  1. Both characters are same
    1. add 1 to the result and remove the last character from both the strings and make recursive call to the modified strings.
  2. Both characters are different
    1. 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 :4

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.

Dynamic Programming:

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.



LCS :4

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):


 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

Click here to see the code for Dynamic Programming – Top Down Approach:

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

%d bloggers like this: