Dp 25. Longest Common Subsequence | Top Down | Bottom-Up | Space Optimised | DP on Strings

Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
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/takeUforward
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.org/interviews/s...
You can also get in touch with me at my social handles: linktr.ee/takeUforward

Пікірлер: 748

  • @sujalgupta6100
    @sujalgupta6100 Жыл бұрын

    46:05 for space optimization we don't require the loop for prev as the values are already zero in it.

  • @art4eigen93
    @art4eigen93 Жыл бұрын

    In the future, Mr. Vikramaditya will go down as the G.O.A.T online DSA teacher.

  • @wahtNIGGA

    @wahtNIGGA

    4 ай бұрын

    Yes he is GOAT I am the future

  • @abhishekkarn8918

    @abhishekkarn8918

    2 ай бұрын

    He is already for me.

  • @anweshabanerjee6241
    @anweshabanerjee6241 Жыл бұрын

    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😁

  • @manthenanagendra1077
    @manthenanagendra1077 Жыл бұрын

    putting your years of hardwork in some videos ,how lucky we are,Thanks a lot striver bhaiya for everything you are doing for us

  • @pratyakshsinha6292
    @pratyakshsinha6292 Жыл бұрын

    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.

  • @AdityaKumar-be7hx

    @AdityaKumar-be7hx

    Жыл бұрын

    Keep trying. It will work out. Dream for the day when you are so good that you REJECT your dream company.

  • @iamnoob7593

    @iamnoob7593

    5 ай бұрын

    @@AdityaKumar-be7hx Requires god level consistency to do that.

  • @idris110

    @idris110

    5 ай бұрын

    You will probably forget the concepts, since these are never used in the industry. What a satire !

  • @bharatravihal9646

    @bharatravihal9646

    5 ай бұрын

    🤣🤣@@idris110

  • @spoidy2025

    @spoidy2025

    3 ай бұрын

    brother are you placed

  • @arunkumarsahoo1344
    @arunkumarsahoo1344 Жыл бұрын

    15:34 "You kow i am hearing you" in the background

  • @jeet-smokey

    @jeet-smokey

    4 ай бұрын

    So?

  • @arjundevmishra7207
    @arjundevmishra720710 ай бұрын

    Protect this guy at all costs. Thank you sir for all your hard work in making this video.

  • @mayanksingh7501

    @mayanksingh7501

    Ай бұрын

    Everyone should be protected bro

  • @kartiksuman9814
    @kartiksuman98142 жыл бұрын

    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

  • @avimandavia6154
    @avimandavia6154 Жыл бұрын

    No other video on the topic will offer you a better explanation than this! This is just pure teaching excellence! Subscribed.

  • @donno2423
    @donno2423 Жыл бұрын

    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.

  • @Amitchoudhary-ij4we
    @Amitchoudhary-ij4we Жыл бұрын

    I am very grateful to you for uploading this playlist. This is great!!!!!!!!!!!!!!!!!!!!! Understood.

  • @ramanahlawat398
    @ramanahlawat398 Жыл бұрын

    bro!!!!!! What an explanation, absolutely brilliant. I am starting to love coding now. Thanks

  • @duyhuynh1234
    @duyhuynh123411 ай бұрын

    You're a great teacher. If possible, please do problems about DP on tree. I struggle with them. Thank you!

  • @yashsinghal3404
    @yashsinghal34042 жыл бұрын

    What an easy explanation, loved it !

  • @mahammedhashish9170
    @mahammedhashish9170 Жыл бұрын

    You can't find better explanation than this, Brilliant!!

  • @monojittewari6495
    @monojittewari6495 Жыл бұрын

    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.

  • @akashyadagouda896
    @akashyadagouda8965 ай бұрын

    Its taking while to digest this information for me , Just imagine the efforts this guy is adding to make it available for everyone.

  • @stith_pragya
    @stith_pragya5 ай бұрын

    UNDERSTOOD.....Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @nottheradbrad184
    @nottheradbrad18410 ай бұрын

    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❤

  • @BG-lj6fw
    @BG-lj6fw Жыл бұрын

    understood.wonderful. amazing. hats-off. unmatchable.

  • @ankita716
    @ankita7165 күн бұрын

    understood, always in awe of you genius and hard working you are:)

  • @pratapsingh9638
    @pratapsingh9638 Жыл бұрын

    Understood,you are doing the great job , very thankful to you

  • @cinime
    @cinime Жыл бұрын

    Understood! Awesome explanation as always, thank you very much!!!

  • @sameersahu4569
    @sameersahu4569 Жыл бұрын

    Loved the Explanation!!Understood

  • @ranasauravsingh
    @ranasauravsingh2 жыл бұрын

    UNDERSTOOD... Thanks striver for the video... :)

  • @dashsights4258
    @dashsights4258 Жыл бұрын

    Just one word , Beautiful!

  • @DevashishJose
    @DevashishJose5 ай бұрын

    Understood, Thank you so so much.

  • @ahsanulanam-4632
    @ahsanulanam-46326 ай бұрын

    Dhonnobad Brother You are great

  • @virgarg1012
    @virgarg101213 күн бұрын

    Giving my best shot for preparing for my placements. Let's see what happens in upcoming months. Great Content

  • @deepurana6010
    @deepurana6010 Жыл бұрын

    You are amazing ❤️. Thanks a lot for this valuable content 🙌🙇🙇

  • @shivamsinghnegi1192
    @shivamsinghnegi1192 Жыл бұрын

    Thanks bro the Playlist is top notch...am getting a lot better...Thanks again

  • @gyanunlimited740
    @gyanunlimited7402 жыл бұрын

    Man the content is gold.

  • @arnabroy2995
    @arnabroy2995 Жыл бұрын

    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❤

  • @Nikhil-ov6sm
    @Nikhil-ov6sm Жыл бұрын

    awesome, understood..was pretty easy due to what you've alrready taught

  • @ChutHunter
    @ChutHunter4 ай бұрын

    Thank you so much bhaiya, I always heard of this question as one of the most difficult ones. But by following your DP series, I did it on my own. You can easily sell this quality content for ₹ 50k but you're giving it for free. BEST TEACHER EVER ♥♥♥♥

  • @rishabhgupta9846
    @rishabhgupta9846 Жыл бұрын

    understood,previously I mugged up the tabulation formula for the exam ,but now I know how it came.

  • @ritikshandilya7075
    @ritikshandilya70756 күн бұрын

    Thanks for great explanation striver

  • @himanshuagrawal8012
    @himanshuagrawal80122 жыл бұрын

    Can we do using single array if we denote prev[j-1] in a variable, keep updating it after every iteration of inner loop ?

  • @alokbhowmik3547
    @alokbhowmik35472 жыл бұрын

    what an explanation thanks for your time and such video

  • @daniel.w8112
    @daniel.w8112 Жыл бұрын

    Thank You Very much Striver !

  • @m4n0jkum4r
    @m4n0jkum4r Жыл бұрын

    In Tabulation we are declaring vector with values zero. So we can skip first two loops.

  • @sanyazaveri4511
    @sanyazaveri4511 Жыл бұрын

    Awesome lecture. Understood!

  • @SethuIyer95
    @SethuIyer953 ай бұрын

    I deeply understand recursion, memoization, tabulation and space optimization.

  • @varunsharma1889
    @varunsharma18892 жыл бұрын

    Very good explanation. Understood. Thanks.

  • @user-oi6eb7ru2s
    @user-oi6eb7ru2s7 ай бұрын

    Thanks a lot striver

  • @jaideeppyne1880
    @jaideeppyne1880 Жыл бұрын

    very well explained step by step

  • @oqant0424
    @oqant0424 Жыл бұрын

    seriously you are a legit! understood ,thanks🙌

  • @kareni7572
    @kareni7572 Жыл бұрын

    Why can't we put the base case at index 0? Especially for LCS - DP on strings related questions?

  • @sarveshamrute4982
    @sarveshamrute49822 жыл бұрын

    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

  • @saddy4420

    @saddy4420

    Жыл бұрын

    same

  • @pj-nz6nm
    @pj-nz6nm Жыл бұрын

    hey man i know you are a very good teacher , but you are also a very good human being.

  • @ganeshkamath89
    @ganeshkamath892 жыл бұрын

    Thanks Striver. Understood LCS.

  • @rakshitpandey7517
    @rakshitpandey75172 жыл бұрын

    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 }

  • @NarenderMajoka

    @NarenderMajoka

    2 жыл бұрын

    I did the same with java code

  • @jskr456
    @jskr4566 ай бұрын

    very good explanation

  • @sypher4735
    @sypher4735 Жыл бұрын

    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😂

  • @ishitasrivastava8417

    @ishitasrivastava8417

    Жыл бұрын

    me too😂

  • @abdallaalhag4425
    @abdallaalhag44256 ай бұрын

    Understood boss man!

  • @user-ke7fs7ds6h
    @user-ke7fs7ds6h6 ай бұрын

    GOAT for a reason❤❤

  • @avikdey01
    @avikdey012 жыл бұрын

    In interviews do we need to show the overlapping subproblems by taking longer examples or we can simply move on with memoization?

  • @coding8000

    @coding8000

    2 жыл бұрын

    take till 5 to 6 steps., if recruiter does want , want he will tell u., to move to next step.

  • @jarvis3551
    @jarvis35516 ай бұрын

    did it in 1st attempt without seeing the video, shifting index to right was new to me

  • @tejasghone5118
    @tejasghone51182 жыл бұрын

    Can also be done using single array if we preserve cur[j-1] in a prv variable

  • @takeUforward

    @takeUforward

    2 жыл бұрын

    Yeah.. you guys are learning fast 😍

  • @tejasghone5118

    @tejasghone5118

    2 жыл бұрын

    @@takeUforward thanks to your playlist💯

  • @jaykumargupta7307

    @jaykumargupta7307

    2 жыл бұрын

    @@tejasghone5118 can u provide the code

  • @himanshuguleria9474

    @himanshuguleria9474

    2 жыл бұрын

    @@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

  • @ishwarshelke128

    @ishwarshelke128

    2 жыл бұрын

    can u give the video link in which he taught this space opt technique

  • @harisai3580
    @harisai35805 ай бұрын

    Understood Sir!

  • @yashchawla3729
    @yashchawla37292 жыл бұрын

    Understood, sir. Thank you very much.

  • @priyanshuchouhan9845
    @priyanshuchouhan98459 ай бұрын

    who agrees that this dp series is one of the best on KZread

  • @tasneemayham974

    @tasneemayham974

    9 ай бұрын

    Not one of the best no. THE BESTTTTT!!!!!

  • @santoshb7776
    @santoshb77767 ай бұрын

    Understood sir ! 🙏🙏

  • @varunbansal1136
    @varunbansal11368 ай бұрын

    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.

  • @tibebuwassie6017
    @tibebuwassie6017 Жыл бұрын

    hey great video but isn't the auxiliary stack space supposed to be max(s.size(), t.size())?

  • @rpriyanka28
    @rpriyanka282 ай бұрын

    he is the example of beauty with brains

  • @priyanshuchouhan9845
    @priyanshuchouhan98459 ай бұрын

    one of the best dp series

  • @vedxnt10
    @vedxnt10Ай бұрын

    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

  • @rohan8758
    @rohan875813 күн бұрын

    Nice explanation raj bhaiyan or dude, I might call you angel!🥰🥰

  • @UECAshutoshKumar
    @UECAshutoshKumar7 күн бұрын

    Thank You Understood!!!

  • @CodeMode9313
    @CodeMode93138 ай бұрын

    awesome paaji

  • @jashhinger442
    @jashhinger442 Жыл бұрын

    Love the concept ✨

  • @harshitatripathi6440
    @harshitatripathi644011 ай бұрын

    best dp playlist on youtube

  • @prabhakaran5542
    @prabhakaran55423 ай бұрын

    Understood ❤

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz5 ай бұрын

    god!!! of teaching dsa understood

  • @raj_kundalia
    @raj_kundalia Жыл бұрын

    thank you so much!

  • @meetharsoda5152
    @meetharsoda515220 күн бұрын

    Finally DP on strings Let's Go

  • @lakshmanpadigala1605
    @lakshmanpadigala1605 Жыл бұрын

    Great explanation!

  • @thiyaneswarana3656
    @thiyaneswarana3656 Жыл бұрын

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

  • @codewithpranjay9618
    @codewithpranjay96182 жыл бұрын

    understood well !

  • @coding8000
    @coding8000 Жыл бұрын

    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 !!!

  • @coderrr3353
    @coderrr3353 Жыл бұрын

    This man is saviour. Thanks bhaiya ❤️

  • @amartyadas5-yriddmechanica597
    @amartyadas5-yriddmechanica5972 жыл бұрын

    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.

  • @takeUforward

    @takeUforward

    2 жыл бұрын

    m+n

  • @AbdulKadir-bh3el
    @AbdulKadir-bh3el2 жыл бұрын

    understood bruh, plz make video on sliding window technique too.

  • @Parmindersingh-of6oo
    @Parmindersingh-of6oo Жыл бұрын

    We don't need to add base case while bottom-up in case of index shifting. If we just replace "-1" with "0" while initializing dp vector. Isn't it?

  • @HarshSharma-ff3ox
    @HarshSharma-ff3ox Жыл бұрын

    Thank you Striver for such a wonderful explanation. Finally understood it intuitively. 💯💯

  • @lambar0
    @lambar0 Жыл бұрын

    Clear and Simple

  • @rahulkakkar7241
    @rahulkakkar72412 жыл бұрын

    Explained Like a Dopamine Hit!

  • @girishbhargava6367
    @girishbhargava63672 жыл бұрын

    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.

  • @md.rashedulislam5141
    @md.rashedulislam51412 жыл бұрын

    Is it possible to represent every dp problem by recursion tree?

  • @dnavas7719
    @dnavas77192 жыл бұрын

    beautifully explained thanks a lot!!

  • @ScienceSeekho
    @ScienceSeekho Жыл бұрын

    Thanks learned new thing index shifting

  • @zodiacsama7693
    @zodiacsama7693 Жыл бұрын

    how we computed the time complexity of recursive solution?

  • @himanshugoyal7941
    @himanshugoyal7941 Жыл бұрын

    Can anyone please explain why (index1 == 0 || index2 == 0) has not been considered as base case here as we did in DP of arrays? In DP of Arrays, I was trying to use (index Can someone please explain why?

  • @sanyattaneja8551

    @sanyattaneja8551

    Жыл бұрын

    You can use index1 == 0 || index2 == 0 but then it will have 2 cases 1) if(index1 == 0 && index2 == 0) return s1[index1] == s2[index2] ; 2) if(index1 == 0 || index2 == 0) \\ using loop find the single character(having index == 0) in other string if present return 1 else return 0 therefore it is difficult to find and index < 0 base case is much easier so it's better to write that

  • @Sumeet_100
    @Sumeet_10018 күн бұрын

    Understood !!

  • @saurabhrthakur
    @saurabhrthakur2 ай бұрын

    Understood!

  • @thanhbinhnguyen5943
    @thanhbinhnguyen5943 Жыл бұрын

    nice explaination !

  • @shashijaiswal5014
    @shashijaiswal5014 Жыл бұрын

    GREAT AND INSIGHTFUL VIDEO

  • @vishious14
    @vishious146 ай бұрын

    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

  • @rahulsaha332
    @rahulsaha332 Жыл бұрын

    Decode ways is a new kind of pattern on dp on strings i think and that can be covered