Тёмный
No video :(

Regular Expression Matching - Dynamic Programming Top-Down Memoization - Leetcode 10 

NeetCode
Подписаться 795 тыс.
Просмотров 157 тыс.
50% 1

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

 

23 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 188   
@NeetCode
@NeetCode 3 года назад
Dynamic Programming Playlist: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-73r3KWiEvyk.html
@sudheerk7989
@sudheerk7989 2 года назад
I can't thank enough for this video! Even after spending nearly 3 hours on various videos showing direct DP (bottom-up) solution, and countless posts in Discuss section with no clear explanation, I could not understand how the formulae are derived. But.... this is gem of a video and probably the only video on RU-vid explaining the Top-Down approach. In fact, it is better to try Top-Down approach in interviews. Directly trying to attack a problem with DP formula can lead to disasters. If the interviewer is not dumb expecting a specific answer involving DP, he/she/they should be okay with Top-Down+Memoization approach. Thanks again! Keep making such amazing videos.
@parambole8671
@parambole8671 3 года назад
If I crack my FB interviews it's only going to be because of this channel. Keep up the "NEET" work :)
@NeetCode
@NeetCode 3 года назад
Good luck, you're going to do great!
@Avinash_kumar_IITD
@Avinash_kumar_IITD 3 года назад
same for me, I cracked Microsoft India interview
@_ipsissimus_
@_ipsissimus_ 3 года назад
Hey boss, what coding problem did they ask you?
@yukselpolatakbyk8468
@yukselpolatakbyk8468 2 года назад
how did it go??
@RaghavSharma-nt3hr
@RaghavSharma-nt3hr 2 года назад
did u crack?? 😅
@jimwu3856
@jimwu3856 2 года назад
To be honest, I never would have even guessed that this is a Dynamic Programming problem. Once you started explaining the decision tree, it ALL made sense. Thanks for teaching me something new!
@TheRetrobek
@TheRetrobek 2 года назад
If someone didn't get the cache part, try printing out the (i, j) pairs with the following example: text="aaabaaa" and pattern = "a*b*a*". If you take a careful look, since we are backtracking, there are cases where we check the same (i, j) pairs. And that's why caching helps.
@adityaathota501
@adityaathota501 2 года назад
Ty so much, I was stuck trying to figure out where it was saving time
@80kg
@80kg 2 года назад
thank you for pointing out the test case it's important for understanding why caching is effective at saving time
@shalsteven
@shalsteven Год назад
how did you come with that example to identify the sub-problem?
@dollyvishwakarma2
@dollyvishwakarma2 2 года назад
OMG, the question looked so tough, but you made it soo easy !! Another awesome explanation. Great fan of your NEET coding style :)
@auto8ot644
@auto8ot644 2 года назад
Great explanation! I really like how you easily added memoization to the brute force algorithm. Other DP solutions were SO complicated. This explanation was perfect.
@abhimanyuambastha2595
@abhimanyuambastha2595 2 месяца назад
Maybe its just me but I found the drawing explanation very confusing although the code made more sense. Usually I love all of Neetcode’s explanations but the pointer usage threw me off around first. There’s a similar problem Wildcard Matching and I solved it by checking the current j index and not j+1, thats why the j+2 was confusing. But in the end the code is very similar. I recommend others to try the Wildcard Matching problem its Leetcode 40 or 41
@chinmaym92
@chinmaym92 3 года назад
I really appreciate how clean your code was and was so easy to understand
@xinchenyi9878
@xinchenyi9878 3 года назад
The clearest explanation I've seen!!! You are awesome :)
@hakoHiyo271
@hakoHiyo271 3 года назад
Great explanation! You explained the algorithm very clearly. I'm not even Python guy, but I still liked your video!
@eyosiasbitsu4919
@eyosiasbitsu4919 2 года назад
I'll have my google interview in October and this channel is really helping me in my preparation. if i pass then i'll be your biggest supporter neet!
@orogheneemudainohwo3493
@orogheneemudainohwo3493 2 года назад
How do you have an October interview scheduled in May? Full time or internship? Goodluck
@nealp9084
@nealp9084 2 месяца назад
did you pass?
@pranavkashyap8610
@pranavkashyap8610 Год назад
The dramatic decrease in time taken to execute after using cache surprised me . Thanks for the explanation and code
@chaoluncai4300
@chaoluncai4300 2 года назад
2 more observations/ optimized caches: 1. If j+1 is * and both of its dfs() returned false, we actually dont need to re-check the matching condition again since j+1 is a *, means dfs(i+1, j+1) will fo sho return false due to s[i+1] != p[j+1] which is a *. That being said, we can change the last if (match) condition to else if condition~ 2. current cache will only does its job while next state's dfs() returned false (any next state is true indicates a solution is found). By storing only false returned val into cache will reduce memory space Both opts are negligible / pretty insignificant since either of them only saved limited constant time/space
@michaeldunlap2693
@michaeldunlap2693 2 года назад
This is a fabulous and thorough explanation. The decision tree explanation and visual for the asterisk was what did it for me. Thank you!
@santoshr4212
@santoshr4212 10 месяцев назад
Thanks alot for helping me understand the issue, I was doing the bottom up and solution was failing in leetcode on last 2 test cases. After watching this video I understood we need to do bound check on index "i" - stupid of me missing such a basic thing. Now solution worked
@arpanbanejee5143
@arpanbanejee5143 2 года назад
Simply mind-blowing explanation, you made it so simple!! Thanks you! Please keep up the good work!
@for_whom_the_bell_tolls
@for_whom_the_bell_tolls 11 дней назад
Wow man your explanation is just fantastic!
@AbanoubAsaad-YT
@AbanoubAsaad-YT 2 года назад
BEST EXPLANATIONS ARE HERE :) Thank you so much for these quality videos!
@user-po5dk2xr7d
@user-po5dk2xr7d Год назад
This explanation makes the whole process clean and simple.
@Whatthetrash
@Whatthetrash 2 года назад
Thank you so much for this. After weeks (!) of trying to solve this problem on my own, I decided to look it up and lo and behold -- the solution involves a bunch of stuff that I have never heard of. I feel a bit better (and I need to learn about Dynamic Programming now). >_< Thanks again for the elegant, succinct solution.
@VishalKumar-kr9me
@VishalKumar-kr9me Год назад
How easy you make a hard problem is unbelievable !!!!!! Salute to you
@kanwarkajla
@kanwarkajla 2 года назад
great explanation .....also the vibe of ur videos is relaxing!
@buttercup5029
@buttercup5029 2 года назад
I was struggling a lot with this question. Your video cleared my doubts. Thanks for sharing!
@lootlook5148
@lootlook5148 3 года назад
WTF !! Bro You made clear in just 20 minutes🤩
@Iamnoone56
@Iamnoone56 2 года назад
Thanks Hokage for explaining it really well.Seen a lot of videos where people directly start drawing table.Will memoized soln be enough for an interview or do we have to give a bottom up soln too??
@somilsinghai5966
@somilsinghai5966 2 года назад
This question makes me appreciate the hard work of people who have created the full-blown regex.
@thinja
@thinja 2 года назад
Wow so much easier to understand than the DP solution! Thanks for showing your mistakes as well
@vdyb745
@vdyb745 2 года назад
What a brilliant solution and in-depth explanation for this seriously hard problem !!! WOW .... Thank you !!!!
@sauravchandra10
@sauravchandra10 Год назад
You make hard problems very easy. Thanks for explaining.
@saadkhan4049
@saadkhan4049 5 месяцев назад
You made it look so simple. Thank you!
@kocot.
@kocot. 2 месяца назад
Instead of optimizing it with cache, I've got an almost identical result (1300ms->50ms) by simply ensuring you don't process subsequent stars. So if there is a*a*a*, you'd only check it once. Now the cache seems obvious, but that addressed directly the most compute intensive cases :) Funny to see how it's same efficient. Using both would probably improve it even further.
@yukselpolatakbyk8468
@yukselpolatakbyk8468 2 года назад
You make this question easy to udnerstand. Thank you for all your amazing solutions!!
@shai1esh
@shai1esh Год назад
if this is the case below then : '*' Matches any sequence of characters (including the empty sequence). def isMatch(self, s: str, p: str) -> bool: # top down memo cache = {} def dfs(i, j): if(i, j) in cache: return cache[(i, j)] if i >= len(s) and j >= len(p): return True if j >= len(p): return False if p[j] == '*': # Check if the '*' matches an empty sequence or matches one or more characters cache[(i, j)] = dfs(i, j + 1) or (i < len(s) and dfs(i + 1, j)) return cache[(i, j)] match = i < len(s) and (s[i] == p[j] or p[j] == "?") if match: cache[(i, j)] = dfs(i + 1, j + 1) return cache[(i, j)] cache[(i, j)] = False return False return dfs(0, 0)
@jacques-dev
@jacques-dev Год назад
For anyone getting confused with the solution to this problem, I found the bottom-up approach to be much more intuitive. Just my experience, I know everyone is different.
@juliramoos
@juliramoos 5 месяцев назад
Perfect explanation! Thank you. And the code looks good.
@comander47
@comander47 Год назад
this problem took me soo much time to understand, thank you dude
@notsadsisyphus6224
@notsadsisyphus6224 2 года назад
i seriously love your video i swear you dont waste time i love it
@ruizhu5295
@ruizhu5295 2 года назад
I really appreciate your explanation, so clean, so logical!
@rahuljain281
@rahuljain281 6 месяцев назад
What an explanation sir ji❤. After this, I was able to solve wildcard matching by myself.
@yangyue2791
@yangyue2791 3 года назад
Yeah, that problem is so hard. I tried to use loops to compare character by character, and deal with * differently. Though, that way did not work well.
@capriworld
@capriworld 3 года назад
Best explanation ever found for this problem.
@meto4545
@meto4545 Год назад
I have a question, why the testcase - s="a", p=".*..a*" has to return false? I mean we just make ".*.." a 0 character string "" and the rest we make one "a". Edit: Nevermind I thought '.' can be a 0 character string since in the question it doesn't say anything about it.
@emretekmen1602
@emretekmen1602 8 месяцев назад
you need to explain how the cache actually saves time. How does storing the indexes along with a bool value save any time?
@Varukimm
@Varukimm Месяц назад
I struggled with this problem today without PD. In your example a = a*b* should give true because we use a and a* and remain with 0 b. Correct would be a = a*b
@MutuallyBro
@MutuallyBro Год назад
Very good explanation, love the channel!
@daved1113
@daved1113 2 года назад
Excellent work. Earned a sub. Keep it up.
@nishapaila7870
@nishapaila7870 Месяц назад
just a clean explanation amazing
@jonathanandres1657
@jonathanandres1657 8 месяцев назад
Great explanation! :) @NeetCode what tool do you use to write and draw ? Any specific brand, I'm looking for this kind of tools
@krugerbrent4270
@krugerbrent4270 2 года назад
Wow! You made it look like a piece 'o cake. Thanks, I really appreciate it.
@iharshgarg
@iharshgarg 7 месяцев назад
i checked so many videos of this problem.. but there's simply no compedition to you bro. keep the neet work up bro/>
@nagasivakrishna5660
@nagasivakrishna5660 Год назад
man love ur explanation ,great man ,no words love u man
@NAVNEET549
@NAVNEET549 2 года назад
Awesome explanation. Thank you so much
@saksham.malhotra
@saksham.malhotra 2 года назад
Thanks!
@NeetCode
@NeetCode 2 года назад
Thank you so much!
@pranavsharma7479
@pranavsharma7479 2 года назад
thnks bro i was not getting how to solve this , this is same as wildcard matching with a twist
@MaxFung
@MaxFung 2 года назад
thank god for this guy i was STUCK
@sachin_yt
@sachin_yt 2 года назад
I loved it man, thanks a lot :)
@xinniu3145
@xinniu3145 2 года назад
Thank you! this is owesome!
@Chirayu19
@Chirayu19 Год назад
I think the time complexity would be O(N^2M) and not O(N*M) since we have to iterate through the s in case of "*". Please do correct me if I am wrong. Thanks!
@tanaysingh5348
@tanaysingh5348 11 месяцев назад
the question description feels really incomplete without your explanation , thanks a lot
@dineshkannam8078
@dineshkannam8078 3 года назад
You are a legend.
@diveshkubal2936
@diveshkubal2936 28 дней назад
A correction: if j>=len(p): return i >= len(s) # If pattern is exhausted, return True only if string is also exhausted
@JameS00989
@JameS00989 Год назад
Super awesome explanation NeetCode 🎉Thanku
@jerryjohnthomas4908
@jerryjohnthomas4908 2 года назад
can some one explain what happens if i==m and j
@zl7460
@zl7460 Год назад
Thank you for the explanation. I thought according to the problem statement, 'c*' cannot be empty, which was very misleading by Leetcode.
@davidm3614
@davidm3614 5 месяцев назад
Great video! During your explanation of the examples I had one question, why does "a.." != "aa" if the character "." can represent zero or more of any character? I was thinking that the first period could be "a" and the second period can be empty therefore making aa = aa. Did I understand incorrectly?
@jimmyliu3101
@jimmyliu3101 4 месяца назад
Periods must represent a character, they can't be an empty string, only the star can be an empty string.
@aldogutierrez8240
@aldogutierrez8240 Год назад
Very good explanation thanks!
@Avuvos
@Avuvos 3 года назад
Great video and explanation thank you!
@mrpotatohasabanana
@mrpotatohasabanana 10 дней назад
Does this code cover TC where pattern ends with .* , like s="aab" p="aaa*.*" Iam getting false for such .* Ending cases where I need to ommit * but code returns false as we have passed s length
@taroserigano6546
@taroserigano6546 Год назад
Super thanks for this!
@z08840
@z08840 3 года назад
by switching "use" and "don't use" in or return statement you can significantly cut complexity down even without memoization
@shashwatmaru9238
@shashwatmaru9238 3 года назад
Hey what would be the Time and Space complexity ? Backtracking with memoization means 2^n?
@xyhan98
@xyhan98 Год назад
Hi NeetCode, not sure if LeetCode has added some new test cases, but seems like both top down and bottom up approaches are experiencing TLE for test case s = "aaaaaaaaaaaaaaaaaaab", p = "a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*". Other than that, thanks for your great explanation!
@eldercampanhabaltaza
@eldercampanhabaltaza Год назад
+1
@sumeetchawla3545
@sumeetchawla3545 2 года назад
Amazing explanation .. Really thankful for your great efforts.. :)
@NeetCode
@NeetCode 2 года назад
Glad it was helpful!
@haodantan2014
@haodantan2014 3 года назад
Best explanations! Thank you!
@NeetCode
@NeetCode 3 года назад
Thanks 😊
@emmatime2016
@emmatime2016 3 года назад
Great video!!!! Thank you!
@sungiv
@sungiv 2 года назад
this algorithm is not working for case s="aa" and p="*". since its checking for j+1 for * its not working..
@thisissheraf2633
@thisissheraf2633 2 года назад
Appreciate your explanation thank you
@purnimadas768
@purnimadas768 7 месяцев назад
Thank you for the very good video
@letscode1000
@letscode1000 3 года назад
Could anyone explain how the cache works, I understand the top-down, but don't understand memo as you said, it works well as memo, but when I trace the recursion, cannot see how the cache works?
@TheRetrobek
@TheRetrobek 2 года назад
Since we are backtracking, our explorations have a chance of repeating the same (i, j). Try printing out the (i, j) pairs with example as: text="cccbccc" and pattern = "c*b*c*"
@Tyokok
@Tyokok 3 года назад
Thanks for great explain! Do you also have an explain on complexity analysis of this recursive call? Thank you!
@sunginjung3854
@sunginjung3854 3 года назад
Thank you for the great video! Is there any chance you could also solve Minimum Difficulty of a Job Schedule (LC 1335)? I would really appreciate it.
@lingyunsu1589
@lingyunsu1589 2 года назад
Best explanation ever!
@biancafuu
@biancafuu 11 месяцев назад
Does anyone know why does the solution run into max recursion depth problem if I set the condition as : (dfs(i+1, j) and match) or dfs(i, j+2) for the dfs call instead of : dfs(i, j+2) or ((dfs(i+1, j) and match)) (I switch the order of the recursion call) 🤔
@nikosreg9710
@nikosreg9710 2 месяца назад
I think it's because you don't allow short-circuiting evaluation of your logical expressions to take place, saving you from the fact that there will always be one branch in your decision tree that accepts the star (left branch in the diagram in the video), thus leading to an infinite branch. Note: the fact that one branch of the tree can be infinitely extended is not false. That's the notion of "*" anyways. You just should make use of python's short-circuiting to avoid this infinite trap...
@danielsun1993
@danielsun1993 3 года назад
The best explanation!!!
@kaartiknagarajan5009
@kaartiknagarajan5009 Год назад
I've recoded your solution and it's brilliant. However, I've been trying to follow the stack trace of the program execution to convince myself when the first if statement would execute "if (i, j) in cache: return cache([i, j])". I know it does because I tried removing it and got a TLE error o Leetcode, but I don't see when it would be needed as we always progress into a deeper DFS and cache. Would appreciate an explanation if you could please! Thank you.
@anujdoshi882
@anujdoshi882 2 года назад
Amazing explanation!
@siddharthpandey8516
@siddharthpandey8516 2 года назад
This was perfect!
@MsSkip60
@MsSkip60 3 года назад
Awesome mate!
@yongquanrao7398
@yongquanrao7398 3 года назад
Great one. Thanks.
@ArpitDhamija
@ArpitDhamija 2 года назад
I tried your method. Did in C++, both recurssion and memoization but there was no big significant difference in the time. Gap was only 20ms. But there is huge time difference in your code. Hows that possible
@mayureshgharat1600
@mayureshgharat1600 3 года назад
Excellent tutorial !!! In case of P[ j + 1] = '*', why don't we check dfs(i, j+2) only when there is no match. IOW, why don't we do this : if (!match) { dfs(i , j+2) } else { dfs(i + 1, j) } why do we have to do: dfs(i, j + 2) even in case of a match?
@weiwang4045
@weiwang4045 2 года назад
Because it can be faster... in that, Consider 'a*a*a*aaa' match 'aaa', if we check dfs(i, j+2) even in case of current position match 'a' == 'a', the pattern would quickly skip all 'a*' cases, match the string with the tailing 'aaa' pattern, and return True whatsoever result returned from the other 'matched' side, because of the shortcut feature of OR operator. And in fact, on the 'matched' side, because a* would devour all matched letter 'a' and goes to the very end of the string '' , only to find '' is not matched and return False. So if you don't check dfs(i, j+2) before matched side is finished with a False, the OR expression have to wait for the other dfs(i, j+2) side result to determine the final boolean value. True || (whatsoever) vs False || True, obviously the former is faster. Just my opinion.
@80kg
@80kg 2 года назад
Aren't those two ways are same? and Wei Wang's explanation only can help with his example. In the case of "bc" "a*a*bc", it jump over to j + 2 because of not matched.
@MonkeyHitsDonkey
@MonkeyHitsDonkey 2 года назад
Can someone please explain how the time complexity is improved from O(2^n) to O(m*n) using a cache
@Terracraft321
@Terracraft321 2 года назад
"Bilguun If someone didn't get the cache part, try printing out the (i, j) pairs with the following example: text="aaabaaa" and pattern = "a*b*a*". If you take a careful look, since we are backtracking, there are cases where we check the same (i, j) pairs. And that's why caching helps."
@nikosreg9710
@nikosreg9710 2 месяца назад
I still don't get how cache helps us... Where is the backtrack step that would benefit from the use of a cache? Don't we use the indices' pair (i, j) only once and then move to the next character?🤔
@prithulbahukhandi1714
@prithulbahukhandi1714 3 года назад
bro good work. Can you give me 1 example where you are making use of pair stored in the cache. I don't get it, how are sub-problems repeating?
@AshishKumar-dk5ve
@AshishKumar-dk5ve 3 года назад
"cccbccc" "c*b*c*" (i j) cached values used in below cases 1 4 2 4 3 4
@prithulbahukhandi1714
@prithulbahukhandi1714 3 года назад
@@AshishKumar-dk5ve Thank you ashish got it!
@Demo-yc8fb
@Demo-yc8fb 2 года назад
what if s reaches the end but p does not? That will not always return a true right? Where did we cover that case?
@amitavamozumder73
@amitavamozumder73 3 года назад
can i do it just by using a stack? push any pattern and if found star removes all stack top char and pop it and move on. in case of .* we just push it in the stack and move on..
@pulakammalathy6968
@pulakammalathy6968 2 года назад
Thank you so much
@pinkylover911
@pinkylover911 2 года назад
you are talented
@tanayshah275
@tanayshah275 3 года назад
great explanation!
@ashkankipati
@ashkankipati Год назад
Could you explain wildcard matching, a similar problem to this one?
Далее
Jump Game - Greedy - Leetcode 55
16:28
Просмотров 230 тыс.
How I would learn Leetcode if I could start over
18:03
Просмотров 454 тыс.
5 Useful F-String Tricks In Python
10:02
Просмотров 297 тыс.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 181 тыс.
Where Does Bad Code Come From?
42:21
Просмотров 190 тыс.
Computer Science Is Not Software Engineering
14:55
Просмотров 63 тыс.
Regular Expression Dynamic Programming
18:34
Просмотров 254 тыс.