Longest Common Subsequence (Top-Down)— Day 52(Python)

Image for post
Image for post
Photo by Les Anderson on Unsplash

Today’s article is the second part of the three-part series of Longest Common Subsequence(LCS) problem. You can follow this link if you need to understand how to solve the LCS problem using recursion.

1143. Longest Common Subsequence

Given two strings text1 and text2, return the length of their longest common subsequence.

A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, “ace” is a subsequence of “abcde” while “aec” is not). A common subsequence of two strings is a subsequence that is common to both strings.

If there is no common subsequence, return 0.

Example 1:

Example 2:

Example 3:


  • 1 <= text1.length <= 1000
  • 1 <= text2.length <= 1000
  • The input strings consist of lowercase English characters only.

Before we learn about Top-Down method, let us recall the code for LCS using recursion.

In the above code, we are using recursion to find the length of longest common subsequence. If we observe the last line, we see a presence of overlapping subproblem.

Confused? Let us take an example.

Below is the two input string we have. Since we start from the end, we compare the characters at the end of the input string. In our case, the characters at the end are “c” and “e”. Since both the characters do not match, we move forward. When we continue to compare, we see, at some point, the input strings are repeated. It is an example of an overlapping sub-problem.

Since we want to avoid recalculating the repeating sub-problems, we can memoize our answers in some kind of a data structure.

In the recursive function, the size of the input string keeps changing. Anything that keeps changing, has to be memoized. In this case, we will memoize the length of the strings and their results.

Since we need to keep track of both the length of input strings, we will use a 2D array to store the results. The dimensions of the array will be length of both the input strings + 1. We will fill the cell values with MAX INT value.

When we wrote the recursive, the base condition was as follows,

We will be initialising the same in memoized code too. The cells in first row and first column is initialised with 0.

Next, we will fill the rest of the values in our table. Let us recall rest of the recursive function.

Only change we need to bring in here is passing the result to our memoization table. Let us see how it is done.

Another addition we need to make is to check if the cell value at particular position is already filled. If its filled before, then we will pass that value instead of recalulating.

Let us look at the complete code.

Complexity analysis.

Time Complexity

Since we are avoiding repetitive calculations, the time complexity of the above algorithm is O(M*N) where M is the size of string1 and N is the size of string2.

Space Complexity.

The number of recursive calls has been reduced due to memoization. Hence the space complexity if O(M*N) where M is the size of string1 and N is the size of string2.

Link to Aditya Verma’s video

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store