Тёмный
No video :(

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

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

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

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

 

24 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 813   
@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 7 месяцев назад
@@AdityaKumar-be7hx Requires god level consistency to do that.
@idris110
@idris110 7 месяцев назад
You will probably forget the concepts, since these are never used in the industry. What a satire !
@bharatravihal9646
@bharatravihal9646 7 месяцев назад
🤣🤣@@idris110
@spoidy2025
@spoidy2025 5 месяцев назад
brother are you placed
@art4eigen93
@art4eigen93 2 года назад
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 4 месяца назад
He is already for me.
@v7gamerff809
@v7gamerff809 7 дней назад
No a Guy named Srikar From Vizag will go down as the GOAT online DSA in future
@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
@arjundevmishra7207
@arjundevmishra7207 Год назад
Protect this guy at all costs. Thank you sir for all your hard work in making this video.
@mayanksingh7501
@mayanksingh7501 3 месяца назад
Everyone should be protected bro
@sujalgupta6100
@sujalgupta6100 Год назад
46:05 for space optimization we don't require the loop for prev as the values are already zero in it.
@dikshamakkar2850
@dikshamakkar2850 29 дней назад
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 23 дня назад
@@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?
@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.
@kartiksuman9814
@kartiksuman9814 2 года назад
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
@arunkumarsahoo1344
@arunkumarsahoo1344 2 года назад
15:34 "You kow i am hearing you" in the background
@jeet-smokey
@jeet-smokey 6 месяцев назад
So?
@kanikajain5627
@kanikajain5627 Месяц назад
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.
@duyhuynh1234
@duyhuynh1234 Год назад
You're a great teacher. If possible, please do problems about DP on tree. I struggle with them. Thank you!
@avimandavia6154
@avimandavia6154 Год назад
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 2 года назад
I am very grateful to you for uploading this playlist. This is great!!!!!!!!!!!!!!!!!!!!! Understood.
@siddharthsarraf330
@siddharthsarraf330 2 года назад
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
@akashyadagouda896
@akashyadagouda896 7 месяцев назад
Its taking while to digest this information for me , Just imagine the efforts this guy is adding to make it available for everyone.
@World-Of-Mr-Motivater
@World-Of-Mr-Motivater Месяц назад
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!!
@mjmusic65
@mjmusic65 Год назад
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.
@ramanahlawat398
@ramanahlawat398 Год назад
bro!!!!!! What an explanation, absolutely brilliant. I am starting to love coding now. Thanks
@priyanshuchouhan9845
@priyanshuchouhan9845 11 месяцев назад
who agrees that this dp series is one of the best on RU-vid
@tasneemayham974
@tasneemayham974 10 месяцев назад
Not one of the best no. THE BESTTTTT!!!!!
@sarveshamrute4982
@sarveshamrute4982 2 года назад
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
@ankita716
@ankita716 2 месяца назад
understood, always in awe of you genius and hard working you are:)
@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❤
@virgarg1012
@virgarg1012 2 месяца назад
Giving my best shot for preparing for my placements. Let's see what happens in upcoming months. Great Content
@VishalYadav-gk1kg
@VishalYadav-gk1kg Месяц назад
Very nice explanation sir, Thank you!
@yashsinghal3404
@yashsinghal3404 2 года назад
What an easy explanation, loved it !
@dharanyuvi6951
@dharanyuvi6951 Месяц назад
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.
@tejasghone5118
@tejasghone5118 2 года назад
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
@rohan8758
@rohan8758 2 месяца назад
Nice explanation raj bhaiyan or dude, I might call you angel!🥰🥰
@raghavmanish24
@raghavmanish24 7 дней назад
understood .....thanku striver .....let's finish this dp on string topic
@varunbansal1136
@varunbansal1136 10 месяцев назад
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.
@NiharIngale
@NiharIngale 22 дня назад
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.
@gyanunlimited740
@gyanunlimited740 2 года назад
Man the content is gold.
@ABDULRAHMAN-hr3ko
@ABDULRAHMAN-hr3ko 5 часов назад
thanks for beautiful explanation
@vedxnt10
@vedxnt10 3 месяца назад
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 Год назад
You can't find better explanation than this, Brilliant!!
@stith_pragya
@stith_pragya 7 месяцев назад
UNDERSTOOD.....Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@vamsikrishnareddy9345
@vamsikrishnareddy9345 Месяц назад
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
@vinaylj
@vinaylj Месяц назад
I was able to see that curr= prev; mistake as you were typing it. thanks to you Striver. you are the best
@abhinavgoli868
@abhinavgoli868 Месяц назад
when the character was not matching why are we not considering the possiblity of f(index1-1,index2-1). Thanks for the help
@HIMANSHU-u6y
@HIMANSHU-u6y 7 дней назад
Striver what do u eat, how can be someone explain in such an excellenct manner❤
@amartyadas5-yriddmechanica597
@amartyadas5-yriddmechanica597 2 года назад
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
@rpriyanka28
@rpriyanka28 4 месяца назад
he is the example of beauty with brains
@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 !!!
@m4n0jkum4r
@m4n0jkum4r Год назад
In Tabulation we are declaring vector with values zero. So we can skip first two loops.
@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❤
@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😂
@d_0364
@d_0364 Месяц назад
almost all of the questions in this playlist can be space optimized to not just O(2m) but O(m) if you apply some logic and visualize how the table is being filled.
@avikdey01
@avikdey01 2 года назад
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.
@ChutHunter
@ChutHunter 6 месяцев назад
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 ♥♥♥♥
@Harshanandita
@Harshanandita Год назад
Today, I solved my first ever dp problem in a contest just by watching this video... Codechef Starter 71 Longest Common Subsequence problem
@hashcodez757
@hashcodez757 22 дня назад
"UNDERSTOOD BHAIYA!!"
@harihardhik3293
@harihardhik3293 9 дней назад
Understood sir very well!!
@rakshitpandey7517
@rakshitpandey7517 2 года назад
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
@adarshanku7988
@adarshanku7988 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 ❤
@itsaryanb9316
@itsaryanb9316 Год назад
Memoization solution will give TLE on leetcode, simply pass the strings by reference !!
@lavkushdas5529
@lavkushdas5529 Год назад
what could be the reason?
@anuragprasad6116
@anuragprasad6116 Год назад
@@lavkushdas5529 not passing by reference will require the strings to be copied in every function call which will multiply overall time complexity by (n+m) required for copying strings.
@veereshr
@veereshr 2 года назад
Does interviewers expect us to know the tabulation directly? because I can solve tabulation only after analysing recursion
@viasmit
@viasmit 2 года назад
No, Its not expected to directly jump to the tabulation. In Contrast, the interviewer will rather be impressed, that you moved your solution trom recursion to memoised to space optimised solution
@parthsalat
@parthsalat 2 года назад
@@viasmit Exactly, they want to know how we reach the tabulation from the recursion
@divyanshsharma8401
@divyanshsharma8401 2 года назад
@@parthsalat is space optimization after tabulation essential?
@parthsalat
@parthsalat 2 года назад
@@divyanshsharma8401 An average interview question lasts around 45 mins. If you have time remaining, then yes, go for space optimisation. But it isn't compulsory.
@prabhakaran5542
@prabhakaran5542 5 месяцев назад
Understood ❤
@SethuIyer95
@SethuIyer95 5 месяцев назад
I deeply understand recursion, memoization, tabulation and space optimization.
@meetharsoda5152
@meetharsoda5152 2 месяца назад
Finally DP on strings Let's Go
@kareni7572
@kareni7572 Год назад
Why can't we put the base case at index 0? Especially for LCS - DP on strings related questions?
@pj-nz6nm
@pj-nz6nm Год назад
hey man i know you are a very good teacher , but you are also a very good human being.
@jitendrasinghsola
@jitendrasinghsola 2 года назад
I was always scared of these LCS and all but not anymore thanks to striver
@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 :)
@HarshSharma-ff3ox
@HarshSharma-ff3ox 2 года назад
Thank you Striver for such a wonderful explanation. Finally understood it intuitively. 💯💯
@AngadSingh97
@AngadSingh97 Месяц назад
Life Lesson: If Raj tells you to go revise a video, then go revise that video. It's really hard not to understand something once you watch everything in the order he presents.
@yeswanthh5068
@yeswanthh5068 2 года назад
Understood 🙂🙂💚
@namankaushik5693
@namankaushik5693 2 года назад
Me: getting distracted while watcing the video. Striver: 1:01 dekh ** 😂
@rishabhgupta9846
@rishabhgupta9846 Год назад
understood,previously I mugged up the tabulation formula for the exam ,but now I know how it came.
@vishious14
@vishious14 8 месяцев назад
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
@Sumeet_100
@Sumeet_100 2 месяца назад
Understood !!
@BG-lj6fw
@BG-lj6fw Год назад
understood.wonderful. amazing. hats-off. unmatchable.
@coding8000
@coding8000 Год назад
Don't know whether you will watch it or not? But, again @takeUforwad (striver bhaiya), 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!!!
@ranasauravsingh
@ranasauravsingh 2 года назад
UNDERSTOOD... Thanks striver for the video... :)
@jarvis3551
@jarvis3551 8 месяцев назад
did it in 1st attempt without seeing the video, shifting index to right was new to me
@parthsalat
@parthsalat 2 года назад
Continue from 23:30
@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
@harshitatripathi6440
@harshitatripathi6440 Год назад
best dp playlist on youtube
@himanshuagrawal8012
@himanshuagrawal8012 2 года назад
Can we do using single array if we denote prev[j-1] in a variable, keep updating it after every iteration of inner loop ?
@UECAshutoshKumar
@UECAshutoshKumar 2 месяца назад
Thank You Understood!!!
@raZer.7_7
@raZer.7_7 Месяц назад
UNDERSTOOD!
@rahulsaha332
@rahulsaha332 2 года назад
Decode ways is a new kind of pattern on dp on strings i think and that can be covered
@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.
@logeshv2125
@logeshv2125 2 месяца назад
Understood Striver✨
@AniKet-bw7vv
@AniKet-bw7vv Год назад
without shifting tabulation method: vector dp(n1, vector(n2)); if(s[0] == t[0]) dp[0][0] = 1; else dp[0][0] = 0; for(int i = 1; i < n1; i++){ if(s[i] == t[0]){ dp[i][0] =1+ dp[i-1][0]; } else dp[i][0] = dp[i-1][0]; } for(int i = 1; i < n2; i++){ if(s[0] == t[i]){ dp[0][i] = 1+dp[0][i-1]; } else dp[0][i] = dp[0][i-1]; } for(int index1 = 1; index1 < n1; index1++){ for(int index2 = 1; index2 < n2; index2++){ if(s[index1] == t[index2]) dp[index1][index2] = 1+dp[index1-1][index2-1]; else dp[index1][index2] = max(dp[index1][index2-1], dp[index1-1][index2]); } } return dp[n1-1][n2-1];
@ritikshandilya7075
@ritikshandilya7075 2 месяца назад
Thanks for great explanation striver
@honeykumar5700
@honeykumar5700 2 года назад
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
@YourEngineerBro_
@YourEngineerBro_ 4 дня назад
Quality content
@sauravfarkade7032
@sauravfarkade7032 3 часа назад
Undersoood!!
@cinime
@cinime 2 года назад
Understood! Awesome explanation as always, thank you very much!!!
@AmanKumar-qz4jz
@AmanKumar-qz4jz 7 месяцев назад
god!!! of teaching dsa understood
@dashsights4258
@dashsights4258 Год назад
Just one word , Beautiful!
@coderrr3353
@coderrr3353 Год назад
This man is saviour. Thanks bhaiya ❤️
@iamnoob7593
@iamnoob7593 7 месяцев назад
US Striver , Getting into Google i can image ur DSA level 🔥
@SurajPrasad-bf9qn
@SurajPrasad-bf9qn Месяц назад
you are too awesome bro
@HarchitGulati
@HarchitGulati Год назад
Bhaiya ,why cant we solve this question like if we have got two strings then we are eliminating the characters from both strings that are uncommon and dont have frequency 1 (checking that in common characters)..ultimately we will get two resultant strings and if they are equal that is longest subsequence and if not there is no longest common subsequence present ..pls clear my doubt
@deepurana6010
@deepurana6010 Год назад
You are amazing ❤️. Thanks a lot for this valuable content 🙌🙇🙇
@theresilientpianist7114
@theresilientpianist7114 Месяц назад
Understood❤❤🔥🔥
@DevashishJose
@DevashishJose 7 месяцев назад
Understood, Thank you so so much.
@original_gangsta_
@original_gangsta_ 2 года назад
Understood...
@bitran4283
@bitran4283 11 месяцев назад
You always have very good solutions, but your writing is hard for us to clearly get all information, I think we should write upper case for all letters, then it will be helpful for all. Thank you
Далее
DP 26. Print Longest Common Subsequence | Dp on Strings
16:55
WILL IT BURST?
00:31
Просмотров 3,2 млн
Yana bir yangi qo'shiq YORAM BIYO | Yaqin kunlarda
00:57
DP 27. Longest Common Substring | DP on Strings 🔥
14:02
DP 31. Shortest Common Supersequence | DP on Strings
26:44
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Launching the best DSA Course + Platform
36:29
Просмотров 196 тыс.
WILL IT BURST?
00:31
Просмотров 3,2 млн