Тёмный

DP 31. Shortest Common Supersequence | DP on Strings 

take U forward
Подписаться 668 тыс.
Просмотров 157 тыс.
50% 1

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

Опубликовано:

 

12 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 398   
@zaidshaikh2536
@zaidshaikh2536 2 года назад
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 года назад
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!
@parthh3963
@parthh3963 11 дней назад
wtf? bro this one was like the toughest yet!!! congrats
@_archit_sahay
@_archit_sahay Год назад
Almost did it Myself. Really helpful playlist cuz this is the closest I have gotten to solving a hard question myself
@manitsaharan5995
@manitsaharan5995 2 года назад
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
@stith_pragya
@stith_pragya 8 месяцев назад
UNDERSTOOD..........Thanks a ton Striver Bhaiya for this wonderful series.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@pranjalthapliyal2278
@pranjalthapliyal2278 Год назад
Understood :) Prolly the toughest problem so far. Did it on my own in some other way, feeling v v satisfied
@codetechiee
@codetechiee Год назад
Can you please tell your approach??
@rahulbrave1126
@rahulbrave1126 Год назад
chal jutha
@skilz9525
@skilz9525 Год назад
@@rahulbrave1126 abe XD
@ayepea4471
@ayepea4471 24 дня назад
le me needing solutions
@rohanrai5932
@rohanrai5932 3 месяца назад
at 1:35 you get the idea and solve the problem , striver is GOAT
@VarshaSingh-hi2sb
@VarshaSingh-hi2sb 23 дня назад
Can you explain me If S1[2] != S2[2] why we move to the cell with the greatest value ? I am getting confused
@kushalgupta2041
@kushalgupta2041 19 дней назад
@@VarshaSingh-hi2sb first of all if you can then watch print lcs of striver video but let me tell you it is very simple we are moving to the cell with greatest value is because that in the current cell value can come from three direction one diagonal, vertical, horizontal but if they are not equal then you the value coming is from either vertical or horizontal but you know we are taking the max so simple maximum value of vertical and horizontal. That's all
@VarshaSingh-hi2sb
@VarshaSingh-hi2sb 19 дней назад
@@kushalgupta2041 Thank you
@Sandeep-zd6dq
@Sandeep-zd6dq 2 года назад
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 👍😂🔥
@dipaligangawane980
@dipaligangawane980 2 года назад
Thank you so much striver for wonderful series. Understood every problem present in the series
@Harshit126
@Harshit126 2 года назад
Understood, thanks. Both of the vides helped a lot to understand string extraction from DP matrix via tabulation :)
@me.deepaksharma
@me.deepaksharma 2 года назад
Use scs = char + scs, instead of reversing the string.
@parthsalat
@parthsalat 2 года назад
Not All Heroes Wear Capes 🦸
@18lakhvinder18
@18lakhvinder18 2 года назад
i was living under a rock for so long.
@anumoynandy5811
@anumoynandy5811 2 года назад
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 2 года назад
@@anumoynandy5811 Good point! I never thought about that!
@sumanpatnala9064
@sumanpatnala9064 2 года назад
@@anumoynandy5811 inserting a character in beginning of string is just O(1). And indeed appending it in end of string is o(len(str)).
@securelyinsaycure
@securelyinsaycure 6 месяцев назад
you are golden! thanks to you(for explaining such complex problems with ease) hearts won't be broken anymore
@ishangujarathi10
@ishangujarathi10 Год назад
Understood!!! another LC HARD problem, but your explanation and previous concepts makes it looks so simple!!! AMAZINGGG!!!LOVEDD IT
@AlokLaha-te2pd
@AlokLaha-te2pd 2 месяца назад
Understood. You made it simple to understand how to pick uncommon elements into our result string.
@TheDev05
@TheDev05 Год назад
How to determine when to use DP table for solving a problem or when to use recursion or memo ??
@amanmishra-vt8hk
@amanmishra-vt8hk 7 месяцев назад
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).
@rohitchanda8461
@rohitchanda8461 Год назад
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.
@sanyamwalia217
@sanyamwalia217 Год назад
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
@mohammedadnan2450
@mohammedadnan2450 Год назад
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
@uditgarg6508
@uditgarg6508 3 месяца назад
wrong. giving rte
@harshitgarg987
@harshitgarg987 Год назад
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
@developer0707
@developer0707 Год назад
it will be horse itself i think
@Sha-256-rath
@Sha-256-rath Год назад
@@developer0707 with the formula also the required minimum steps is 1 only, please recheck your calculations
@anshumaan1024
@anshumaan1024 Год назад
4:41 : suffice meaning - to be enough for somebody/something 🙂
@priyanshkumar17
@priyanshkumar17 2 месяца назад
Great explanation bhaiya!! Understood this concept of printing the shortest supersequence using LCS DP table clearly...Thanks. This was indeed a difficult one..
@anshul5533
@anshul5533 2 года назад
Bhaiya shayad aapne glti se shortest ki jagah longest likh diya title me
@UECAshutoshKumar
@UECAshutoshKumar 2 месяца назад
Thank you Understood!!!
@electron-u8p
@electron-u8p 26 дней назад
7:45 n(AUB)=n(A)+n(B)-n(A^B) 😊
@anshumaan1024
@anshumaan1024 Год назад
TC -> O(n+m), for traversing the dp[][] table
@A_Myth963
@A_Myth963 2 месяца назад
this guy deserves every follower he got
@RajKumar-mk7bs
@RajKumar-mk7bs 2 года назад
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 года назад
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 года назад
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..
@hashcodez757
@hashcodez757 Месяц назад
"UNDERSTOOD BHAIYA!!"
@prabhakaran5542
@prabhakaran5542 5 месяцев назад
Understood ❤
@adityasaxena6971
@adityasaxena6971 Год назад
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
@himanshuagrawal8012
@himanshuagrawal8012 2 года назад
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 года назад
Yes just like we did in lcd
@himanshuagrawal8012
@himanshuagrawal8012 2 года назад
@@takeUforward Yes like we printed LCS...Thanks
@amankrsingh
@amankrsingh 2 года назад
@@takeUforward or we can do like ans = s[i-1] + ans Instead of ans+= s[i-1]
@takeUforward
@takeUforward 2 года назад
@@amankrsingh that operation is costly 😅
@chandrachurmukherjeejucse5816
Understood. Great content striver. Keep it up. Thanks for all your efforts to uplift the community.
@sudiptvyas4536
@sudiptvyas4536 Год назад
Understood! You explained it so well! Thank you so much for making these videos
@jeet7000
@jeet7000 Год назад
nice sir..understood.
@jyothiyadav2595
@jyothiyadav2595 10 месяцев назад
Thank you sooo much striver , this playlist is just amazing, mind-blowing, just like a woow
@kartikrameshchavan6662
@kartikrameshchavan6662 Год назад
Understood 🙏
@DivyaKumari179
@DivyaKumari179 2 месяца назад
Understood sir!!!
@Shubh_____
@Shubh_____ 2 месяца назад
thanks sir !! cant express my gratitude in word.
@cameye9
@cameye9 3 месяца назад
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.
@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
@RVcalisthenics
@RVcalisthenics 12 дней назад
Thanks a lot sir for this amazing series😇.
@CEOplays
@CEOplays 2 года назад
Thank you so much STRIVER for these amazing videos!!🙌🙌❤❤
@pj-nz6nm
@pj-nz6nm Год назад
only because of you solution came into my mind before your explanation.
@parthsalat
@parthsalat 2 года назад
Understood kaka!
@raghavmanish24
@raghavmanish24 25 дней назад
understood....thanku striver
@anubhavdeepankar6681
@anubhavdeepankar6681 Месяц назад
21:53 , similar to what we did at end of merge sort
@shera2667
@shera2667 Месяц назад
US, the DP on string is still something which is not eas for me to pick up, as I cant see the patterns by myself. but after watching half of the video I was able to code the solution
@clashtm8210
@clashtm8210 16 дней назад
konse year mai ho bhai?
@navneetdubey6918
@navneetdubey6918 11 месяцев назад
Great explanation underStand Striver
@pabitrakb5291
@pabitrakb5291 Год назад
Just dooper explanation Understood sir!!! ❤️ Thank you sooooooo much for your striving efforts
@aryanyadav3926
@aryanyadav3926 2 года назад
Wonderful explanation!
@ganeshkamath89
@ganeshkamath89 2 года назад
Thanks Striver. Understood SCS. How to use the space optimized DP for this problem?
@parthsalat
@parthsalat 2 года назад
I saw your comments in almost all videos of this playlist. Are you in 4th Year?
@ganeshkamath89
@ganeshkamath89 2 года назад
@@parthsalat Hi Parth. No I am learning DP just like you.
@deepakbhallavi1612
@deepakbhallavi1612 Год назад
Thank you for the playlist ❤
@desh756
@desh756 2 года назад
What about str1=bruteo str2=groot For this I think this is not working if working give wrong ans.
@bhargavinaik8145
@bhargavinaik8145 5 месяцев назад
I have a doubt here, what if the common letters are not in right order? Ex: s1=horse, s2 = rose. In this case we will end up with hrorse or rhorse we are taking r twice. Isnt it wrong?
@googleit2490
@googleit2490 10 месяцев назад
Done and dusted in DP revision :) Though took a lot of time to figure out (~33mins) Nov'17, 2023 06:07 pm
@striverdaaadi
@striverdaaadi 8 месяцев назад
ustaadan de ustaad😍😍
@_Kunal_Pawar
@_Kunal_Pawar 6 месяцев назад
Understood! I can't thank you enough
@Akashkumar-hz1by
@Akashkumar-hz1by 2 года назад
Unbelievable 😂this time
@sukritiguin5637
@sukritiguin5637 7 месяцев назад
Amazing Explanation ❤❤
@surajkumar-hd2kc
@surajkumar-hd2kc Год назад
We might have more then one Supersubsequence for one problem and both have same length. like s1= brute s2= groot Result1= bgrooute and Result2 = bgruoote then which one will be prefer.
@sauravdutta
@sauravdutta Год назад
There could be multiple answers. Both are fine, as long as it's the minimum length.
@cinime
@cinime 2 года назад
Understood! Thank you so so SO much as always!!
@GauravThinks
@GauravThinks 7 месяцев назад
For those who are Having any difficulty getting proper code : Note** The apporach is in reverse that of what raj bhai did . 🙂 class Solution { public: string shortestCommonSupersequence(string s, string t) { int n= s.size(); int m= t.size(); string super=""; vector dp(n+1, vector (m+1 , 0)); for(int i=n-1; i>=0; i--){ for(int j=m-1; j>=0; j--){ int take=0, nottake=0; if(s[i]==t[j]) take= 1 + dp[i+1][j+1]; else nottake= (max(dp[i+1][j], dp[i][j+1])); dp[i][j]= max(take, nottake); } } cout
@rudrajitgupta3688
@rudrajitgupta3688 Год назад
understood , Thank you Striver
@MrJatanmehta
@MrJatanmehta 4 месяца назад
amazing explanation
@spree9053
@spree9053 2 года назад
*exclaims "I am groot" after solving this question* 💪
@shriRadheHariom
@shriRadheHariom Месяц назад
Understood sir😄
@DevashishJose
@DevashishJose 8 месяцев назад
Thank you so much, Understood.
@vatsalvasoya5243
@vatsalvasoya5243 Год назад
Understood!! Wonderful explanation!!!
@HarshalDev
@HarshalDev 5 месяцев назад
Awesome video . Striver orz 🙏
@abhishek__anand__
@abhishek__anand__ Год назад
Great Explanation
@gsampath8017
@gsampath8017 2 года назад
Thanks striver❣️
@ishaankaustav727
@ishaankaustav727 2 года назад
loved the way u explained 💚💚💚💚
@sparshyadav9709
@sparshyadav9709 Месяц назад
Understood.
@ranasauravsingh
@ranasauravsingh 2 года назад
UNDERSTOOD...!! Thanks striver for the video... :)
@bhavya8608
@bhavya8608 Год назад
understooodddddd!!!!!!!!!!!!!!!
@VikashYadav-px8ei
@VikashYadav-px8ei Год назад
Understood 🎉
@sparshbarolia1556
@sparshbarolia1556 Год назад
@original_gangsta_
@original_gangsta_ 2 года назад
Understood 💯💯💯
@hitheshpk6030
@hitheshpk6030 Год назад
UNDERSTOOD
@36_sachinkumar92
@36_sachinkumar92 Год назад
What if we have tho print all the supersequence possible then ???
@kathanvakharia
@kathanvakharia Год назад
Understood...Completed (31/56)
@gourishettyvivek4153
@gourishettyvivek4153 2 года назад
understood striver
@me.deepaksharma
@me.deepaksharma 2 года назад
Thank You :)
@ScienceSeekho
@ScienceSeekho 2 года назад
Thanks
@AJ-xc3ks
@AJ-xc3ks Год назад
Omg ! Just awesome 👍👍❤
@shubhigupta5689
@shubhigupta5689 Год назад
Understood🌻
@jigardoshi2852
@jigardoshi2852 2 года назад
Its nice to directly use DP table, but it would be great if you can explain the recursive part and see if we can develop using that. I tried doing recursive way but got stuck.
@pranavsharma7479
@pranavsharma7479 2 года назад
see all lectures bro
@jigardoshi2852
@jigardoshi2852 2 года назад
@@pranavsharma7479 I have watched all 50, I was specifically asking for this problem. If you can come up with recursive approach with this problem do send me the code piece. Thanks
@jigardoshi2852
@jigardoshi2852 2 года назад
@@pranavsharma7479 Did you happen to solve it using recursive method "bro" ?
@daxterminator9
@daxterminator9 2 года назад
@@jigardoshi2852 I did manage to write a recursive memoization solution for this problem, but ended up getting a TLE for the last case on leetcode. Can you think of doing any optimization to this? class Solution { public: string solve(int i, int j, int n, int m, string &s, string &t, vector &dp){ if(i==n && j==m) return ""; else if(i==n) return t.substr(j,m-j+1); else if(j==m) return s.substr(i,n-i+1); if(dp[i][j]!="-1") return dp[i][j]; if(s[i]==t[j]) return dp[i][j]=s[i]+solve(i+1,j+1,n,m,s,t,dp); else { string fs=s[i]+solve(i+1,j,n,m,s,t,dp); string ss=t[j]+solve(i,j+1,n,m,s,t,dp); return dp[i][j]=fs.size()
@jigardoshi2852
@jigardoshi2852 2 года назад
@@daxterminator9 thanks will check it out.
@iamsatyamsaurav
@iamsatyamsaurav 4 дня назад
understood
@suiwala
@suiwala Год назад
Got it Sir
@LowkeyCoder
@LowkeyCoder 9 месяцев назад
"Understood"!
@pranav732
@pranav732 Год назад
Understood, thank you!
@devbhojani9274
@devbhojani9274 4 месяца назад
Understood...
@iamnoob7593
@iamnoob7593 8 месяцев назад
US striver
@mr-pm9eg
@mr-pm9eg Год назад
i solved this. amazing content striver sir.
@animeshkumar2683
@animeshkumar2683 25 дней назад
Understood
@arushbhatia1298
@arushbhatia1298 Год назад
NICE EXPLANATION
@yeswanthh5068
@yeswanthh5068 2 года назад
Understood 🙂✌️✌️
@ratinderpalsingh5909
@ratinderpalsingh5909 2 года назад
Understood, sir. Thank you very much.
@aftabalam7103
@aftabalam7103 2 года назад
Undesrtood well and clear
@sumitkumartah2106
@sumitkumartah2106 7 месяцев назад
Can this problem is done by using hashset ??
Далее
24 Shortest Common SuperSequence
22:59
Просмотров 195 тыс.
А ВЫ ЛЮБИТЕ ШКОЛУ?? #shorts
00:20
Просмотров 2,2 млн
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Longest Common Subsequence
7:55
Просмотров 761 тыс.
DP 50. Minimum Cost to Cut the Stick
30:02
Просмотров 135 тыс.