🚀 neetcode.io/ - A better way to prepare for Coding Interviews 🥷 Discord: discord.gg/ddjKRXPqtk 🐦 Twitter: twitter.com/neetcode1
@8359s2 жыл бұрын
7:40 DUDE, I watched 3 other videos and couldn't grasp that part until I watched yours! Thank you.
@dollyvishwakarma22 жыл бұрын
In the top down memoization approach, a condition is missing when the input s1 and s2 are empty but we have some characters in s3: class Solution: def isInterleave(self, s1: str, s2: str, s3: str) -> bool: dp = {} if len(s1) + len(s2) != len(s3): return False def dfs(i, j): if i == len(s1) and j == len(s2): return True if (i, j) in dp: return dp[(i, j)] if i < len(s1) and s1[i] == s3[i+j] and dfs(i+1, j): return True if j < len(s2) and s2[j] == s3[i+j] and dfs(i, j+1): return True dp[(i,j)] = False return False return dfs(0,0)
@akshatsinghbodh30672 жыл бұрын
he wrote the condition for it if len(s1)+len(s2) != len(s3): return False
@vaibhavsoni24372 жыл бұрын
@@akshatsinghbodh3067 in bottom-up, not in top-down. Read carefully!
@arunsp767Ай бұрын
My question is, where is a situation where we save True into dp? I don't see dp[(I,j)] = True anywhere
@HalfJoked Жыл бұрын
This was super helpful. Here's an O(N) solution I made based off this, given we only have to track 2 rows of our grid at once. def isInterleave(self, s1: str, s2: str, s3: str) -> bool: if len(s1) + len(s2) != len(s3): return False dp, prev = [False] * (len(s2)+1), [False] * (len(s2)+1) for i in range(len(s1), -1, -1): for j in range(len(s2), -1, -1): dp[j] = False if i == len(s1) and j == len(s2): dp[j] = True if i < len(s1) and s1[i] == s3[i + j] and prev[j]: dp[j] = True if j < len(s2) and s2[j] == s3[i + j] and dp[j+1]: dp[j] = True dp, prev = prev, dp return prev[0]
@kvtysАй бұрын
thought you meant time complexity - was gonna say you should win a nobel prize lol
@arunsp767Ай бұрын
Ran your code and it took 25 passes to this test case abcd, efgh, abcsefgh
@pengtroll62473 жыл бұрын
Bro these are the best algorithms videos I have ever seen. I love the visuals and the step by step process in solving the problems. Keep it up!
@tnmyk_8 ай бұрын
I am glad you gave both top-down and bottom-up solutions. Thanks a lot!
@alokesh9852 жыл бұрын
This is how DP should be taught! Amazing stuff. Please keep up the good work. I would happily pay for a full-fledged DP course taught by you
@juliahuanlingtong67573 жыл бұрын
OMG!!!Though both iterate the strings from front to back and back to front would work, your technique here makes the base case way more sensable! Like the walk through from recursive all the way up to bottom up dp! ♥️♥️♥️
@iced_espresso_27172 жыл бұрын
How the solution could make sure | m - n |
@karlocehulic5156 ай бұрын
I think no metter how you arrsnge your substrings from s1 and s2 condition will be met. So therefor, in every case condition is already met.
@joy794193 жыл бұрын
I am just happy that the greatest leetcode content creator had his first sponsor
@meeradad7 ай бұрын
I am puzzled by the recurrent dfs solution. After each of the lines 12, 14 return True. But before returning True, should not th dp[(i,j)] be set to True for memoization just as in the return False case, dp[(i.j)] is set to False?
@AnandKumar-kz3ls2 жыл бұрын
the example in the video was the test case that get failed by my code. your channel is gold keep it up
@sanjai_rs74 ай бұрын
Bro you are everywhere
@licokr8 ай бұрын
I've tried to write code without seeing your code, but of course, the logic is the same as yours though. I can't say that I fully understand, but I know it will keep better. Without your videos, I may've just moved on to a next question. Thank you!
@kareemadesola65222 жыл бұрын
Using the memoization technique, you could just use an hash set since it is always going to return False
@shrn10 ай бұрын
Good !dea
@MrShag232 ай бұрын
this might be the best dp explanation I've seen so far ngl
@BirajDahal-z6u10 ай бұрын
Did actually try to solve this question with two pointers first, and had a fall realizing its tough to decide which strings to take
@laksh90847 ай бұрын
why is he checking i < len(s1) when we are already iterating from s1 to 0 ... what's the point here?
@jayjw15 ай бұрын
We do this because if we check for the condition dp[i + 1][j] we will be checking an out of bounds array position which will throw an error in python..
@MianyunNi Жыл бұрын
I am obsessed with your explanation! You become my no.1 idol
@PedanticAnswerSeeker7 ай бұрын
here is the way if you want to do it bottom up if len(s1) + len(s2) != len(s3): return False # Create a 2D array to store the results of subproblems dp = [[False] * (len(s2) + 1) for _ in range(len(s1) + 1)] # Base case: Empty strings interleave to form an empty string dp[0][0] = True # Fill in the first row (s1 is empty) for j in range(1, len(s2) + 1): dp[0][j] = dp[0][j - 1] and s2[j - 1] == s3[j - 1] # Fill in the first column (s2 is empty) for i in range(1, len(s1) + 1): dp[i][0] = dp[i - 1][0] and s1[i - 1] == s3[i - 1] for i in range(1, len(s1) + 1): for j in range(1,len(s2) + 1): dp[i][j] = (dp[i - 1][j] and s1[i - 1] == s3[i + j - 1]) or \ (dp[i][j - 1] and s2[j - 1] == s3[i + j - 1]) return dp[len(s1)][len(s2)]
@yassineacherkouk Жыл бұрын
I spend 45 min trying to figure out the tabulation solution but i can't your a hero men.
@pravinchoudhari3533 ай бұрын
The only diversion in this questions was to make sure that we use s1 and s2 alternatively to that |m-n|
@manpreetsinghrana77519 ай бұрын
In the memoization soln, dp[i][j] is never set to true.
@starstarhaha7 ай бұрын
yes
@kywei74852 жыл бұрын
Excellent explanation! The problem is really complicated, but your video explains really well!
@vedantshinde22773 жыл бұрын
That explanation with choice diagram was on point!
@ashisranjandey754110 ай бұрын
How are we making sure that it is not a concatenation? (a+b) = c? we have to check in base case . right?
@shuvbhowmickbestin Жыл бұрын
what's the need of using a hashmap when all we're storing in it for the keys are false values? Can't we use a set instead and whenever the element is present in the set can we not return false?
@raylei47002 жыл бұрын
Thanks for the awesome explanation. Can you elaborate more on how is this condition "|m - n|
@mohinishdaswani9836 Жыл бұрын
No matter how you divided the strings to form the target string, this condition will always be met.
@consistentthoughts8263 жыл бұрын
Hi Bro If you do videos on system design it will be amazing
@adityan53022 жыл бұрын
Excellent memoization solution. Loved it
@НикитаБаринов-р6г5 ай бұрын
How does this solution take into account the fact that |n - m|
@dragonthis79485 ай бұрын
hi can i ask how did u take into account that |m-n| < 1 is accounted for in the algorithm?
@sudheerk93472 жыл бұрын
Sad to see this as Medium. This is definitely Hard for the most optimized solution (i.e., DP). Though it is a medium for DFS (memoization). I did some search in internet on Memoization and DP. Memoization = O(n^2), DP = O(n) time. It would be great if you can make a video on time complexity difference between Memoization and DP in general.
@rahulnegi456 Жыл бұрын
how is DP O(n) ? we have two full for loops there.
@mohithadiyal60832 жыл бұрын
The best explanation I can ever find 😁😁👍🏻
@willw4096 Жыл бұрын
4:50 6:33 10:20
@begula_chan7 ай бұрын
Just thanks, that's the best solution
@Sulerhy7 ай бұрын
Decision tree! you're the best
@bigrat51012 жыл бұрын
Hi NC, can I ask, why we have to cache the False to dp{} not just simply return the False then end the recursion?
@shuvbhowmickbestin Жыл бұрын
yeah same question.
@venkyvids2 жыл бұрын
How can this solution be extended for when s3 is an interleaving of loops s1 and s2?
@ChanChan-pg4wu2 жыл бұрын
Top notch solution and explanation! I have solved this question before but keep forgetting the correct thinking process. I guess more practice is the way to go. But on top of that, any suggestions to keep the learning in our brain rather than repeated revisiting?
@himanshut1172 жыл бұрын
I do struggle with this alot. I guess there is thing, "Actually understanding the problem and fooling yourself that you understood it". If u have understood the logic behind maybe that can hit you in any question you come up with. That is I guess people do CP in codeforces cause you get to experience lot of new problems. While solving these problems think you are collecting tools (logics, thinking process, what thought made u take wrong path for the problem, the patterns etc). This will 100% help you future problems.
@IK-xk7ex Жыл бұрын
Hello, the proposed Backtracking solution has a mistake. It will return wrong result 'true' for a test case s1="a", s2="", s3="c"
@dumbfailurekms Жыл бұрын
if len(s1) + len(s2) != len(s3): return False he said that at the start of the video too
@vatsalsharma57913 жыл бұрын
Best❤️ Plzz try to make more videos on hard algorithms like dp, recursion, backtracking and graph algo Thankyou so much ❤️
@algorithmo1343 жыл бұрын
Hi NeetCode can you do a lecture on algorithm design and analysis? Thank you so much for your hard work!
@Martelagelo Жыл бұрын
FYI your DFS solution does not pass all test cases. I get the point of this video is to highlight the DP solution but you shouldn't show incorrect code. Case that does not pass: s1: "" s2: "" s3: "a"
@CharlesGreenberg0009 ай бұрын
Simple solution: add this before the call to dfs() if len(s1)+len(s2)!=len(s3): return False
@raylin2527Ай бұрын
Love this video!!!
@ersinerdem72852 жыл бұрын
Are these 2D true DP solutions really expected to come up in an interview? It just sounds too much at the current state of my mind power :)
@athenalin6360 Жыл бұрын
Why I= len(s1) and j =len(s2) return true? What if the first character does not match either one?
@dumbfailurekms Жыл бұрын
Then it will return false because i and j start 0..
@tushartiwari79292 жыл бұрын
Ninja level teaching❣
@wanderer49542 ай бұрын
I watched this solution in 0.75 speed. still had to rewatch it.
@themagickalmagickman Жыл бұрын
Could someone explain why this doesnt work, it appears to do the same exact thing? setting dp[i][j] = dp[i+1][j] should be the same as setting it to True or False as needed, right? dp = [[False for j in range(len(s2)+1)] for i in range(len(s1)+1)] dp[-1][-1] = True for i in range(len(s1),-1,-1): for j in range(len(s2),-1,-1): if i < len(s1) and s1[i] == s3[i+j]: # THIS LINE IS CHANGED dp[i][j] = dp[i+1][j] if j < len(s2) and s2[j] == s3[i+j]: # THIS LINE IS CHANGED dp[i][j] = dp[i][j+1] return dp[0][0]
@kunindsahu9743 ай бұрын
I'd recommend putting your question to Claude, I had the same question and it gave me a whole lot of clarity.
@kunindsahu9743 ай бұрын
A small snippet from Claude - "The issue with the incorrect version is that it doesn't consider both possibilities simultaneously. Here's why: In the correct version, you're checking if the current character matches s1 AND if it's possible to form the string up to the previous character using s1. The same logic applies for s2. In the incorrect version, you're only checking if the current character matches, and then directly assigning the previous state. This doesn't account for cases where both s1 and s2 could potentially contribute to the current position. The correct version uses True assignment, which allows for the possibility that either s1 or s2 could work. The incorrect version always assigns the previous state, which might inadvertently overwrite a valid solution."
@Ben-pb7ct2 жыл бұрын
Anyone can modify Java’s solution from bottom-up to top-down? I don’t find the reason why we need to do it reversely?
I think your code is not working in leetcode, it is failing with case s1="a", s2="" , s3="c", try and let me know
@tsunghan_yu2 жыл бұрын
Just add this check: if len(s1) + len(s2) != len(s3): return False
@서울_9반_김노아3 жыл бұрын
wow
@glenfernandes52483 жыл бұрын
The recursive soln. fails leetcode testcases.
@KeshavKumar694203 жыл бұрын
Just add a check for sum of length of str1 and str2. If the sum is not equal to length of str3 then return False. That check was present in bottom up soln.
@alokesh9852 жыл бұрын
The regular recursive soln will time out. However, the memoization actually was faster than DP for me in leetcode