Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de6efba

Browse files
Word Break
1 parent 5015e88 commit de6efba

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

‎139-word-break.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
s and wordDict[i] consist of only lowercase English letters.
2828
All the strings of wordDict are unique.
2929
"""
30+
# Time Complexity: O(N^2)
31+
class Solution:
32+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
33+
wordDict = set(wordDict)
34+
35+
w = [False] * (len(s) + 1)
36+
w[0] = True
37+
38+
for i in range(1, len(s)+1):
39+
for j in range(i):
40+
if w[j] and s[j:i] in wordDict:
41+
w[i] = True
42+
break
43+
return w[-1]
44+
3045
# TLE - O (2^N)
3146
class Solution:
3247
def wordBreak(self, s: str, wordDict: List[str]) -> bool:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /