After hours of searching for the solution, this came through for me
@amitpurohit88163 жыл бұрын
Spent 3 days searching for a good video explanation....and then found this....great and simple explanation....thank you:)
@manwinsingh73819 ай бұрын
Bro. You made my day. I was exhausted by this problem and couldn't find a correct explanation. Your dry runs and explanation were way too smooth. Thank you so much.
@drewlee74354 ай бұрын
literally THE best explanation i've seen
@KnapsackLabs4 жыл бұрын
Hello all, I've actually made a few mistakes in this video as many kind viewers have pointed out. I just want to note them here. 1) For the bottom up approach it would have been a good idea to convert the dictionary to a set first. This allows us to look up values in O(n) time. O(n) and not O(1) because in order to compute the hash for the string you have to go through the string char by char. 2) For time complexity of the bottom up and top down approach. Those should be O(n^3) technically (if we use a set for bottom up). It is because implicitly it cost O(n) to compare strings in the worst case (again because of the cost of computing the hash). 3) Furthermore the memoization solution should be O(N^2) space complexity as implemented, because we are storing the whole string; however, it is possible to come up with an O(N) space by simply storing a true false array corresponding to the string. Where memo[i] would be s[i...] subproblem can be solved. If you have any further questions or see more mistakes feel free to let me know. Sorry again for the mistakes and any confusion that I might have caused. I was counting things weirdly in my head at the time of making this video haha.
@neelgandhi66723 жыл бұрын
@Knapsak - for top down with memoization, shouldn't the time complexity be O(d * N^3)? As we saw, there can be N^2 unique recursive calls. For each call, we iterate over (potentially in worst case), all words from given dictionary - so the for loop runs at the most d times (d = length of Dictionary) - And for each of those d iterations, we do string slicing s[0:len(x)] - which in worst case takes O(N) time. Thus, for each of the N^2 recursive calls, we do O(d*N) amount of work. So the time complexity should be O(d * N^3), right? Can you please point out if I am missing something here? Thanks !!
@deepakkamble3602 Жыл бұрын
it might not work for overlaping dict string like s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] from leetcode i tried it it is not working please sugget me a solution for bottom up approch
@satyarthaprakash90514 жыл бұрын
One of the best solutions to this problem. Amazing step by step explanation!
@vikashkumarchaurasia12993 жыл бұрын
I am waiting for your next upcoming videos playlist . Please continue this series
@kant.shashi4 жыл бұрын
perfect explanation of word break problem .. understood the algo completely
@dhruvilpatel51354 жыл бұрын
Hello Mr.Knapsack...I think you are doing a great job and your explanations are crystal clear. Please make more such videos on DP with both recursive, top-down and bottom-up approaches as you have done till now.
@Babe_Chinwendum2 жыл бұрын
I support this
@shivangishukla26294 жыл бұрын
Amazing explanation! You should post more
@rodanm3 жыл бұрын
trueeeeee
@floatingpoint76294 жыл бұрын
I really like the way you dry run the recursive brute force method. Thanks for doing this. It was really helpful in understanding. Please keep doing dry run with example in your future videos too. Please keep them coming, and thanks again for high quality content.
@emersonmuriel50073 жыл бұрын
Thank you very much, this is the only video out of so many that I watched which actually helped me understand how to solve
@lavanya_m013 жыл бұрын
Best explanation!! Pls make more videos like this :)) It would be nice if you also provide a link from where you took this problem(like leetcode, hackerrank etc)
@vipinamar83233 жыл бұрын
You should do many more, definitely the best explanation on youtube for this problem and you just earned a subscription.
@JuanGonzalez-cl2fy3 жыл бұрын
This is the best explanation I've seen of this problem! Thank you so much for this wonderful video!
Can you please create the video for the extension of this problem Word Break II Leetcode 140
@TheJohnniePlays3 жыл бұрын
Thank you so much for this its really helpful with all these editing. I had the same approach its just i could figure out how to implement memoization. Those posts on leetcode discuss dont help at all they all just show code and try to explain in 2 sentances :S
@yawar1103 жыл бұрын
An excellent explanation and nicely presented too. Thumbs up for that. I do however a query. using brute force approach ....for the 4th example @0:50 where dict = [ "rac", "fast", "car", "racecar"] after the first iteration, "fast" will be removed and s becomes "racecar". the word "rac" is then used which makes s = "ecar" and this makes the result as false. I think im missing something here and would really appreciate if this could be explained - thanks alot!
@liuchen68702 жыл бұрын
you nailed it, keep doing man!
@MiddleEasternInAmerica4 жыл бұрын
awesome, thank you so much, please keep making videos
@bluesteel1 Жыл бұрын
Nice video
@vikashkumarchaurasia12993 жыл бұрын
Your videos are really awesome dude !!
@vennyroxz4 жыл бұрын
great explanation! best word break solution breakdown
@KnapsackLabs4 жыл бұрын
I'm glad you found it helpful!
@tryandcatchjeeves4 жыл бұрын
@Knapsak For the top down memoization solution, I don't understand why the space complexity is O(N); If we take your example of "abcd" at 9:33, I believe the memoized dictionary should contain the following subproblems: ['a', 'b', 'c', 'd', 'ab', 'bc', 'cd', 'abc', 'bcd', 'abcd']. If there are N^2 unique subproblems, then how is it we're using O(N) space worst-case?
@KnapsackLabs4 жыл бұрын
Yeah you are right my mistake. I was counting a single string as O(1). I think the subproblems are from a position to the end of the who string so for example. "abcde" would be ["bcde", "cde", "de", "e"] regardless still O(n^2). Sorry again for the mistake.
@tryandcatchjeeves4 жыл бұрын
@@KnapsackLabs No problem! First time coming to the channel / your videos. Thanks for making this video, it was very helpful!
@sunginjung38544 жыл бұрын
the best explanation I found on this problem. thank you
@vikashkumarchaurasia12993 жыл бұрын
great explaination !! Thanks a lot
@kovoorhareesh4 жыл бұрын
Very nice explanation. Thank you.
@ahmedouyahya4 жыл бұрын
Great explanation. Thank you so much
@rroy28122 жыл бұрын
excellent video
@HimanshuNegiHimNG4 жыл бұрын
@knapsak minor improvement in runtime if put break after dp[i]=True statement.
@kavyap71134 жыл бұрын
amazing work , well explained , good job
@abhilakshsharma12754 жыл бұрын
Awesome work done ✅ Best explaination I can found online for this problem 💯 Thanks for sharing with us 😁
@MedhatR-do9leАй бұрын
great explantation thanks
@yoshi49803 жыл бұрын
really good explanation! i was able to come up with the top down solution but i had trouble figuring out what the subproblems meant and just kind of got lucky with my thinking, but after the example you gave showing the duplicate subproblems, i think it makes more sense. for each subproblem, you ask "with the current string s(which may be a substring of the original input string), can i make this string using the other strings in the dictionary". does that sound right?
@ankursuri38534 жыл бұрын
Thanks for the explanation buddy! You rock.
@yashsingla54914 жыл бұрын
amazing solution
@johnromano21802 жыл бұрын
How is top-down runtime polynomial? Shouldn't it be linear since there's only one for loop with early returns for recursion over already computed subproblems?
@deepakkumarsharma40214 жыл бұрын
nice video, clear explanation along with time complexity, liked and subscribed!
@KnapsackLabs4 жыл бұрын
Thank you!
@electric3362 жыл бұрын
Good implementation. It would be helpful if you explained how you came up with the time complexity. You just say what it is without explaining.
@adarshjadhav88774 жыл бұрын
Thanks fo wonderful explanation
@sapnokiranii4 жыл бұрын
Great explanation, thanks!! 👍
@misoren96453 жыл бұрын
For the bottom up approach, can you share the intuition of how to come up with a decreasing j? Would an increasing j also work? it seems to me that yes. As at the end we would be asking if we can solve the subproblem [0...j] and string [j+1, ... i] being in the dictionary. And increasing j or decreasing it, seems to yield the same generic question. Or is there a reason of why to choose a decreasing j ? For me, it makes it look as in "harder" to come up with.
@dev_among_men3 жыл бұрын
Nice explaination, but I think dictionary would not be necessary for memoization and neither passing string U can use just index for recursive solution
@ShivamSingh-me1nb4 жыл бұрын
Thanks a lot dude ,what a explanation, i am able to solve the question
@KnapsackLabs4 жыл бұрын
Glad you found it useful!
@waynegreen79704 жыл бұрын
I enjoyed this video.
@akshatjain68544 жыл бұрын
Hey man! Great video. Just one doubt. how to decide which to use 2d matrix or 1d for dp
@KnapsackLabs4 жыл бұрын
This is a hard question to answer! I would say there isn't really any set way of knowing if a problem needs a 1d vs 2d dp table. Usually when I'm solving these problems, my general approach is to start with the recursive solution or the recurrence relationship. From there I ask myself "can I represent the subproblems in a 1d table or am I going to need more space with a 2d table" and usually I can figure it out from there. So my advice would be don't start with the dp table, start with solving the recurrence relationship and figuring out how the problem can use previous subproblems to solve the current one. You can then see if the previous subproblems you need can fit in a 1d array or if you need more space and need to instantiate a 2d array. Hopefully this helps!
@akshatjain68544 жыл бұрын
@@KnapsackLabs Thanks a ton! You cleared my doubts
@bruce70143 жыл бұрын
Can we say that the top down algorithm is O(N^3) because the for loop is O(N), and the substring processing(s[0:len(x)) also costs O(N), making the code inside the helper function O(N^2), but because it's recursive we multiply that by the for loop which is O(N), resulting O(N^3)?
@KnapsackLabs3 жыл бұрын
Yup thanks for noticing, if you look at the pinned comment, I explain my mistake and make the correction.
@bruce70143 жыл бұрын
@@KnapsackLabs Hey bro no worries, I've seen your pinned comment but even with that I wasn't able to fully understand the time complexity of the algorithm because of the for loop + a recursion inside of it. Thank you for the great content btw, explained details like your videos are extremely rare.
@ivandrofly7 ай бұрын
Thanks
@kentsang93763 жыл бұрын
Best
@Dan-tg3gs3 жыл бұрын
Trie with memo might be better for this problem
@aayushjain93394 жыл бұрын
Great !!!!...
@sagarguglani3724 жыл бұрын
sir i have a question.... at 15:19 in bottom up approach, you have mentioned that for i=2, j=0, s[j:i]='ab' But in list slicing, the list should be sliced from j(0) to i-1(2-1=2) so it must be 'a'... please answer
@KnapsackLabs4 жыл бұрын
Hello so I think where the confusion is coming from is distinguishing between the DP table and the string itself, notice the the corresponding values in the DP table are "shifted" by one to accommodate for the empty string base case. The string itself, s[0] = 'a', and s[2] = 'c'. Therefore when j=0 and i=2, s[j:i] = s[0:2] = 'ab'. Hope this clears things up! Feel free to follow up if there is still confusion! :)
@sagarguglani3724 жыл бұрын
@@KnapsackLabs thanku sir ....no confusions now...clear now
@shoooozzzz2 жыл бұрын
Day 37 of trying to understand DP tabulation. Result: I'm not getting any closer to reaching my goal of "getting it"
@shivanshrawat26884 жыл бұрын
Solution is wonderful but where is the link to code?
@KnapsackLabs4 жыл бұрын
Hi sorry there is no link to code at the moment. I'm working on making a website to display this; however, I've been delayed because I'm actually preparing for the interview process myself. I hope to have it up in a couple months. I apologise once again.
@adityarajora72193 жыл бұрын
where are you working or worked?
@KnapsackLabs3 жыл бұрын
I'm currently finishing my last year in college. I've done internships and will be starting full time at some of the larger tech companies. I don't want to say exactly which ones but they are among Facebook, Amazon, Apple, Microsoft, and Google.
@nawabsonu4 жыл бұрын
This problem is solved with multiple variants with one DP approach having O(N*S) where S is the length of the max string in the dict. I didn't understood the approach here www.geeksforgeeks.org/word-break-problem-dp-32/ and came to your video. The bottom-up approach resulted in O(N^2) in your solution. Can you have a look to the solution in the link? Thanks
@KnapsackLabs4 жыл бұрын
I tried looking at the solution in the link, and I also can't figure out what it is doing. Sorry I couldn't be of any help, best of luck!