Skip to content

Commit 0138b06

Browse files
committed
word break solution
1 parent 7e37d94 commit 0138b06

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

word-break/devyejin.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
6+
def dfs(subs: str):
7+
if subs in memo:
8+
return memo[subs]
9+
10+
if not subs:
11+
return True
12+
13+
for word in wordDict:
14+
if subs.startswith(word):
15+
if dfs(subs[len(word):]):
16+
memo[subs] = True
17+
return True
18+
19+
memo[subs] = False
20+
return False
21+
22+
memo = {}
23+
return dfs(s)
24+

0 commit comments

Comments
 (0)