Word Break II(dp with memo)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
Example
Gieve s =lintcode,
dict =["de", "ding", "co", "code", "lint"].
A solution is["lint code", "lint co de"].
分析
带memo的dfs.用S 做cache的key
每次substr + x for x in self.dfs
import collections
class Solution:
"""
@param: s: A string
@param: wordDict: A set of words.
@return: All possible sentences.
"""
def wordBreak(self, s, wordDict):
# write your code here
cache = collections.defaultdict(list)
return self.dfs(s, wordDict,cache)
def dfs(self, s, wordDict, cache):
if s in cache:
return cache[s]
res = []
if s == '':
return ['']
for i in range(len(s)):
if s[:i+1] in wordDict:
ns = s[i+1:] #i+1这里,loop处理每个i
c= "" if ns=="" else " "
res += [s[:i+1] +c+ x for x in self.dfs(ns, wordDict,cache)]
cache[s] = res
return res先判断pos以后是否能用can[pos],利用这个在dfs里剪枝,同时f[i][j]来存储str[i,j]是否在字典里
记忆化搜索只能用s做key,不能用pos,因为str有star和长度,pos只有start
Last updated
Was this helpful?