Skip to content

Commit 30e2a70

Browse files
Merge pull request #2162 from prograsshopper/main
[prograsshopper] Week 5 Solutions
2 parents 9be1134 + 2b62803 commit 30e2a70

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

word-break/prograsshopper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time complexity: O(N∗N∗M)
2+
# Space complexity: O(N)
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
5+
dp = [False] * (len(s) + 1)
6+
dp[0] = True
7+
wordSet = set(wordDict)
8+
9+
for i in range(1, len(s) + 1):
10+
for j in range(0, i):
11+
if dp[j]:
12+
if s[j:i] in wordSet:
13+
dp[i] = True
14+
break
15+
return dp[len(s)]

0 commit comments

Comments
 (0)