-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path140.Word-Break-II.py
50 lines (40 loc) · 1.11 KB
/
140.Word-Break-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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# https://leetcode.com/problems/word-break-ii/
#
# algorithms
# Hard (26.26%)
# Total Accepted: 141,443
# Total Submissions: 538,582
# beats 100.0% of python submissions
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
if len(wordDict) == 0:
return []
hash_map = {}
for word in wordDict:
hash_map[word] = True
length = len(s)
dp = [None] * (length + 1)
dp[0] = True
for i in xrange(1, length + 1):
tmp = []
for j in xrange(i):
if dp[j] is not None and s[j:i] in hash_map:
tmp += j,
if len(tmp) > 0:
dp[i] = tmp
if dp[-1] is None:
return []
res = [[]]
def recursive(idx, path):
if idx == 0:
res[0] += path.strip(),
return
for i in dp[idx]:
recursive(i, s[i:idx] + ' ' + path)
recursive(length, '')
return res[0]