Skip to content

Commit 70de401

Browse files
committed
adding word break
1 parent 4d13e2a commit 70de401

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

word-break/daiyongg-kim.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
""" Failed Attempt
23
class Solution:
34
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@@ -10,4 +11,15 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
1011
return len(s) == 0
1112
"""
1213
class Solution:
13-
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
14+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
15+
word_set = set(wordDict)
16+
17+
dp = [False] * (len(s) + 1)
18+
dp[0] = True
19+
20+
for i in range(1, len(s) + 1):
21+
for j in range(i):
22+
if dp[j] and s[j:i] in word_set:
23+
dp[i] = True
24+
break
25+
return dp[len(s)]

0 commit comments

Comments
 (0)