forked from brownlzw/Leetcode-python-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path139. Word Break.py
More file actions
51 lines (49 loc) · 1.26 KB
/
Copy path139. Word Break.py
File metadata and controls
51 lines (49 loc) · 1.26 KB
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
51
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
def word_search(dp, s, pos, words):
if pos == len(s):
return True
if pos in dp:
return dp[pos]
dp[pos] = False
for i in xrange(pos, len(s)):
if s[pos:i + 1] in words and word_search(dp, s, i + 1, words):
dp[pos] = True
break
return dp[pos]
dp = {}
w_set = set(wordDict)
return word_search(dp, s, 0, w_set)
# def wordBreak(self, s, wordDict):
# """
# :type s: str
# :type wordDict: List[str]
# :rtype: bool
# """
# dic = {}
# for word in wordDict:
# if not len(word) in dic:
# dic[len(word)] = Set()
# dic[len(word)].add(word)
# dp = {}
# self.isValid(s, 0, dic, dp)
# return dp[0]
#
# def isValid(self, s, index, dic, dp):
# if index == len(s):
# return True
# if index in dp:
# return dp[index]
# for key, wordSet in dic.iteritems():
# if key > len(s) - index:
# continue
# if s[index:index + key] in wordSet and self.isValid(s, index + key, dic, dp):
# dp[index] = True
# return True
# dp[index] = False
# return False