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

Поделиться
HTML-код
  • Опубликовано: 28 фев 2022
  • 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

Комментарии • 790

  • @art4eigen93
    @art4eigen93 Год назад +168

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

    • @wahtNIGGA
      @wahtNIGGA 6 месяцев назад

      Yes he is GOAT
      I am the future

    • @abhishekkarn8918
      @abhishekkarn8918 3 месяца назад +2

      He is already for me.

  • @pratyakshsinha6292
    @pratyakshsinha6292 Год назад +147

    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 Год назад +19

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

    • @iamnoob7593
      @iamnoob7593 6 месяцев назад

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

    • @idris110
      @idris110 6 месяцев назад +1

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

    • @bharatravihal9646
      @bharatravihal9646 6 месяцев назад

      🤣🤣@@idris110

    • @spoidy2025
      @spoidy2025 4 месяца назад

      brother are you placed

  • @anweshabanerjee6241
    @anweshabanerjee6241 Год назад +73

    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 Год назад +122

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

  • @sujalgupta6100
    @sujalgupta6100 Год назад +74

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

    • @dikshamakkar2850
      @dikshamakkar2850 5 дней назад

      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.

  • @arjundevmishra7207
    @arjundevmishra7207 11 месяцев назад +48

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

  • @avimandavia6154
    @avimandavia6154 Год назад +5

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

  • @Amitchoudhary-ij4we
    @Amitchoudhary-ij4we Год назад +13

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

  • @duyhuynh1234
    @duyhuynh1234 Год назад +5

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

  • @kartiksuman9814
    @kartiksuman9814 2 года назад +52

    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

  • @kanikajain5627
    @kanikajain5627 20 дней назад

    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.

  • @ramanahlawat398
    @ramanahlawat398 Год назад +4

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

  • @yashsinghal3404
    @yashsinghal3404 2 года назад +5

    What an easy explanation, loved it !

  • @mjmusic65
    @mjmusic65 Год назад +3

    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
    @akashyadagouda896 6 месяцев назад +1

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

  • @mahammedhashish9170
    @mahammedhashish9170 Год назад +1

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

  • @nottheradbrad184
    @nottheradbrad184 11 месяцев назад

    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❤

  • @ankita716
    @ankita716 Месяц назад +1

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

  • @donno2423
    @donno2423 Год назад +10

    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.

  • @stith_pragya
    @stith_pragya 6 месяцев назад +1

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

  • @arunkumarsahoo1344
    @arunkumarsahoo1344 2 года назад +20

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

  • @sypher4735
    @sypher4735 Год назад +6

    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😂

  • @cinime
    @cinime 2 года назад +1

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

  • @BG-lj6fw
    @BG-lj6fw Год назад

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

  • @pratapsingh9638
    @pratapsingh9638 Год назад

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

  • @deepurana6010
    @deepurana6010 Год назад +1

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

  • @virgarg1012
    @virgarg1012 Месяц назад +1

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

  • @arnabroy2995
    @arnabroy2995 Год назад +3

    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❤

  • @VishalYadav-gk1kg
    @VishalYadav-gk1kg 16 дней назад +1

    Very nice explanation sir, Thank you!

  • @shivamsinghnegi1192
    @shivamsinghnegi1192 Год назад

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

  • @sameersahu4569
    @sameersahu4569 2 года назад

    Loved the Explanation!!Understood

  • @ranasauravsingh
    @ranasauravsingh 2 года назад +2

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

  • @Nikhil-ov6sm
    @Nikhil-ov6sm Год назад

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

  • @ChutHunter
    @ChutHunter 5 месяцев назад

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

  • @DevashishJose
    @DevashishJose 6 месяцев назад

    Understood, Thank you so so much.

  • @vinaylj
    @vinaylj 22 дня назад

    I was able to see that curr= prev; mistake as you were typing it. thanks to you Striver. you are the best

  • @himanshuagrawal8012
    @himanshuagrawal8012 2 года назад +3

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

  • @pj-nz6nm
    @pj-nz6nm Год назад

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

  • @dashsights4258
    @dashsights4258 Год назад

    Just one word , Beautiful!

  • @gyanunlimited740
    @gyanunlimited740 2 года назад +5

    Man the content is gold.

  • @oqant0424
    @oqant0424 Год назад

    seriously you are a legit!
    understood ,thanks🙌

  • @ritikshandilya7075
    @ritikshandilya7075 Месяц назад

    Thanks for great explanation striver

  • @alokbhowmik3547
    @alokbhowmik3547 2 года назад +1

    what an explanation thanks for your time and such video

  • @vedxnt10
    @vedxnt10 2 месяца назад

    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

  • @sarveshamrute4982
    @sarveshamrute4982 2 года назад +7

    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

  • @rohan8758
    @rohan8758 Месяц назад +1

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

  • @varunsharma1889
    @varunsharma1889 2 года назад

    Very good explanation. Understood. Thanks.

  • @ahsanulanam-4632
    @ahsanulanam-4632 7 месяцев назад

    Dhonnobad Brother
    You are great

  • @daniel.w8112
    @daniel.w8112 Год назад

    Thank You Very much Striver !

  • @m4n0jkum4r
    @m4n0jkum4r Год назад +1

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

  • @user-oi6eb7ru2s
    @user-oi6eb7ru2s 8 месяцев назад

    Thanks a lot striver

  • @sanyazaveri4511
    @sanyazaveri4511 Год назад

    Awesome lecture. Understood!

  • @kareni7572
    @kareni7572 Год назад +2

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

  • @rishabhgupta9846
    @rishabhgupta9846 Год назад

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

  • @jaideeppyne1880
    @jaideeppyne1880 Год назад

    very well explained step by step

  • @tibebuwassie6017
    @tibebuwassie6017 Год назад +1

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

  • @abdallaalhag4425
    @abdallaalhag4425 7 месяцев назад

    Understood boss man!

  • @vamsikrishnareddy9345
    @vamsikrishnareddy9345 8 дней назад

    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

  • @amartyadas5-yriddmechanica597
    @amartyadas5-yriddmechanica597 2 года назад +1

    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.

  • @jskr456
    @jskr456 7 месяцев назад

    very good explanation

  • @HarshSharma-ff3ox
    @HarshSharma-ff3ox 2 года назад +5

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

  • @jashhinger442
    @jashhinger442 Год назад

    Love the concept ✨

  • @yashchawla3729
    @yashchawla3729 2 года назад

    Understood, sir. Thank you very much.

  • @ganeshkamath89
    @ganeshkamath89 2 года назад

    Thanks Striver. Understood LCS.

  • @SethuIyer95
    @SethuIyer95 4 месяца назад

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

  • @user-ke7fs7ds6h
    @user-ke7fs7ds6h 7 месяцев назад

    GOAT for a reason❤❤

  • @CodeMode9313
    @CodeMode9313 10 месяцев назад

    awesome paaji

  • @AbdulKadir-bh3el
    @AbdulKadir-bh3el 2 года назад

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

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater 12 дней назад +1

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

  • @thiyaneswarana3656
    @thiyaneswarana3656 Год назад +2

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

  • @lakshmanpadigala1605
    @lakshmanpadigala1605 Год назад

    Great explanation!

  • @harisai3580
    @harisai3580 6 месяцев назад

    Understood Sir!

  • @rahulkakkar7241
    @rahulkakkar7241 2 года назад

    Explained Like a Dopamine Hit!

  • @santoshb7776
    @santoshb7776 8 месяцев назад

    Understood sir ! 🙏🙏

  • @girishbhargava6367
    @girishbhargava6367 2 года назад

    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.

  • @dharanyuvi6951
    @dharanyuvi6951 6 дней назад

    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.

  • @raj_kundalia
    @raj_kundalia Год назад

    thank you so much!

  • @coding8000
    @coding8000 Год назад +1

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

  • @varunbansal1136
    @varunbansal1136 9 месяцев назад +1

    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.

  • @SurajPrasad-bf9qn
    @SurajPrasad-bf9qn 25 дней назад

    you are too awesome bro

  • @priyanshuchouhan9845
    @priyanshuchouhan9845 10 месяцев назад

    one of the best dp series

  • @Jagz1200
    @Jagz1200 Год назад

    Love you bro :) you are saviour!

  • @coderrr3353
    @coderrr3353 Год назад +1

    This man is saviour. Thanks bhaiya ❤️

  • @theresilientpianist7114
    @theresilientpianist7114 24 дня назад

    Understood❤❤🔥🔥

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

  • @jarvis3551
    @jarvis3551 7 месяцев назад

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

  • @rahulsaha332
    @rahulsaha332 Год назад +2

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

  • @avikdey01
    @avikdey01 2 года назад +6

    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 года назад +3

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

  • @shineinusa
    @shineinusa 2 года назад

    love this!

  • @dnavas7719
    @dnavas7719 2 года назад +1

    beautifully explained thanks a lot!!

  • @prabhakaran5542
    @prabhakaran5542 4 месяца назад +1

    Understood ❤

  • @raZer.7_7
    @raZer.7_7 13 дней назад

    UNDERSTOOD!

  • @thanhbinhnguyen5943
    @thanhbinhnguyen5943 Год назад +1

    nice explaination !

  • @codewithpranjay9618
    @codewithpranjay9618 2 года назад

    understood well !

  • @prashanjeetkumar1999
    @prashanjeetkumar1999 18 дней назад

    understood !!

  • @himanshugoyal7941
    @himanshugoyal7941 Год назад +1

    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 < 0) as the base case, but when I saw Striver's video, I changed my approach to his, where he was considering (index == 0 ) as the base case. But now, in this, he is using the other way around.
    Can someone please explain why?

    • @sanyattaneja8551
      @sanyattaneja8551 Год назад +1

      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

  • @saurabhrthakur
    @saurabhrthakur 3 месяца назад

    Understood!

  • @shababrabbani8085
    @shababrabbani8085 2 года назад

    You are a legend!

  • @suneelpadala1964
    @suneelpadala1964 2 года назад +1

    Excellent explanation bro

  • @harshitatripathi6440
    @harshitatripathi6440 Год назад

    best dp playlist on youtube