1 class Solution(object):
 2     def wordBreak(self, s, wordDict):
 3         """
 4         :type s: str
 5         :type wordDict: Set[str]
 6         :rtype: List[str]
 7         """
 8         return findWords(0, len(s), s, wordDict, {})
 9 
10 def findWords(start, end, s, wordDict, cache):
11     if start in cache:
12         return cache[start]
13     cache[start] = []
14     candidate = ''
15     current = start
16     while current < end:
17         candidate += s[current]
18         current += 1
19         if candidate in wordDict:
20             if current == end:
21                 cache[start].append(candidate)
22             else:
23                 for x in findWords(current, end, s, wordDict, cache):
24                     cache[start].append(candidate + ' ' + x)
25     return cache[start]

 

posted on 2019-03-08 16:00  Sempron2800+  阅读(143)  评论(0编辑  收藏  举报