forked from KDF5000/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordBreakII.py
52 lines (37 loc) · 1.33 KB
/
WordBreakII.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
# coding: utf-8
__author__ = 'devin'
class Solution(object):
def __init__(self):
self._sub_s_word_break = dict()
def joinWord(self, word, data=[]):
if len(data) == 0:
return [word]
retData = []
for d in data:
word = word + " " + d
retData.append(word)
return retData
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: Set[str]
:rtype: List[str]
"""
if s in self._sub_s_word_break.keys():
return self._sub_s_word_break[s]
word = ''
ret_list = []
s_len = len(s)
for i in range(s_len):
word += s[i]
if word in wordDict:
sub_sentence = self.wordBreak(s[i+1:], wordDict)
sentence = self.joinWord(word, sub_sentence)
ret_list.extend(sentence)
return ret_list
if __name__ == "__main__":
s = Solution()
str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
dic = ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
print s.wordBreak(str, dic)
# print s.wordBreak("cutsanddog", ["cut", "cuts", "sand", "and", "dog"])