139. Word Break
class Solution:
def concatenate_DFS(self, wordDict, x, vis):
if x == len(self.s):
return True
vis[x] = True
for word in wordDict:
if self.s[x:x+len(word)]==word and not vis[x+len(word)]:
if self.concatenate_DFS(wordDict, x+len(word), vis):
return True
return False
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
self.s = s
vis = [False] * (len(s)+1)
return self.concatenate_DFS(wordDict, 0, vis)