Longest Common Subsequence

Authored by HackerRank

Subsequence

A sequence in mathematics is a an enumerated collection of objects. Unlike sets, they are ordered and can have repeated elements. For example,

A subsequence in mathematics is a sequence that can be derived from another sequence by deleting certain elements without changing the order of the remaining elements. For example,

Longest Common Subsequence

The Longest common subsequence (LCS) of 2 sequences is a subsequence, with maximal length, which is common to both the sequences. The subsequence must appear in the same order in both the sequences and need not be continuous.

For example, for the two strings given below, the length of the LCS is 6.

LCS isn't necessarily unique, there can be more than one common subsequence with the maximum length.

For example, for the two strings given below, the length of the LCS is 3.

Brute Force Solution:


For every subsequence of , check whether it is a subsequence of . Consider two sequences and of length and respectively.

There are subsequences to check in and it takes time to check whether each of these subsequences is a subsequence of or not. This gives a time complexity of:

Dynamic Programming Solution:


The LCS problem follows the optimal substructure property i.e. it can be broken down into smaller subproblems until the solution for the subproblem becomes trivial. It also has overlapping subproblems. Thus, the LCS problem can be solved using dynamic programming by storing the results for the subproblems and reusing them instead of computing them over and over. Consider,

Let be the LCS of A and B of length p.

, and represent the prefixes A[1..i], B[1..j] and C[1..k].

  • If then

  • If then


We save the results for the subproblems in a 2D array so that we don't need to recompute it again and again.

stores the length of the LCS for the prefixes and

Recursive Formulation


stores the length of the LCS of A and B.

Printing the LCS

To find the length of the LCS, we used a 2D table which stores the length of the LCS for the prefixes. We can use this table to print the LCS for the two sequences.

For strings

The LCS table for these two strings is

0 0 0 0 0 0   
0 1 1 1 1 1   
0 1 1 1 2 2   
0 1 2 2 2 3   
0 1 2 2 3 3   
0 1 2 3 3 3   
0 1 2 3 4 4  

We start with the bottom right corner i.e. LCS[n][m].

If the then add this character to the and go to position Otherwise compare and LCS[i][j-1] and go to the position with higher value (The value of will be equal to this value) .

Pseudocode:

i=n,j=m
index=LCS[n][m] //We start from the bottom right corner and add the characters to the LCS from the last
while(i>0 and j>0)
    if(a[i]=b[j])
        LCS_String[index]=a[i]
        index=index-1
        i=i-1
        j=j-1
    else
        if(LCS[i][j-1]=LCS[i][j])
            j=j-1                   
        else
            i=i-1
 
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score