DP 31. Shortest Common Supersequence | DP on Strings

Поділитися
Вставка
  • Опубліковано 5 бер 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/3vEYKce
    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 Longest Common Supersequence, please watch LCS video before solving this problem.
    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

КОМЕНТАРІ • 381

  • @zaidshaikh2536
    @zaidshaikh2536 2 роки тому +128

    A lot of String DP problems simply revolve around LCS, Edit Distance. This problem as well is a pretty good follow-up of LCS where one would build up finding the LCS & then skip the Characters in LCS & pick the remaining ones from both strings.

  • @NarenderMajoka
    @NarenderMajoka 2 роки тому +31

    Thanks Striver for whole DP playlist, I learned alot from this and I did this problem on my own, just because of you. A big THANKS!

  • @_archit_sahay
    @_archit_sahay Рік тому +7

    Almost did it Myself. Really helpful playlist cuz this is the closest I have gotten to solving a hard question myself

  • @dipaligangawane980
    @dipaligangawane980 2 роки тому +7

    Thank you so much striver for wonderful series. Understood every problem present in the series

  • @ishangujarathi10
    @ishangujarathi10 Рік тому +1

    Understood!!! another LC HARD problem, but your explanation and previous concepts makes it looks so simple!!! AMAZINGGG!!!LOVEDD IT

  • @chandrachurmukherjeejucse5816

    Understood. Great content striver. Keep it up. Thanks for all your efforts to uplift the community.

  • @Harshit126
    @Harshit126 2 роки тому +5

    Understood, thanks. Both of the vides helped a lot to understand string extraction from DP matrix via tabulation :)

  • @manitsaharan5995
    @manitsaharan5995 Рік тому +12

    thank you so much i can,t even imagine i am solving hard dp problems of leetcode so easily so much understandable better than any paid course which i took of coding ninjas and coding blocks

  • @sudiptvyas4536
    @sudiptvyas4536 10 місяців тому

    Understood! You explained it so well! Thank you so much for making these videos

  • @securelyinsaycure
    @securelyinsaycure 5 місяців тому +1

    you are golden! thanks to you(for explaining such complex problems with ease) hearts won't be broken anymore

  • @stith_pragya
    @stith_pragya 6 місяців тому +4

    UNDERSTOOD..........Thanks a ton Striver Bhaiya for this wonderful series.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @CEOplays
    @CEOplays 2 роки тому +1

    Thank you so much STRIVER for these amazing videos!!🙌🙌❤❤

  • @AlokLaha-te2pd
    @AlokLaha-te2pd 16 днів тому

    Understood. You made it simple to understand how to pick uncommon elements into our result string.

  • @jyothiyadav2595
    @jyothiyadav2595 8 місяців тому

    Thank you sooo much striver , this playlist is just amazing, mind-blowing, just like a woow

  • @Sandeep-zd6dq
    @Sandeep-zd6dq 2 роки тому +10

    Bhai aaj ka weekly kidhar hai 😂 I was waiting ki ab aayega ab aayega aur tumne DP ki video daal di, bdhiya compensate kr diya 👍😂🔥

  • @cinime
    @cinime 2 роки тому

    Understood! Thank you so so SO much as always!!

  • @pranjalthapliyal2278
    @pranjalthapliyal2278 Рік тому +10

    Understood :)
    Prolly the toughest problem so far. Did it on my own in some other way, feeling v v satisfied

  • @deepakbhallavi1612
    @deepakbhallavi1612 Рік тому

    Thank you for the playlist ❤

  • @ranasauravsingh
    @ranasauravsingh 2 роки тому

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

  • @Shubh_____
    @Shubh_____ 26 днів тому

    thanks sir !! cant express my gratitude in word.

  • @pabitrakb5291
    @pabitrakb5291 Рік тому

    Just dooper explanation
    Understood sir!!! ❤️
    Thank you sooooooo much for your striving efforts

  • @aryanyadav3926
    @aryanyadav3926 2 роки тому +1

    Wonderful explanation!

  • @UECAshutoshKumar
    @UECAshutoshKumar Місяць тому +1

    Thank you
    Understood!!!

  • @chinu_.16
    @chinu_.16 Рік тому

    Understood....It took me so long but I solved this without watching video and I'm so happy ....thank you striver you are the best

  • @priyanshkumariitd
    @priyanshkumariitd 24 дні тому

    Great explanation bhaiya!! Understood this concept of printing the shortest supersequence using LCS DP table clearly...Thanks. This was indeed a difficult one..

  • @ishaankaustav727
    @ishaankaustav727 2 роки тому

    loved the way u explained 💚💚💚💚

  • @_Kunal_Pawar
    @_Kunal_Pawar 5 місяців тому

    Understood! I can't thank you enough

  • @sukritiguin5637
    @sukritiguin5637 6 місяців тому

    Amazing Explanation ❤❤

  • @vatsalvasoya5243
    @vatsalvasoya5243 Рік тому

    Understood!! Wonderful explanation!!!

  • @DevashishJose
    @DevashishJose 6 місяців тому

    Thank you so much, Understood.

  • @navneetdubey6918
    @navneetdubey6918 9 місяців тому

    Great explanation underStand Striver

  • @sanyamwalia217
    @sanyamwalia217 11 місяців тому +5

    Instead of using LCS, i tried to directly fill in the DP for Shortest Common Superseq, where dp[i][j] = SCS of strings a (from 0 till index i) and b (from 0 till index j)
    Code:
    #include
    string shortestSupersequence(string a, string b) {
    // Write your code here.
    int n = a.length(), m = b.length();
    vector dp(n, vector(m, ""));
    string temp = "";
    int flag=0;
    for (int i = 0; i < m; i++)
    {
    temp+=b[i];
    dp[0][i]=temp;
    if(!flag)
    {
    if(a[0]==b[i]) flag=1;
    else dp[0][i]+=a[0];
    }
    }
    temp="";
    flag=0;
    for (int i = 0; i < n; i++)
    {
    temp+=a[i];
    dp[i][0]=temp;
    if(!flag)
    {
    if(a[i]==b[0]) flag=1;
    else dp[i][0]+=b[0];
    }
    }
    for(int i=1;i

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 2 роки тому

    Understood, sir. Thank you very much.

  • @rudrajitgupta3688
    @rudrajitgupta3688 Рік тому

    understood , Thank you Striver

  • @MrJatanmehta
    @MrJatanmehta 3 місяці тому

    amazing explanation

  • @abhishek__anand__
    @abhishek__anand__ Рік тому

    Great Explanation

  • @rohitchanda8461
    @rohitchanda8461 11 місяців тому +2

    Amazing explanation! You have made DP really solvable, thanks a lot Striver!
    While solving though, when memo[i - 1][j] == memo[i][j - 1], I appended str1.charAt(i - 1) and then decremented i and j counters. This yielded wrong output in a test case. To fix that I had to instead compare the characters at those indices. Instead of this condition, I wrote if(str1.charAt(i - 1) == str2.charAt(j - 1) { sb.append(str1.charAt(i - 1); i--; j--; } and it worked just fine. Sounds like the same thing to me but the previous condition was not working I don't know why. Any explanation for this would be deeply appreciated, guys.

  • @anshumaan1024
    @anshumaan1024 Рік тому +1

    TC -> O(n+m), for traversing the dp[][] table

  • @me.deepaksharma
    @me.deepaksharma 2 роки тому +42

    Use scs = char + scs, instead of reversing the string.

    • @parthsalat
      @parthsalat 2 роки тому +6

      Not All Heroes Wear Capes 🦸

    • @18lakhvinder18
      @18lakhvinder18 2 роки тому +5

      i was living under a rock for so long.

    • @anumoynandy5811
      @anumoynandy5811 Рік тому +12

      it will increase the time complexity for inserting a new character at the beginning of any string will take O(len(str)) Time and we have to do it for let say N times , then it will give us TLE .

    • @parthsalat
      @parthsalat Рік тому +3

      @@anumoynandy5811 Good point! I never thought about that!

    • @sumanpatnala9064
      @sumanpatnala9064 Рік тому

      @@anumoynandy5811 inserting a character in beginning of string is just O(1). And indeed appending it in end of string is o(len(str)).

  • @A_Myth963
    @A_Myth963 29 днів тому

    this guy deserves every follower he got

  • @HarshalDev
    @HarshalDev 3 місяці тому

    Awesome video . Striver orz 🙏

  • @prabhakaran5542
    @prabhakaran5542 3 місяці тому +1

    Understood ❤

  • @DivyaKumari179
    @DivyaKumari179 Місяць тому +1

    Understood sir!!!

  • @pranav732
    @pranav732 Рік тому

    Understood, thank you!

  • @pj-nz6nm
    @pj-nz6nm Рік тому

    only because of you solution came into my mind before your explanation.

  • @santoshb7776
    @santoshb7776 8 місяців тому

    Understood sir ! 🙏🙏

  • @anshumaan1024
    @anshumaan1024 Рік тому +1

    4:41 : suffice meaning - to be enough for somebody/something 🙂

  • @rohanrai5932
    @rohanrai5932 Місяць тому

    at 1:35 you get the idea and solve the problem , striver is GOAT

  • @shivisingh9975
    @shivisingh9975 3 місяці тому

    Understood sir!

  • @AJ-xc3ks
    @AJ-xc3ks 10 місяців тому

    Omg ! Just awesome 👍👍❤

  • @learner2027
    @learner2027 2 роки тому

    understood. thanks.

  • @mr-pm9eg
    @mr-pm9eg Рік тому

    i solved this. amazing content striver sir.

  • @rishabhagarwal8049
    @rishabhagarwal8049 Рік тому

    Understood Sir thank you very much

  • @pankajmittal1002
    @pankajmittal1002 2 місяці тому

    understood!!

  • @jeet7000
    @jeet7000 Рік тому +1

    nice sir..understood.

  • @gsampath8017
    @gsampath8017 2 роки тому +1

    Thanks striver❣️

  • @mohammedadnan2450
    @mohammedadnan2450 11 місяців тому +1

    we can also find ans by:
    iterating over LCS and adding all those characters of stg1 and stg2 that lies before i th character of LCS....because LCS already contains common characters we only need to add those extra characters of stg1 and stg2 to our LCS in same order to conver it into our ANS
    //LCS is logest common subsequence string which can be find using DP lec 26
    int i=0;
    int j=0;
    string ans="";
    for(int k=0 ; k < LCS.size() ; k++ ) {

    while(stg1[i]!=LCS[k])
    ans+=stg1[i++]
    while(stg2[j]!=LCS[k])
    ans+=stg2[j++];
    ans+=LCS[k];
    i++;
    j++;
    }
    while(i

  • @arushbhatia1298
    @arushbhatia1298 Рік тому

    NICE EXPLANATION

  • @shriRadheHariom
    @shriRadheHariom День тому

    Understood sir😄

  • @googleit2490
    @googleit2490 8 місяців тому

    Done and dusted in DP revision :)
    Though took a lot of time to figure out (~33mins)
    Nov'17, 2023 06:07 pm

  • @TheDev05
    @TheDev05 Рік тому +10

    How to determine when to use DP table for solving a problem or when to use recursion or memo ??

    • @amanmishra-vt8hk
      @amanmishra-vt8hk 5 місяців тому +4

      Instead of revolving around memo or recursion try to understand the concept behind the answer, Why this dp array looks like, why this dp[1][2] contains 2 and what does it means. As you will understand the meaning, first try to write the code if possible through recursion or memo, otherwise try to write by your own logic (that you understood why you have created the dp matrix).

  • @aruna5869
    @aruna5869 4 місяці тому

    understood :)❤

  • @original_gangsta_
    @original_gangsta_ 2 роки тому +1

    Understood 💯💯💯

  • @anshul5533
    @anshul5533 2 роки тому +7

    Bhaiya shayad aapne glti se shortest ki jagah longest likh diya title me

  • @abdallaalhag4425
    @abdallaalhag4425 7 місяців тому

    Understand!

  • @me.deepaksharma
    @me.deepaksharma 2 роки тому +1

    Thank You :)

  • @harshitgarg987
    @harshitgarg987 Рік тому +2

    Hi striver thanks for the detailed explanation,
    I have one question if there are 2 longest common subsequence then what should we do in that case?
    Like
    Str1 = horse
    Str2 = ros
    Then there are 2 lcs rs and os
    Then the supersequence min steps would be 1 > horose but by the formulae it would be 2 steps Horrose

    • @ankitpatil2979
      @ankitpatil2979 Рік тому

      it will be horse itself i think

    • @Sha-256-rath
      @Sha-256-rath Рік тому

      @@ankitpatil2979 with the formula also the required minimum steps is 1 only, please recheck your calculations

  • @BADASSBLADE
    @BADASSBLADE 10 місяців тому

    Understood!

  • @the_shridhar
    @the_shridhar 2 роки тому

    Wah bhaiya! Ekdum mast ❤

  • @ScienceSeekho
    @ScienceSeekho Рік тому

    Thanks

  • @arpitrajput6424
    @arpitrajput6424 2 роки тому

    UNDERSTOOD ++👌👌😍😍❤❤

  • @adityasaxena6971
    @adityasaxena6971 Рік тому +1

    Done on my own🤩🤩thanks striver

    • @anukulsahu
      @anukulsahu Рік тому

      whats the Time complexity?

    • @adityasaxena6971
      @adityasaxena6971 Рік тому

      @@anukulsahu O(N*M) where n = size of 1st string and m = size of 2nd same as time complexity of LCS code

  • @user-ke7fs7ds6h
    @user-ke7fs7ds6h 7 місяців тому

    ustaadan de ustaad😍😍

  • @_hulk748
    @_hulk748 Рік тому

    understood sir❤️🙏

  • @himanshuagrawal8012
    @himanshuagrawal8012 2 роки тому +5

    Instead of Doing Reversal of string....We can also make a dummy string of length (n + m - lcs( s, t ) ) and maintain index to the last of the dummy string and whenever we need that charracter we just update over dummy string and reduce the index..... We can do this na ? @STRIVER #UNDERSTOOD

    • @takeUforward
      @takeUforward  2 роки тому +3

      Yes just like we did in lcd

    • @himanshuagrawal8012
      @himanshuagrawal8012 2 роки тому

      @@takeUforward Yes like we printed LCS...Thanks

    • @amankrsingh
      @amankrsingh 2 роки тому +1

      @@takeUforward or we can do like
      ans = s[i-1] + ans
      Instead of ans+= s[i-1]

    • @takeUforward
      @takeUforward  2 роки тому +2

      @@amankrsingh that operation is costly 😅

  • @anubhavdeepankar6681
    @anubhavdeepankar6681 10 днів тому

    21:53 , similar to what we did at end of merge sort

  • @pardhi8959
    @pardhi8959 3 місяці тому

    Understood.

  • @naveen_kotha
    @naveen_kotha 2 роки тому

    Understood 🔥🔥

  • @bhavya8608
    @bhavya8608 Рік тому

    understooodddddd!!!!!!!!!!!!!!!

  • @hrituraj5973
    @hrituraj5973 10 місяців тому

    understood

  • @ganeshkamath89
    @ganeshkamath89 2 роки тому +5

    Thanks Striver. Understood SCS.
    How to use the space optimized DP for this problem?

    • @parthsalat
      @parthsalat 2 роки тому +1

      I saw your comments in almost all videos of this playlist.
      Are you in 4th Year?

    • @ganeshkamath89
      @ganeshkamath89 2 роки тому +3

      @@parthsalat Hi Parth. No I am learning DP just like you.

  • @akashkumarsingh3595
    @akashkumarsingh3595 2 роки тому

    Understood🔥🔥

  • @harshbokan6874
    @harshbokan6874 2 роки тому

    Understood ❤️❤️🔥🔥

  • @amishasahu1586
    @amishasahu1586 2 роки тому

    Understood ♥️🌿

  • @kumarpurushottam632
    @kumarpurushottam632 Рік тому

    Thanks Striver Got It : )

  • @jimspatel2407
    @jimspatel2407 2 роки тому

    Understood !! 🔥

  • @kartikrameshchavan6662
    @kartikrameshchavan6662 Рік тому +1

    Understood 🙏

  • @rajsekhardutta8891
    @rajsekhardutta8891 Рік тому

    Understood!!🤩

  • @devbhojani9274
    @devbhojani9274 2 місяці тому

    Understood...

  • @RajKumar-mk7bs
    @RajKumar-mk7bs 2 роки тому +10

    Please update the notes on websites it is only upto lec 23 , notes are very helpful 👍 bhaiya .... thanks for your hard work keep going

    • @takeUforward
      @takeUforward  2 роки тому +6

      Coming Soon, Anshuman writes it, I don't wanna give it to other people, as the quality matters, so it will come up soon!

    • @anshumansharma4580
      @anshumansharma4580 2 роки тому +13

      Hi, Anshuman here. Further notes are uploaded. I will complete the remaining ones as soon as possible.

    • @pravinkumarsaha5691
      @pravinkumarsaha5691 2 роки тому

      @@anshumansharma4580 uploaded where, here the note's link does not have them, can u please share..

  • @cameye9
    @cameye9 Місяць тому

    I have a cross question regarding this question that if we want to find smallest resultant string of S1 and S2 strings such that resultant string contains both S1 and S2 as a substring. Smallest resultant string means just returning length of that string. Btw this question came in coding ninja weekly contest 128 which held on 30/05/2024 and that is the 2 question of the contest and I was stuck on this question for next 2 hours in whole contest.

  • @parshchoradia9909
    @parshchoradia9909 Рік тому

    Understood Sir!

  • @saurabhrthakur
    @saurabhrthakur 3 місяці тому

    Understood

  • @aftabalam7103
    @aftabalam7103 2 роки тому

    Undesrtood well and clear

  • @sameersahu4569
    @sameersahu4569 2 роки тому

    Understood!!Can we use the same approach to print as we used in printing LCS...i tried that and it is not working

  • @LBK3
    @LBK3 Рік тому

    understood ❤

  • @Akashkumar-hz1by
    @Akashkumar-hz1by 2 роки тому +3

    Unbelievable 😂this time

  • @yeswanthh5068
    @yeswanthh5068 Рік тому +1

    Understood 🙂✌️✌️

  • @shubhigupta5689
    @shubhigupta5689 Рік тому

    Understood🌻

  • @hitheshpk6030
    @hitheshpk6030 Рік тому

    UNDERSTOOD