DP 33. Edit Distance | Recursive to 1D Array Optimised Solution 🔥

Sdílet
Vložit
  • čas přidán 10. 03. 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/3HcTJdy
    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 Edit Distance 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

Komentáře • 512

  • @Anonymous-uj3jx
    @Anonymous-uj3jx Před 2 lety +422

    Hey Striver, I got this question in my Microsoft Internship Interview today, without your video I would never have solved this question so perfectly, the interviewer is indeed amazed by the space optimization that I did! Thank you so much!

  • @RaghavSharma-nt3hr
    @RaghavSharma-nt3hr Před 2 lety +223

    After 33 lecs, 90% effort is for recursion 10% effort for memo,tabulation.space opti.
    if u master recursion all the next 3 things are piece of cake.

    • @AshishKumar-ww1mr
      @AshishKumar-ww1mr Před rokem +11

      true , we just need to understand the recurrance , its more about the question than dp itself

    • @muditkhanna8164
      @muditkhanna8164 Před 10 měsíci

      they say just trust the leap of faith and trust PMI , and recursion is done, i didn't do that. and i'm glad i did it my way!

    • @priyanshkumariitd
      @priyanshkumariitd Před 25 dny

      Yes, agreed :)

  • @pavannettam8968
    @pavannettam8968 Před rokem +11

    I’m following the DP play list from the beginning. What an amazing way to deliver content, I’m really amazed to see clarity in the steps thought. Thank you sooo much :)

  • @anuragpandey8165
    @anuragpandey8165 Před 2 lety +23

    Hey Striver, I got the space optimization logic in the first time without looking at solution and wrong answer. All thanks to you man!

  • @your_name96
    @your_name96 Před 2 lety +1

    Thanks a lot again! I was always confused between insert and replace , this video finally cleared it all, also would like to add, the order is kinda important I think, like i variable is for the string that needs to be changed, j variable is kinda the target string but that does not change the answer as cost of insert for one string is the cost of deletion of another string so operations are equivalent. Correct me if I am wrong though.

  • @uddeshyatyagi5
    @uddeshyatyagi5 Před 2 lety +6

    Amazing Striver, you made me understand this question so easily !

  • @amancuber5235
    @amancuber5235 Před rokem +28

    Hey striver,
    I was able to do it myself and all the credit goes to you and aditya Verma!
    Thanks a lot

    • @magnetrix
      @magnetrix Před 4 měsíci

      What was your intuition? Does it match with LCS pattern taught by Aditya Verma or you thought it from scratch like Striver?

  • @kiranraaj7889
    @kiranraaj7889 Před rokem +2

    i could solve this on my own, this feels so great, thanks for the previous videos!

  • @iamnoob7593
    @iamnoob7593 Před 2 lety +5

    Hey Striver , Man ur videos are just superbly fantastic. I knew only how to recursion + memo dp for the past 2years , But i did not know Bottom up dp. I could not clear online test rounds due to TLE. Now after watching ur videos in dp bottom up/recursion approaches . I got some hope that i can crack interviews in product based.

  • @shantipriya370
    @shantipriya370 Před 8 měsíci +2

    first it seemed extremely complicated, but towards the end as it unfolded, it was very clear.. u r an amazing teacher..

  • @_Kunal_Pawar
    @_Kunal_Pawar Před 5 měsíci

    I've gotta say, your way of teaching is pretty awesome. I haven't come across any other CZcamsr breaking down tricky stuff like you do. It's obvious you've put in a ton of work to make things easy to understand 💯

  • @SreyesSrinivasan
    @SreyesSrinivasan Před rokem

    Amazing solution Striver! Thank you so much for explaining it so patiently!!

  • @namangarg8976
    @namangarg8976 Před 10 měsíci +1

    What a wonderful person you are. I solved this problem without viewing the video because of your previous videos

  • @suhasherle7350
    @suhasherle7350 Před rokem +5

    Initially I thought I won't be able to solve this question by myself, but I spent some time on it, thought about the different possible cases, and got the solution on my own. Your videos are just amazing !!

  • @souvikbhattacharjee2210
    @souvikbhattacharjee2210 Před rokem +3

    Thank you so much for putting this much effort..I never thought i would do this type of hard quiestions on my own..

  • @sasageyo9571
    @sasageyo9571 Před 2 lety +1

    Jus Amazing....................................................
    best explanation of edit distnce ever ....................
    explained all cases with such elegance and clarity 🙏🙏

  • @shubhammalviya3001
    @shubhammalviya3001 Před 2 lety +1

    Amazing you made it look very simple ,also the optimisation part was great 🔥

  • @sahilbadkul7130
    @sahilbadkul7130 Před 2 lety +4

    You explain in such a way even a kid who do not know any thing related this, will understand😄.
    Amazing video, definitely understood.

  • @karanthakur11
    @karanthakur11 Před 5 měsíci +5

    Method 1)-- recursion
    i=str1_hourse || intention, j=str2_ros || extention ()
    1.1)
    13:37 insert operation
    15:24 delete operation
    16:41 replace operation
    1.2)
    min
    1.3)
    Base case 17:58
    s1 exeusted 18:52 (min op to convert str1 to str2 (j+1))
    s2 exeusted 20:56 (min op to convert str1 to str2_khali dusri (i+1))
    1.4) space O(N+M)
    and time complexity (exponential e^x) 22:05
    Method 2)-- recursion memoisation 23:21 + tc & sc 23:30
    2.1)-- to reduce oxillary stack space present in rec_memo we use tabulation
    Method 2)---tabulation &
    space optimisation
    2.1) 26:58 to 28:35 one based indexing (slight change)
    2.2) base case 29:24
    2.3) I j ( bottom up= tabulation) 30:40
    2.4) copy the recurrence
    2.5)Code - 31:06
    *2.6) space opt---33:16
    2.7)concept of space optimasation 33:48
    2.8)Code

  • @dipanshusharma9084
    @dipanshusharma9084 Před 2 lety +18

    Note : you cannot return cur[m] because , in the case when str1 is empty and str2 has element , only prev array stores the corresponding m value

    • @OMPRAKASH-is7fj
      @OMPRAKASH-is7fj Před 8 měsíci +1

      True ... because of this only all test case was not passing in leetcode

  • @chandrachurmukherjeejucse5816

    Hello Striver. Thanks a lot for the amazing content. Solved the ques by myself just after understanding the question. Got a dopamine hit today.

  • @srijanprasad8319
    @srijanprasad8319 Před rokem +1

    literally THE BEST EXPLANATION for the problem

  • @abdallaalhag4425
    @abdallaalhag4425 Před 7 měsíci

    What a wonderful challenge! At first, I though this would be really tricky since we had to keep track of replacing but if we just move the index around and count them, it does all the work since we don't have to print anything. Really like this question! Understood as usual.

  • @shalinice3677
    @shalinice3677 Před rokem

    Thank you so much bhaiyya for this wonderful playlist, I have improved a lot on how to approach a problem and today I could solve this problem without even starting this video. Keep spreading the knowledge!!

  • @ankitpandey7262
    @ankitpandey7262 Před 2 lety +1

    Written Space Optimised Solution without Watching this Video ..... This show the level of concept your previous videos built. Thanks ✨💯

  • @animearena8443
    @animearena8443 Před rokem

    solved by myself. can't believe these used to look horrific a month ago. Thanks a ton man!

  • @rajeshseptember09
    @rajeshseptember09 Před 8 měsíci

    It is actually a very simple problem after you have simplified it so much !

  • @shambhavikhare9850
    @shambhavikhare9850 Před 11 měsíci +1

    Understood. extremely simple explanation, thankyou so much!

  • @InWonderland-z2l
    @InWonderland-z2l Před 13 dny

    The key thing to understand is that we don't ACTUALLY have to insert, delete or replace.. we only have to count the operations. This kind of observation may seem dumb at first glance, but when approaching this problem (or similar problems) myself, this is the major thing that causes a mind block and keeps me from getting to the solution.
    Great video!!🙏

  • @ganeshkamath89
    @ganeshkamath89 Před 2 lety +2

    Thanks Striver. Understood. Really enjoying the series.

  • @Sumit-wy4zp
    @Sumit-wy4zp Před 6 měsíci

    super duper easy ... thankyou so much striver your teaching style is something different 😍😍😍

  • @muskanchaurasia1532
    @muskanchaurasia1532 Před 8 měsíci

    This man is no doubt a genius .You literally made tough topics seem so easy.

  • @alifarah9
    @alifarah9 Před 2 lety +12

    Thanks

  • @sarthakyadav9950
    @sarthakyadav9950 Před rokem +1

    After so many lectures it is clear that the hardest part is to figure out the recursion. After that memoization,tabulation and space optimization is a piece of cake.

  • @stith_pragya
    @stith_pragya Před 6 měsíci +2

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

  • @Parthj426
    @Parthj426 Před 19 dny

    understood , and i'll like to mention that till today i was a bit confused abt the working of 2 arrays space optimisation , but after dry run , it got absolutely clear.
    thanks for this whole DP series , enjoying it too much.

  • @umangkumar2005
    @umangkumar2005 Před 5 měsíci

    you are the best teacher and the whole code i had written by myself without seeing your video

  • @ArdentMusicLover
    @ArdentMusicLover Před 3 měsíci

    Thank you, Striver for this outstanding series on DP. I've been following every DP video of yours. One intuition that I explored for this problem before abandoning it was using the LongestCommonSubsequence logic. I wish you would've added explanation on why LCS approach wouldn't have worked well for this problem. After all , if you subtract out the LCS value , I would've thought using the remaining characters from both would've been enough to come up with answer.

  • @anveshkarra3861
    @anveshkarra3861 Před 2 lety +1

    Scared a lot by seeing the question that how we can solve it but after watching the video , the concept is crystal clear . Enjoyed it a lot

  • @vanshsehgal9475
    @vanshsehgal9475 Před 2 lety +7

    Hey Striver! We can actually space optimise it to 1D array. If we see carefully see that from the prev row, we only require j-1 so why not store that in temp variable. Below is my code in C++:
    class Solution {
    public:
    int minDistance(string word1, string word2) {
    int n1=word1.size();
    int n2=word2.size();
    vector dp(n2+1,0);
    dp[0]=0;
    for(int j=1;j

    • @takeUforward
      @takeUforward  Před 2 lety +8

      Yess, my teachings 🥹

    • @vanshsehgal9475
      @vanshsehgal9475 Před 2 lety +3

      @@takeUforward yeah. Surely it's your teaching💯🔥

    • @techspecstm7032
      @techspecstm7032 Před 2 lety

      @@vanshsehgal9475 why this way of writing separately for all possibility is not working?
      static int f(int i,int j,String stri, String str2)
      if(i

    • @vanshsehgal9475
      @vanshsehgal9475 Před 2 lety

      @@techspecstm7032 bruh!! You mistakenly passed in replace function call in 1st param as 1-1. It should be i-1.

    • @techspecstm7032
      @techspecstm7032 Před 2 lety

      @@vanshsehgal9475 That was my typo mistake... It's correct in code but I am not getting the correct output. Am I correct in defining the recurrence?

  • @ranasauravsingh
    @ranasauravsingh Před 2 lety +1

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

  • @543-kishorebugatha7
    @543-kishorebugatha7 Před 6 měsíci

    Love you Bro without your videos dp is like a nightmare to me . Thank you for your wonderful explaination and the for the time you spent to do this series.

  • @manishbolbanda9872
    @manishbolbanda9872 Před rokem

    you explain DP very well. Thanks fpr making this video :)

  • @MeenakshiSharma-bd3bu

    thankyou bhaiya , previously i was having a fear of DP, but after reaching at this video , i was able to do this hard question by myself.............and m so much happy ,thank you for helping🥺❤

  • @mdmudassiralam5490
    @mdmudassiralam5490 Před 2 lety +21

    Hey striver, U taught us in such a way that now LC hard problems seems to be easy. I could have solve last 3 questions including this one without watching the video. Thanks a lot for such a great content ❤️.

    • @GajendraSingh-lv3jw
      @GajendraSingh-lv3jw Před 2 lety +2

      i have solved the last 6 question by myself now without watchinng the video... thts how good is his content

  • @Yag116
    @Yag116 Před rokem

    Very very good explanation!! UNDERSTOOD!!

  • @sanjivaniburma6171
    @sanjivaniburma6171 Před 2 měsíci

    Hey Striver, you are great man able to write the recursion on my own for this problem thanks to your series.
    I'm so happy now, its a max level of satisfaction...!
    Thanks sir for all of this

  • @reeteshtiwari5257
    @reeteshtiwari5257 Před 7 měsíci

    You are an absolute genius man!!

  • @1tav0
    @1tav0 Před rokem

    This was amazing thank you so much striver !!!! kind sir!!

  • @Telugu_europe
    @Telugu_europe Před rokem

    Understood... really liked your sense of humour while explanation at 16:50

  • @priyanshkumariitd
    @priyanshkumariitd Před 25 dny

    Thanks bhaiya!!! Understood

  • @DevashishJose
    @DevashishJose Před 6 měsíci

    Understood Thank you so much.

  • @user-rm7jm5tl3w
    @user-rm7jm5tl3w Před 27 dny

    very nicely explained!!

  • @ritikshandilya7075
    @ritikshandilya7075 Před 29 dny

    Thanks for great explanation Striver

  • @UECAshutoshKumar
    @UECAshutoshKumar Před měsícem +1

    Thank you
    Understood!

  • @cinime
    @cinime Před 2 lety

    Understood! So perfect explanation !!

  • @utkarshsingh405
    @utkarshsingh405 Před 10 měsíci

    Brilliant explanation!!

  • @reppee4392
    @reppee4392 Před 8 měsíci

    understood!!! i can't believe i did last 2 qs by myself 🔥

  • @Hrushi_2000
    @Hrushi_2000 Před 10 měsíci

    Understood. Thankyou Sir

  • @JoyBoy_013
    @JoyBoy_013 Před 10 dny +1

    God Level Explanation.

  • @shriRadheHariom
    @shriRadheHariom Před dnem

    Understood sir,Honestly, you are the best.

  • @inspirewithvikash5941
    @inspirewithvikash5941 Před měsícem

    Very Well Explained , I watched till 14:26 and then i solved it on leetcode.

  • @jyothiyadav2595
    @jyothiyadav2595 Před 8 měsíci

    Understood , ur explanations 🤌🤌

  • @theresilientpianist7114

    kudos to you man!

  • @ratinderpalsingh5909
    @ratinderpalsingh5909 Před rokem

    Understood, sir. Thank you very much.

  • @girishbhargava6367
    @girishbhargava6367 Před 2 lety

    Best playlist of DP that anywhere exists.....

  • @preetisahani5054
    @preetisahani5054 Před 9 měsíci

    Understood it very well

  • @gametech7046
    @gametech7046 Před rokem +8

    Hey striver,
    I was able to solve this question today... all by myself and felt a relief that maybe there is a chance for me to get a decent package coming from Tier-3 college. And this is possible only and ONLY because of your lectures. I know saying thank you doesn't mean much, but Thank You! from the bottom of my heart. I am not financially independent as of today, but I promise... the day I am, you are one of the first person that is going to pop in my head and I will definitely gift you something. Thank You So MUCH!!!

  • @Amine-yq7jg
    @Amine-yq7jg Před rokem

    your explanations are just beyond excellent ,US

  • @108_adityakumar6
    @108_adityakumar6 Před 2 lety

    Nice explanation.Understood the soln.

  • @googleit2490
    @googleit2490 Před rokem

    Understood :)
    Self-done it fully, thanks.
    July'30, 2023 23:42 pm

  • @rishabhgupta9846
    @rishabhgupta9846 Před rokem

    Understood,after getting Intuition that we can either replace ,delete ,insert a character to make it equal to other writing recursion was super easy.Just need to figure out base cases.

  • @_sf_editz1870
    @_sf_editz1870 Před 4 měsíci

    my interviews will be in 3 months i masterd top down and bottom up and thank you striver

  • @girava4034
    @girava4034 Před 3 dny

    Hey man you are amazing, Thank you for you hard work, you touched every aspect of the problem, but you didnt explain why you start from the beginning of the strings.

  • @neerajgupta9151
    @neerajgupta9151 Před rokem

    bhaii kya smjhata h ye banda🔥🔥🔥🔥
    wish saare teachers aise hote

  • @pintusuman5936
    @pintusuman5936 Před 2 lety

    your explaination is amazing man🙌🙌

  • @engg.5111
    @engg.5111 Před 11 měsíci

    did complete question by own after learn about else 1st case(insert) striver supermacy thankyou striver

  • @abhishekshrivastav6193

    thank you sir. here after watching *distinct subsequences*.
    and i am able to make recurrence by my self. 🌝🌝

  • @SethuIyer95
    @SethuIyer95 Před 4 měsíci

    Understood, did this myself.

  • @GunavardhanReddy-yq5pf
    @GunavardhanReddy-yq5pf Před 7 měsíci

    Hey, striver after reading comments, some are able to solve on their own so i also gave it a try and hurrah! able to write the recursive code.

  • @adityaraj-zm7zk
    @adityaraj-zm7zk Před 4 měsíci

    nice video bhaiya

  • @dev-rock
    @dev-rock Před 2 lety +2

    Hey, I have solved a DP problem using 2 approaches and only 1 seems to works...not able to figure out why...any way you could help...

  • @shashankrustagi9822
    @shashankrustagi9822 Před 2 měsíci

    The way Raj teaches, I am able to feel he is crazy for DSA and coding and he is the best teacher ever!!!
    That is what a true teacher needs to be.

  • @DeepakYadav-jc8mo
    @DeepakYadav-jc8mo Před rokem

    if I may ask, what tools do you use to make your videos, ipad and ipencil?

  • @santoshb7776
    @santoshb7776 Před 8 měsíci

    Understood sir !🙏🙏

  • @prabhakaran5542
    @prabhakaran5542 Před 3 měsíci +1

    Understood ❤

  • @RupamSasmalYt
    @RupamSasmalYt Před rokem +7

    Trust me I've just watch till 8:04 and thereafter I've solved till space optimization by my own! it's totally striver magic!! damn self confidence boost towards DP 🔥🔥

  • @JIGARSINGTHAKOR-yy6qp
    @JIGARSINGTHAKOR-yy6qp Před 3 měsíci +1

    awesome explanation but one thing is that you should have also need to explain why not performing operation on 2nd string

  • @adarshdubey1784
    @adarshdubey1784 Před 2 lety

    What will be the time complexity of recursive approach?

  • @himanshuagrawal8012
    @himanshuagrawal8012 Před 2 lety +2

    Amazing Problem , Solved by myself own a LEETCODE Hard ❤❤ !! #UNDERSTOOOOOOOOOOOOOOOOD
    I am back, gone for College Exams

  • @yuvrajbais1103
    @yuvrajbais1103 Před rokem

    excellent approach towards problem solving

  • @Zunan263
    @Zunan263 Před 2 lety

    Wow 182k subs getting a huge reach striver bro congrats , Happy and unsatisfied achievement 😅

  • @codizzz3614
    @codizzz3614 Před 2 měsíci

    understood!!

  • @manjunathg.s5967
    @manjunathg.s5967 Před rokem

    Understood!!Top notch!!

  • @mantrarajgotecha3055
    @mantrarajgotecha3055 Před 2 lety

    Wonderful explanation 😊😊

  • @052_a_sourabhpathak5
    @052_a_sourabhpathak5 Před rokem +1

    Started feeling confident now!!!!!!

  • @anchalsharma5777
    @anchalsharma5777 Před 11 měsíci +2

    It's impressive how you've explained this question not just for the sake of solving it, but to foster the development of problem-solving skills. I genuinely wish you achieve great heights in your life. Your efforts are truly appreciated. Today, I learned about your medical condition, and I'm amazed that despite that, you put in such dedicated efforts. Hats off to you!

  • @dayashankarlakhotia4943

    Saw tusar Roy dp table understood everything in space optimization

  • @pulkitchausali1354
    @pulkitchausali1354 Před rokem +1

    Striver dp is best in the universe I can say no one can match this level amazing series
    Dynamic Programming is like peace of cake
    ❤❤❤❤❤💯💯💯💯💯💯

  • @arihantjammar8888
    @arihantjammar8888 Před 9 měsíci

    Understood 😊