Skip to content

Commit 93b4acf

Browse files
committed
solve: word break
1 parent 6d1adcd commit 93b4acf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

word-break/evan.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
6+
dp = [False] * (len(s) + 1)
7+
dp[0] = True
8+
9+
for i in range(1, len(s) + 1):
10+
for j in range(i):
11+
# If the substring s[:j] is breakable and the substring s[j:i] is in the wordSet,
12+
# then the substring s[:i] is breakable
13+
if dp[j] and s[j:i] in wordDict:
14+
dp[i] = True
15+
break
16+
17+
return dp[len(s)]

0 commit comments

Comments
 (0)