-
Notifications
You must be signed in to change notification settings - Fork 481
/
0139.py
31 lines (25 loc) · 777 Bytes
/
0139.py
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
27
28
29
30
31
class Solution:
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
return self._wordBreak(s, set(wordDict), 0, set())
def _wordBreak(self, s, words, start, mem):
if start == len(s):
return True
if start in mem:
return False
for i in range(start + 1, len(s) + 1):
if i in mem:
continue
sub = s[start:i]
if sub in words and self._wordBreak(s, words, i, mem):
return True
mem.add(start)
return False
if __name__ == '__main__':
s = "applepenapple"
wordDict = ["apple", "pen"]
print(Solution().wordBreak(s, wordDict))