Dp 25. Longest Common Subsequence | Top Down | Bottom-Up | Space Optimised | DP on Strings
Вставка
- Опубліковано 22 лис 2024
- Lecture Notes/C++/Java Codes: takeuforward.o...
Problem Link: bit.ly/3pvkqUd
Pre-req for this Series: • Re 1. Introduction to ...
a
Make sure to join our telegram group for discussions: linktr.ee/take...
Full Playlist: • Striver's Dynamic Prog...
In this video, we solve the LCS Dp, this is the first problem on the pattern Strings on DP.
If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.o...
You can also get in touch with me at my social handles: linktr.ee/take...
Understood and you've completely changed my life. Doesn't matter if I get placed in a good company or not but the quality of this lectures are supreme and I will carry this knowledge for rest of my life.
Keep trying. It will work out. Dream for the day when you are so good that you REJECT your dream company.
@@AdityaKumar-be7hx Requires god level consistency to do that.
You will probably forget the concepts, since these are never used in the industry. What a satire !
🤣🤣@@idris110
brother are you placed
If someone is following Striver's series then this LCS is also a cakewalk..#Striver gives u confidence and you are no longer scared of dp😁
In the future, Mr. Vikramaditya will go down as the G.O.A.T online DSA teacher.
Yes he is GOAT
I am the future
He is already for me.
No a Guy named Srikar From Vizag will go down as the GOAT online DSA in future
@@v7gamerff809as usual, just a dumb guy from ap.
True
Protect this guy at all costs. Thank you sir for all your hard work in making this video.
Everyone should be protected bro
15:34 "You kow i am hearing you" in the background
So?
putting your years of hardwork in some videos ,how lucky we are,Thanks a lot striver bhaiya for everything you are doing for us
46:05 for space optimization we don't require the loop for prev as the values are already zero in it.
Loop is required in C++ to initialize array with 0s. But for Java it is not required as by default array is declared with 0s.
@@dikshamakkar2850 I have a question , vector dp(n + 1, vector(m + 1,0)); this line of code initializes all index to 0 then why did he ran 2 more loops to initialize some row and column to 0 again?
@@dikshamakkar2850 nah loop isnt required
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int n = text1.size();
int m = text2.size();
vectorprev(m+1,0),curr(m+1,0);
for(int i=1;i
you're right but that's write for clarity purpose that how we are generating tabulation from already written memorization solution
@@vizzz8906 he forgot to remove this loop.
I used to be scared of Dp when I started coding journey, but when I am actually doing it, it's cake walk. Thankyou striver for sharing your knowledge.
You're a great teacher. If possible, please do problems about DP on tree. I struggle with them. Thank you!
If someone is using java to write the code, in space optimization approach there will be error in a testcase, to resolve that error replace prev = curr with prev = curr.clone(); or else use the code snippet
int [] temp = prev;
prev = (curr);
curr = temp;
Because in java prev = curr will not create a new array but it will point both the references to same address.
Your program would work fine.
understood bhaiya!!! after a very long time i am back to your channel....previously i was doing a race that as soon as you upload the video, i should watch it then n there, before the next video gets uploaded in this dp series, but due to some busy schedule, i lost the race. yeah, but your quality and energy is still the same as your starting videos
Understood!! I don't think any other explanation video can match this one. You are God sent!!
This was life-changing. Thank you Striver, you taught what paid courses could not for so many years. I wish I had discovered your site earlier; it would have saved so much time and energy.
No other video on the topic will offer you a better explanation than this! This is just pure teaching excellence! Subscribed.
I am very grateful to you for uploading this playlist. This is great!!!!!!!!!!!!!!!!!!!!!
Understood.
What a brilliant explanation, I come here after watching Aditya Verma's videos on DP but didn't feel the intuition behind the tabulation that he was doing and now I know the thought process all thanks to Striver.
Its taking while to digest this information for me ,
Just imagine the efforts this guy is adding to make it available for everyone.
Honestly, everytime i listen to you dude i feel like i can handle anything ! much love from Paris
best explanation I have received ever. U are the best. Im watch this whole playlist to prep for my interviews
bro!!!!!! What an explanation, absolutely brilliant. I am starting to love coding now. Thanks
for those who don' t want to use shifting of index in tabulation , they can do it in traditional way using below code. we can discuss if you have any doubts.
int longestCommonSubsequence(string text1, string text2) {
int n1= text1.size();
int n2= text2.size();
vector dp(n1, vector(n2, -1));
//filling first coloumn of the matrix
//say we have text1= bcade and text2=a then
//the longest cmmn subsqnce is 0 until index1 reaches 2, once it reaches 2 beyond that lcs =1;
for(int i=0; i
absolutely minds bogling.i am not kidding i wasn't able to solve simple recusion questions few weeks ago now i can solve medium and hard dp questions on my own without watching videos.all i can say thank you striver.
simple space optimization technique : just 2,3 chanes
instead of using 2 different vectors we can just change dimension of dp to [no of previous rows we wanred + 1][no of colum]
int longestCommonSubsequence(string s1, string s2) {
vectordp(2, vector(s2.size()+1, 0)); // changing dimension as per need
for(int i=1; i
US ❤
I should mention I am solving DSA consistently from past 1 year. Encountered this question a lot of times.
But till date this is the best explanation boss❤
UNDERSTOOD.....Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
Understood!!!
Same tabulation method that you taught in previous videos... Same approach without index shifting.
Base case in Memoization :
if(index1 == 0 || index2 == 0) {
return s1.charAt(index1) == s2.charAt(index2) ? 1 : 0;
}
Tabulation :
static int tabulation(int index1, int index2, String s1, String s2) {
int[][] dp = new int[s1.length()][s2.length()];
dp[0][0] = s1.charAt(0) == s2.charAt(0) ? 1 : 0;
for(int i = 1; i
Giving my best shot for preparing for my placements. Let's see what happens in upcoming months. Great Content
you are a god level teacher🙇♂🙇♂🙇♂
never thought dp would be so easy
I solved with different tabulation approach (without shifting index) as below:
int m=text1.length();
int n=text2.length();
int[][] dp = new int[m][n];
int temp=0;
for(int i=0; i
same
understood, always in awe of you genius and hard working you are:)
In Tabulation we are declaring vector with values zero. So we can skip first two loops.
who agrees that this dp series is one of the best on UA-cam
Not one of the best no. THE BESTTTTT!!!!!
I was able to see that curr= prev; mistake as you were typing it. thanks to you Striver. you are the best
most in depth explanation, thanks
if someone doesn't want shifting they can write base case as below
idea is to make let say dp[0][i1]=1 and also all further i>i1 equal to 1 eg dp[0][i1+1]=1,dp[0][i1+2]=1,....
similarly for second loop also
remember what dp[i][j] represents: ;length of lcs of substring s1[0...i] and s2[0..j]
bool b=false;
for(int i=0;i
Understood. Only one point, if you are doing the tabulation to eliminate the O(m*n) S.C and then including (m+n) extra space for an extra row and column, it seems a bit redundant.
m+n
bro striver you have taught so well that i didnt even need to watch the video,i solved the problem in all three ways,thaank you very much❤
Very nice explanation sir, Thank you!
You can't find better explanation than this, Brilliant!!
Can also be done using single array if we preserve cur[j-1] in a prv variable
Yeah.. you guys are learning fast 😍
@@takeUforward thanks to your playlist💯
@@tejasghone5118 can u provide the code
@@jaykumargupta7307
int longestCommonSubsequence(string s1, string s2) {
int n = s1.length(), m = s2.length();
vector cur(m+1, 0);
int prev;
for(int ind1=1; ind1
can u give the video link in which he taught this space opt technique
What an easy explanation, loved it !
I deeply understand recursion, memoization, tabulation and space optimization.
he is the example of beauty with brains
understood .....thanku striver .....let's finish this dp on string topic
IN TABULATION WE CAN STORE 0 INTIALLY AND skip the base case loops which is used to store 0 in 0th row and column and same for space optimization
If you started new topic like DP on string it is important that you should explain each problem with tabulation method also thoroughly, instead of just writing code, but Great Series.
pro tip -> use right shift method in all questions instead of using index. this way its way easier to write base cases, because you can clearly call out whats the answer when length of array is 0
I didn't get it could you please explain a lil bit.
In interviews do we need to show the overlapping subproblems by taking longer examples or we can simply move on with memoization?
take till 5 to 6 steps., if recruiter does want , want he will tell u., to move to next step.
Amazing lecture sir ❤❤❤.
Thanks for all your efforts for us.
Man the content is gold.
Again @takeUforwad, just letting you know, again that's stuff you have done, is GOD's own work, thank you for from bottom of my heart, thanks !!!
Believe or not Striver
I solved the problem by myself without seeing this video
Note : I gone through all your previous dp videos properly with no skip :)
I am so happy.. Thanks to you.
Understood very well
striver help this brother out!
how you are staying disicplined?
so far you are single ,right?
how you are avoiding every distractions and focusing on your goal
please make a video on this brother!!
understood.wonderful. amazing. hats-off. unmatchable.
understood,previously I mugged up the tabulation formula for the exam ,but now I know how it came.
Without right shifting the Indexes, still we could do it as default initialise with 0 and then start from i=0 and j=0 but adding constraints of out of bounds.
#include
//Tabulation Solution
int lcs(string s, string t)
{
int n1=s.size();
int n2=t.size();
vectordp(n1,vector(n2,0));
for(int i=0;i=0){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=1;
}
}
else{
int a=0,b=0;
if(i-1>=0){
a=dp[i-1][j];
}
if(j-1>=0){
b=dp[i][j-1];
}
dp[i][j]=max(a,b);
}
}
}
return dp[n1-1][n2-1];
//Write your code here
}
I did the same with java code
"UNDERSTOOD BHAIYA!!"
when the character was not matching why are we not considering the possiblity of f(index1-1,index2-1). Thanks for the help
Today, I solved my first ever dp problem in a contest just by watching this video...
Codechef Starter 71 Longest Common Subsequence problem
understood thank you Striver
almost all of the questions in this playlist can be space optimized to not just O(2m) but O(m) if you apply some logic and visualize how the table is being filled.
Striver, why can't we return INT_MIN , in the base case. Because if we return 0, it will be added to 1 and the resultant number will be considered for maximum. And even if it is not the maximum, it will be returned. Your explanation is very intuitive.
did it in 1st attempt without seeing the video, shifting index to right was new to me
hey man i know you are a very good teacher , but you are also a very good human being.
It would be helpful if you explain optimal structure of the problem with a concrete proof
Was able to solve this on Leetcode by myself. Thanks Striver !!!
public int longestCommonSubsequence(String s1, String s2) {
int n1 = s1.length(),n2 = s2.length();
int[] prev = new int[n2];
//base cases
if(s1.charAt(0)==s2.charAt(0)) prev[0]=1;
for(int i=1;i
Hi Raj the whole video was very explanative ,but the last space optimization part was not clear to me as why the prev and curr array has to be of size m and not n
Because as in Space Optimization we required only Columns not Rows....
n rows
m cols
So both the vectors were of size m i.e, Columns
Understood ❤
Dhonnobad Brother
You are great
Thanks for great explanation striver
Finally DP on strings Let's Go
you forgot to update the description box. 😊
Thanks for commenting, done!
Order update hua hai description sahi hai
Understood..! Awesome method "shifting of index" 😁......though without shifting i have solved tabulation, all credits to you only, but shifting of index makes that too easy. Love you ❤
best dp playlist on youtube
Why can't we put the base case at index 0? Especially for LCS - DP on strings related questions?
Just one word , Beautiful!
god!!! of teaching dsa
understood
Nice explanation raj bhaiyan or dude, I might call you angel!🥰🥰
You always have very good solutions, but your writing is hard for us to clearly get all information, I think we should write upper case for all letters, then it will be helpful for all. Thank you
Understood, Thank you so so much.
thanks for beautiful explanation
I’m just so thankful to you for this wonderful series, I’m honestly enjoying it dude, surely you’re one of the best!
I even did the problem by myself so easily, i was shocked believing😂
me too😂
Understand it very clearly>>
Understood sir very well!!
Striver what do u eat, how can be someone explain in such an excellenct manner❤
Once again thanks for this awesome content
I was always scared of these LCS and all but not anymore thanks to striver
GOAT for a reason❤❤
Thank You
Understood!!!
UNDERSTOOD...
Thanks striver for the video... :)
hey striver this is regarding ur tuf website......one feedback....can you add another checklist to mark important or tough nut questions so that for revision we can se it carefully again....because this subsequence is screewing up my brain :)
Thank you Striver for such a wonderful explanation. Finally understood it intuitively. 💯💯
You are a rockstar.. loved it
US Striver , Getting into Google i can image ur DSA level 🔥
Decode ways is a new kind of pattern on dp on strings i think and that can be covered
Thanks bro the Playlist is top notch...am getting a lot better...Thanks again
i myself did it 1array space optimisation ! quite impressive how i learnt that fast 😀😀
can you please share the code of 1 array optimisation , my 1 array code giving me wrong answer on leetcode testcase 23 , i saw a solution where someone uses 2 extra variables to do in 1 array. plz reply
Bhaiya ,why cant we solve this question like if we have got two strings then we are eliminating the characters from both strings that are uncommon and dont have frequency 1 (checking that in common characters)..ultimately we will get two resultant strings and if they are equal that is longest subsequence and if not there is no longest common subsequence present ..pls clear my doubt
Can we do using single array if we denote prev[j-1] in a variable, keep updating it after every iteration of inner loop ?