We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 98db044 + 8db019c commit c3b61c4Copy full SHA for c3b61c4
DSA 450 GFG/WordBreak.py
@@ -0,0 +1,32 @@
1
+
2
+# Link : https://leetcode.com/problems/word-break/submissions/
3
4
+# Reference : https://www.youtube.com/watch?v=Sx9NNgInc3A
5
6
+# TC : O(mn)
7
8
+# Approach : https://somber-approval-8f1.notion.site/DSA-Solutions-34100a8ab92f42029011dcf591668343
9
10
11
+class Solution(object):
12
+ def wordBreak(self, s, wordDict):
13
+ """
14
+ :type s: str
15
+ :type wordDict: List[str]
16
+ :rtype: bool
17
18
19
+ dp = [False] * (len(s) + 1)
20
+ dp[len(s)] = True
21
22
+ for i in range(len(s) - 1 , -1 , -1):
23
+ for w in wordDict:
24
+ if((i + len(w)) <= len(s) and s[i : i + len(w)] == w):
25
+ dp[i] = dp[i + len(w)]
26
27
+ if(dp[i]):
28
+ break
29
30
+ return dp[0]
31
32
0 commit comments