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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024

ความคิดเห็น • 873

  • @pratyakshsinha6292
    @pratyakshsinha6292 2 ปีที่แล้ว +199

    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 ปีที่แล้ว +22

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

    • @iamnoob7593
      @iamnoob7593 10 หลายเดือนก่อน +1

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

    • @idris110
      @idris110 10 หลายเดือนก่อน +5

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

    • @bharatravihal9646
      @bharatravihal9646 10 หลายเดือนก่อน

      🤣🤣@@idris110

    • @spoidy2025
      @spoidy2025 8 หลายเดือนก่อน

      brother are you placed

  • @anweshabanerjee6241
    @anweshabanerjee6241 2 ปีที่แล้ว +86

    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😁

  • @art4eigen93
    @art4eigen93 2 ปีที่แล้ว +209

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

    • @wahtNIGGA
      @wahtNIGGA 10 หลายเดือนก่อน +2

      Yes he is GOAT
      I am the future

    • @abhishekkarn8918
      @abhishekkarn8918 7 หลายเดือนก่อน +2

      He is already for me.

    • @v7gamerff809
      @v7gamerff809 3 หลายเดือนก่อน

      No a Guy named Srikar From Vizag will go down as the GOAT online DSA in future

    • @madhu_mohanreddy
      @madhu_mohanreddy 2 หลายเดือนก่อน

      @@v7gamerff809as usual, just a dumb guy from ap.

    • @Alex-ue9du
      @Alex-ue9du 16 วันที่ผ่านมา

      True

  • @arjundevmishra7207
    @arjundevmishra7207 ปีที่แล้ว +65

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

    • @mayanksingh7501
      @mayanksingh7501 6 หลายเดือนก่อน +3

      Everyone should be protected bro

  • @manthenanagendra1077
    @manthenanagendra1077 2 ปีที่แล้ว +133

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

  • @donno2423
    @donno2423 ปีที่แล้ว +14

    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.

  • @avimandavia6154
    @avimandavia6154 ปีที่แล้ว +6

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

  • @kartiksuman9814
    @kartiksuman9814 2 ปีที่แล้ว +58

    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

  • @duyhuynh1234
    @duyhuynh1234 ปีที่แล้ว +8

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

  • @sujalgupta6100
    @sujalgupta6100 2 ปีที่แล้ว +76

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

    • @dikshamakkar2850
      @dikshamakkar2850 4 หลายเดือนก่อน

      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.

    • @vizzz8906
      @vizzz8906 3 หลายเดือนก่อน

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

    • @madhu_mohanreddy
      @madhu_mohanreddy 2 หลายเดือนก่อน

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

    • @gomzysharma
      @gomzysharma 2 หลายเดือนก่อน +2

      you're right but that's write for clarity purpose that how we are generating tabulation from already written memorization solution

    • @himanshukandwal8710
      @himanshukandwal8710 2 หลายเดือนก่อน +1

      @@vizzz8906 he forgot to remove this loop.

  • @Amitchoudhary-ij4we
    @Amitchoudhary-ij4we 2 ปีที่แล้ว +14

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

  • @kanikajain5627
    @kanikajain5627 4 หลายเดือนก่อน

    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.

  • @arunkumarsahoo1344
    @arunkumarsahoo1344 2 ปีที่แล้ว +27

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

    • @jeet-smokey
      @jeet-smokey 9 หลายเดือนก่อน +4

      So?

  • @sahitid5570
    @sahitid5570 17 วันที่ผ่านมา

    Understood!! I don't think any other explanation video can match this one. You are God sent!!

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

  • @varunbansal1136
    @varunbansal1136 ปีที่แล้ว +5

    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.

  • @arnabroy2995
    @arnabroy2995 ปีที่แล้ว +4

    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❤

  • @ramanahlawat398
    @ramanahlawat398 ปีที่แล้ว +4

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

  • @akashyadagouda896
    @akashyadagouda896 10 หลายเดือนก่อน +1

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

  • @siddharthsarraf330
    @siddharthsarraf330 2 ปีที่แล้ว +7

    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

  • @shuvbhowmickbestin
    @shuvbhowmickbestin หลายเดือนก่อน

    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.

  • @mrcamara5574
    @mrcamara5574 หลายเดือนก่อน

    Honestly, everytime i listen to you dude i feel like i can handle anything ! much love from Paris

  • @therangeplus
    @therangeplus 5 วันที่ผ่านมา

    best explanation I have received ever. U are the best. Im watch this whole playlist to prep for my interviews

  • @deadlockgaming8553
    @deadlockgaming8553 3 หลายเดือนก่อน +1

    you are a god level teacher🙇‍♂🙇‍♂🙇‍♂
    never thought dp would be so easy

  • @virgarg1012
    @virgarg1012 5 หลายเดือนก่อน +1

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

  • @stith_pragya
    @stith_pragya 10 หลายเดือนก่อน +1

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

  • @ankita716
    @ankita716 5 หลายเดือนก่อน +1

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

  • @nottheradbrad184
    @nottheradbrad184 ปีที่แล้ว

    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❤

  • @vedxnt10
    @vedxnt10 6 หลายเดือนก่อน

    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

  • @mahammedhashish9170
    @mahammedhashish9170 ปีที่แล้ว +1

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

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

  • @yashsinghal3404
    @yashsinghal3404 2 ปีที่แล้ว +5

    What an easy explanation, loved it !

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

  • @dharanyuvi6951
    @dharanyuvi6951 4 หลายเดือนก่อน

    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.

  • @vamsikrishnareddy9345
    @vamsikrishnareddy9345 4 หลายเดือนก่อน

    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

  • @sypher4735
    @sypher4735 2 ปีที่แล้ว +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😂

  • @VishalYadav-gk1kg
    @VishalYadav-gk1kg 4 หลายเดือนก่อน +1

    Very nice explanation sir, Thank you!

  • @vaibhavrawat4008
    @vaibhavrawat4008 2 หลายเดือนก่อน

    most in depth explanation, thanks

  • @vinaylj
    @vinaylj 4 หลายเดือนก่อน

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

  • @rpriyanka28
    @rpriyanka28 7 หลายเดือนก่อน +1

    he is the example of beauty with brains

  • @pj-nz6nm
    @pj-nz6nm ปีที่แล้ว

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

  • @priyanshuchouhan9845
    @priyanshuchouhan9845 ปีที่แล้ว +4

    who agrees that this dp series is one of the best on TH-cam

    • @tasneemayham974
      @tasneemayham974 ปีที่แล้ว

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

  • @gyanunlimited740
    @gyanunlimited740 2 ปีที่แล้ว +5

    Man the content is gold.

  • @mohammedraiyaan-bt6iy
    @mohammedraiyaan-bt6iy 2 หลายเดือนก่อน

    Amazing lecture sir ❤❤❤.
    Thanks for all your efforts for us.

  • @raghavmanish24
    @raghavmanish24 3 หลายเดือนก่อน

    understood .....thanku striver .....let's finish this dp on string topic

  • @rakshitpandey7517
    @rakshitpandey7517 2 ปีที่แล้ว +3

    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

  • @NiharIngale
    @NiharIngale 3 หลายเดือนก่อน

    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.

  • @hashcodez757
    @hashcodez757 3 หลายเดือนก่อน

    "UNDERSTOOD BHAIYA!!"

  • @tejasghone5118
    @tejasghone5118 2 ปีที่แล้ว +8

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

    • @takeUforward
      @takeUforward  2 ปีที่แล้ว +13

      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 ปีที่แล้ว +4

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

  • @neilbohr6015
    @neilbohr6015 หลายเดือนก่อน

    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

  • @BG-lj6fw
    @BG-lj6fw ปีที่แล้ว

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

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

  • @adarshanku7988
    @adarshanku7988 2 ปีที่แล้ว +2

    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 ❤

  • @HarshSharma-ff3ox
    @HarshSharma-ff3ox 2 ปีที่แล้ว +5

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

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater 4 หลายเดือนก่อน +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!!

  • @ntgrn-pr5yx
    @ntgrn-pr5yx หลายเดือนก่อน

    understood thank you Striver

  • @rohan8758
    @rohan8758 5 หลายเดือนก่อน +1

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

  • @Anmol-h8z
    @Anmol-h8z 11 วันที่ผ่านมา +1

    Understood very well

  • @m4n0jkum4r
    @m4n0jkum4r 2 ปีที่แล้ว +1

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

  • @SethuIyer95
    @SethuIyer95 8 หลายเดือนก่อน

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

  • @Harshanandita
    @Harshanandita ปีที่แล้ว +1

    Today, I solved my first ever dp problem in a contest just by watching this video...
    Codechef Starter 71 Longest Common Subsequence problem

  • @rishabhgupta9846
    @rishabhgupta9846 ปีที่แล้ว

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

  • @ranasauravsingh
    @ranasauravsingh 2 ปีที่แล้ว +2

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

  • @vishious14
    @vishious14 11 หลายเดือนก่อน

    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

  • @shivamsinghnegi1192
    @shivamsinghnegi1192 ปีที่แล้ว

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

  • @ahsanulanam-4632
    @ahsanulanam-4632 11 หลายเดือนก่อน

    Dhonnobad Brother
    You are great

  • @meetharsoda5152
    @meetharsoda5152 5 หลายเดือนก่อน +1

    Finally DP on strings Let's Go

  • @shauryatomer1058
    @shauryatomer1058 หลายเดือนก่อน

    Once again thanks for this awesome content

  • @ritikshandilya7075
    @ritikshandilya7075 5 หลายเดือนก่อน

    Thanks for great explanation striver

  • @harshitatripathi6440
    @harshitatripathi6440 ปีที่แล้ว

    best dp playlist on youtube

  • @dashsights4258
    @dashsights4258 ปีที่แล้ว

    Just one word , Beautiful!

  • @prabhakaran5542
    @prabhakaran5542 8 หลายเดือนก่อน +1

    Understood ❤

  • @ABDULRAHMAN-hr3ko
    @ABDULRAHMAN-hr3ko 3 หลายเดือนก่อน

    thanks for beautiful explanation

  • @NaveenPGForYou
    @NaveenPGForYou หลายเดือนก่อน

    You are a rockstar.. loved it

  • @honeykumar5700
    @honeykumar5700 2 ปีที่แล้ว +1

    i myself did it 1array space optimisation ! quite impressive how i learnt that fast 😀😀

    • @justarandomguy6106
      @justarandomguy6106 ปีที่แล้ว

      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

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

  • @cinime
    @cinime 2 ปีที่แล้ว +1

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

  • @biswajitadhikary_0807
    @biswajitadhikary_0807 3 หลายเดือนก่อน

    Understand it very clearly>>

  • @jitendrasinghsola
    @jitendrasinghsola 2 ปีที่แล้ว +3

    I was always scared of these LCS and all but not anymore thanks to striver

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz 10 หลายเดือนก่อน

    god!!! of teaching dsa
    understood

  • @vijaybhaskar1413
    @vijaybhaskar1413 ปีที่แล้ว

    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

  • @harihardhik3293
    @harihardhik3293 3 หลายเดือนก่อน

    Understood sir very well!!

  • @parthsalat
    @parthsalat 2 ปีที่แล้ว +1

    Continue from 23:30

  • @pratapsingh9638
    @pratapsingh9638 2 ปีที่แล้ว

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

  • @jarvis3551
    @jarvis3551 11 หลายเดือนก่อน

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

  • @charan775
    @charan775 2 หลายเดือนก่อน

    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

    • @namanpaliwal4858
      @namanpaliwal4858 หลายเดือนก่อน

      I didn't get it could you please explain a lil bit.

  • @striverdaaadi
    @striverdaaadi 11 หลายเดือนก่อน

    GOAT for a reason❤❤

  • @HIMANSHU-u6y
    @HIMANSHU-u6y 3 หลายเดือนก่อน

    Striver what do u eat, how can be someone explain in such an excellenct manner❤

  • @Nikhil-ov6sm
    @Nikhil-ov6sm 2 ปีที่แล้ว

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

  • @DevashishJose
    @DevashishJose 10 หลายเดือนก่อน

    Understood, Thank you so so much.

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

  • @bhuoneparmar5098
    @bhuoneparmar5098 ปีที่แล้ว +1

    understood
    sir i don't even think of dp those days but now i am doing dp questions without any hint or watching videos!!!!
    Thanks a lot striver bhai!! :)

  • @coderrr3353
    @coderrr3353 ปีที่แล้ว +1

    This man is saviour. Thanks bhaiya ❤️

  • @iamnoob7593
    @iamnoob7593 10 หลายเดือนก่อน

    US Striver , Getting into Google i can image ur DSA level 🔥

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

  • @priyanshuchouhan9845
    @priyanshuchouhan9845 ปีที่แล้ว

    one of the best dp series

  • @deepurana6010
    @deepurana6010 ปีที่แล้ว +1

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

  • @abhinavgoli868
    @abhinavgoli868 4 หลายเดือนก่อน +1

    when the character was not matching why are we not considering the possiblity of f(index1-1,index2-1). Thanks for the help

  • @daniel.w8112
    @daniel.w8112 2 ปีที่แล้ว

    Thank You Very much Striver !

  • @yeswanthh5068
    @yeswanthh5068 2 ปีที่แล้ว +4

    Understood 🙂🙂💚