Longest Common Subsequence (Dynamic Programming)
Skills:
Algorithm Basics90%
Key Takeaways
The video tutorial covers the Longest Common Subsequence problem using Dynamic Programming, explaining the general procedure for solving Dynamic Programming problems and providing a recursive solution, a memorized version, and a bottom-up approach.
Full Transcript
hey guys this is my dynamic programming tutorial with the longest common subsequence problem as our example now let me explain what the longest common subsequence problem is we have two strings let's call it P and Q and we're trying to find the common string and we're trying to find the longest one of course and in this case that would be b a d and one thing to note here is that these characters are not necessarily contigous we're going to solve this problem using dynamic programming but let's first talk about this General procedure that we have uh that we can use for solving any dynamic programming problems so the first step is to come up with a recursive solution and then we memorize the intermediate results or restore the intermediate results to make it run faster and finally we can come up with something called a bottom up approach and I'm going to explain what it means later this is an optional step by the way here's a recursive solution we're going to write this function LCS longest common sub sequence of p 0 and q0 the two input strings and we're going to return the length of the longest common subsequence instead of the subsequence itself so with our previous example we're going to return instead of b a d we're going to return the length of it which is three the idea behind any recursion is that of course we're going to take down this problem into smaller problems and we're going to solve those instead and there are two cases we need to consider for that the first case is when p 0 and q0 and with the same character let's call let's just call it X and there are some preceding characters let's call them P1 and q1 they could be any links they could be empty or non empty what we say this case is that the longest common subsequence of p 0 and q0 must end with x and so we'll get rid of these and we'll find the LCS of P1 and q1 and we'll append it there before X and that's going to be our longest common subsequence so that's expressed as LCS of p 0 and q0 is equal to 1 which comes from X Plus LCS of P1 and q1 the second case is when p 0 and q0 don't end with the same character so let's just call those characters X and Y and again there are some preceding characters before X and Y let's call them P1 and q1 again they could be any lengths and what we do in this case is we'll say okay let's just get rid of one of the characters let's just say x and we will find the LCS of P1 and q0 which is what I wrote here and we'll do the same thing with Y and find the LCS of P0 and q1 and we'll take the longer one of those so that's expressed as LCS of P0 q0 is equal to maximum or the larger one of those here's a recursive solution in code we Define the function LCS of PQ n m and n and M those are integers they are here because I don't want to uh recreate strings every time I call this function here's what I mean so let's say p equals ABC and we have Nal 2 then instead of looking at the whole string we'll just look at the first two characters and it's the same thing with q and M if Q is ABC and M is one we'll just look at the first character so this way we don't have to reproduce strings every time and here's our base case if n equals z or m equals 0 that means we're looking at an empty string so we'll just return zero and I'm storing uh this result in the result variable and returning it here if that's not the case and if the first Char if the last character of p and the last character of Q are the same we'll just return 1 plus LCS of PQ nus one M minus one and if that's not the case the last characters are not going to be the same but I wrote this just for clarity and what we're going to do here is take LCS of PQ n minus one m and LCS of PQ n m minus one and we'll take the maximum one of those and return that here's a quick analysis over a recursive solution we'll look at one of the worst case scenarios when p and Q don't have any characters in common and we'll look at this particular example when P equals AA and Q equals BBB first we'll call LCS of PQ 23 because we have two characters and three characters and to find that we need to call LCS of PQ 13 I just abbreviated as l13 and we'll also need to call l22 and to find l13 we need to call l03 which is a base case and l12 and so on that's what this diagram shows and as you can see the problem with this approach is that there are a lot of duplicates in our computation so we are Computing l12 twice the exact same competition and we're Computing l11 three times and that's why this is very wasteful and it's very slow in fact the time it takes to find the or original LCS is about an order of 2 to the power of n plus m in the worst case scenario and dynamic program says why not just store all those intermediate results so we can make this uh function run faster so now that we found a recursive solution we're going to memorize or store the intermediate results to make our function run faster here's a memorized version of our previous recursive function the only things that changed from the previous function are these three lines and what we're doing here is we're storing the intermediate results in this array in this two dimensional array of height n and width M and every time this function is called we ask ourselves do we or do we already uh have this result so we're initializing each element to undefined and we ask ourselves do we already have have it and if we already have the intermediate results if this is not undefined or if it's already defined then return that instead of going through the whole function and if that's not the case we go through the whole function find the result but before we return it we store it in this array so what's the run time for this one thing you can see right here is that we reach this last line only at most n m times because that's the number of variable combinations we have the possible variable combinations we have and so we go through this whole function only at most n m times and in this part we call LCS at most twice so the number of recursive calls is at most to NM times and each time the time it takes to execute each call is a constant time so the whole time complexity is an order of n * m which is much better than what we had before so we found a recursive solution and we memorized intermediate results and now we can come up with the bottom up solution which is an optional step in our recursive and memorize Solutions what we did was let's just say for this particular example P equal a a and qals aab what we did was we called LCS of PQ 23 and we said okay to find this we need to call LCS of PQ 13 and LCS of PQ 22 and so on so we started at the top and we went down so it's a top down approach we can also use a bottom up approach so start with m equal Z and Nal Z and ask ourselves what's LCS of PQ z0 that's that's of course zero and these are going to be all zeros and we can fill out this whole table so what's LCS of pq11 for that one we'll be comparing just these two characters so that's going to be one in the code we are actually using this the information from here and same thing with Nal 1 m equals 2 we're comparing these three characters so that's going to be one that comes from this part so we're going to fill out this whole table that way once we fill out this whole table we can just look at the last value Nal 2 and m = 3 and that's the value we wanted in the first place all right hopefully you like the video if anything was unclear please let me know and if you like this one you might also like this other video I have about dynamic programming with Fibonacci Sequence and if you want to watch more videos like this one you can just subscribe right here and thanks so much
Original Description
Dynamic Programming Tutorial with Longest Common Subsequence
Keywords:
Dynamic Programming Longest Common Subsequence
Dynamic Programming Tutorial with LCS
Playlist
Uploads from CS Dojo · CS Dojo · 4 of 60
1
2
3
▶
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Introducing aiolocust: Load tests in plain Python with asyncio & freethreading
Medium · Python
Which AI Tools Are Useful for Interior Design? A Practical Guide for Designers in 2026
Medium · AI
How to Create an Anime Album Cover with AI: Build a Full Debut Kit
Medium · AI
Why Text-Only Q&A is Dead: Introducing AQS (Anonymous Voice & AI Insights)
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI