-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140.py
29 lines (27 loc) · 851 Bytes
/
140.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
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
if not wordDict:
return []
wordSet = set(wordDict)
maxWordLen = max(len(word) for word in wordSet)
mem = {}
def _wordBreak(s):
if s in mem:
return mem[s]
ans = []
if s in wordSet:
ans.append(s)
for i in range(1, min(maxWordLen + 1, len(s))):
left = s[:i]
if left in wordSet:
ans += [left + ' ' + w for w in _wordBreak(s[i:])]
mem[s] = ans
return ans
return _wordBreak(s)
solution = Solution()
print solution.wordBreak("catsanddog", ["cat","cats","and","sand","dog"])