-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140.单词拆分-ii.py
37 lines (30 loc) · 867 Bytes
/
140.单词拆分-ii.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
30
31
32
33
34
35
36
#
# @lc app=leetcode.cn id=140 lang=python
#
# [140] 单词拆分 II
#
# @lc code=start
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
self.dic = {}
self.result = []
for i in range(len(wordDict)):
self.dic[wordDict[i]] = 1
self.mathWord(s,0,"")
return self.result
def mathWord(self,s,begin,sentence):
if begin >= len(s):
self.result.append(sentence)
for i in range(begin,len(s)+1):
if self.dic.get(s[begin:i]) ==1:
if sentence == "":
new_sentence = s[begin:i]
else:
new_sentence = sentence + " " + s[begin:i]
self.mathWord(s,i,new_sentence)
# @lc code=end