Word Break II - Leetcode 140 - Python

  Рет қаралды 15,339

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 25
@rostyslavmochulskyi159
@rostyslavmochulskyi159 6 ай бұрын
Thank you! Outdated: I believe these sections should be Cache/Memo, not Backtracking as sections 2-3 8:37 - Backtracking Explanation 15:08 - Backtracking Coding
@pastori2672
@pastori2672 6 ай бұрын
i saw people brag about solving this problem but all thanks to you i was able to solve it like it was an easy Thank you bro ❤
@tusov8899
@tusov8899 6 ай бұрын
Build same DFS + Memoization like your flow but your explanation makes me realize I can totally remove the extra work like creating a preprocessed DP array that DP[i] checks s[i:] is breakable or not. If that was used, it could be added as a condition before Line 16 in your code. Again, thank you so much NC
@deathbombs
@deathbombs 6 ай бұрын
Approach 1: "using up"letters, then use remainder" backtracking from where we're at Sln 2: BFS check if word fits at current index
@Mappy13Neb
@Mappy13Neb 6 ай бұрын
Solution that uses your previous Word Break solution: class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: dp = [[False, []] for _ in range(len(s) + 1)] dp[len(s)] = [True, [""]] for i in range(len(s) - 1, -1, -1): for word in wordDict: if i + len(word)
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 6 ай бұрын
Isn't substring is an O(n) operation? So I think total time complexity is N * 2^n. What do you think?
@ItachiUchiha-xi7ox
@ItachiUchiha-xi7ox 6 ай бұрын
yeah but N can be max up to 20 so N*2^n will also work fine
@thelindayz2087
@thelindayz2087 6 ай бұрын
It's not really O(2^n), it is O(n*2^n) because you do s[i:j] which takes O(n) time in the worst case
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 6 ай бұрын
Btw word by word solution might be inefficient if we have same words 1000 times. Suppose string is "catscats" (or longer) and words are [cats, cats, cats.......] (1000 or 100000) So we need to have multiply big o with wordDict size. But if we do substring approach since we have set, we are safe
@1vader
@1vader 6 ай бұрын
The description says all given words are unique. Also, even if not, you can use a set with that approach as well. Though checking 1000 words vs max 20 substrings is still less efficient in practice but I guess not in big O.
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 6 ай бұрын
Thank you. Yes I read the constraints but I was talking about general comparison between them. As you said 1000 words is less efficient and it depends on string length and word dict size. If word dict size are small and unique, word by word is efficient than substring approach but if string length is small and word dict size are big or not unique then substring approach is more efficient
@antondonohue8943
@antondonohue8943 6 ай бұрын
Is there a reason you prefer to have state saved outside of your recursive function? Like in this case you have to append and pop from cur. I think it would be neater if made cur and argument `backtrack(i, cur + [w])` or something. On another note I am very grateful for what you're doing. You've helped me a lot
@janesun3701
@janesun3701 12 күн бұрын
is the join function also o(n) since at worst we'd join every character with a space?
@johnniewalkerjohnniewalker2459
@johnniewalkerjohnniewalker2459 6 ай бұрын
Great work!
@aashishbathe
@aashishbathe 6 ай бұрын
Can you think of / give us a trie based solution? Topics suggested it can be done with trie?
@sarthakjohnsonprasad3909
@sarthakjohnsonprasad3909 6 ай бұрын
Store all the words in the trie and then dfs?
@MP-ny3ep
@MP-ny3ep 6 ай бұрын
Thank you for this
@Nishit_369
@Nishit_369 6 ай бұрын
So, word break 1 was a decision problem and word break 2 is an enumeration problem. Right?
@Antinormanisto
@Antinormanisto 6 ай бұрын
I don't understand why I couldn't come to the first solution... I'm feel depresion
@rahulnegi456
@rahulnegi456 6 ай бұрын
lol same it was fairly simple and easy
@chien-yuyeh9386
@chien-yuyeh9386 6 ай бұрын
First🎉
@ritikaagrawal769
@ritikaagrawal769 6 ай бұрын
why have a nested function when you can use recursion on the given one...? For this specific problem, i found that the most basic(recursive) solution works just as good as a backtracking. but what do i know... def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: if not s : return [ ] res = [ ] for i in range(len(s)-1) : if s[:i+1] in wordDict : ans = s[:i+1] output = self.wordBreak(s[i+1:],wordDict) for out in output : if out : res.append(ans+" "+out) if s in wordDict : res.append(s) return res
@momenwadood1342
@momenwadood1342 6 ай бұрын
Please add the difficulty of problems in the thumbnail or title
@pranithtirumalsetti1453
@pranithtirumalsetti1453 6 ай бұрын
Second🎉
@nikhil199029
@nikhil199029 6 ай бұрын
i chose xxx
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 9 М.
N-Queens - Backtracking - Leetcode 51 - Python
17:51
NeetCode
Рет қаралды 180 М.
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 6 МЛН
Pervasive Computing and Android Development #appdevelopment #java #android
7:46
𝓓𝓮𝓬𝓸𝓭𝓮 𝓤𝓢
Рет қаралды 18
Integer to English Words - Leetcode 273 - Python
20:59
NeetCodeIO
Рет қаралды 15 М.
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 14 М.
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
Word Break - Dynamic Programming - Leetcode 139 - Python
15:35
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 18 М.
Shortest Subarray with Sum at Least K - Leetcode 862 - Python
27:57
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Word Search II - Backtracking Trie - Leetcode 212 - Python
20:54
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН