-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0139.py
More file actions
26 lines (20 loc) · 717 Bytes
/
0139.py
File metadata and controls
26 lines (20 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
seen = set()
def dfs(string):
if string in seen:
return False
for key in wordDict:
if key == string:
print(key, string)
return True
elif self.safe_index(string, key, 0):
if dfs(string[len(key):]):
return True
seen.add(string)
return dfs(s)
def safe_index(self, string, substring, index):
try:
return string.index(substring) == index
except:
return False