forked from mx419/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-break-ii.py
48 lines (45 loc) · 1.45 KB
/
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
# Time: O(2^n)
# Space: O(n)
#
# 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.
#
# For example, given
# s = "catsanddog",
# dict = ["cat", "cats", "and", "sand", "dog"].
#
# A solution is ["cats and dog", "cat sand dog"].
#
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a list of strings
def wordBreak(self, s, dict):
n = len(s)
possible = [False for _ in xrange(n)]
valid = [[False] * n for _ in xrange(n)]
for i in xrange(n):
if s[:i+1] in dict:
possible[i] = True
valid[0][i] = True
for j in xrange(i):
if possible[j] and s[j+1:i+1] in dict:
valid[j+1][i] = True
possible[i] = True
result = []
if possible[n-1]:
self.genPath(s, valid, 0, [], result)
return result
def genPath(self, s, valid, start, path, result):
if start == len(s):
result.append(" ".join(path))
return
for i in xrange(start, len(s)):
if valid[start][i]:
path += [s[start:i+1]]
self.genPath(s, valid, i + 1, path, result)
path.pop()
if __name__ == "__main__":
print Solution().wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"])