-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path140.py
More file actions
executable file
·26 lines (26 loc) · 826 Bytes
/
140.py
File metadata and controls
executable file
·26 lines (26 loc) · 826 Bytes
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
class Solution:
wDict: List[str]
flag: bool
def dfs(self,current,last,tmpanswer):
if (current == ''):
if (last == ''):
self.answer.append(tmpanswer)
return
else:
return
tmp = last + current[:1]
if (self.wDict.get(tmp) != None):
self.dfs(current[1:],tmp,tmpanswer)
if (tmpanswer != ''):
tmpanswer += ' '
self.dfs(current[1:],'',tmpanswer+tmp)
else:
self.dfs(current[1:],tmp,tmpanswer)
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
self.wDict = {}
for each in wordDict:
self.wDict[each] = 1
self.answer: List[str]
self.answer = []
self.dfs(s,'','')
return self.answer