-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
139.py
29 lines (29 loc) · 1.19 KB
/
139.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
dic = dict()
def helper(s):
if not s: return True
elif s in dic: return dic[s]
else:dic[s]=any(helper(s[len(w):]) for w in wordDict if w==s[:len(w)])
return dic[s]
return helper(s)
__________________________________________________________________________________________________
sample 12964 kb submission
class Solution:
def wordBreak(self, s: 'str', wordDict: 'List[str]') -> 'bool':
n = len(s)
if not wordDict:
return False
max_word_size = max([len(i) for i in wordDict])
wordDict = set(wordDict)
memo = [False] * (n+1)
memo[n] = 1
for i in range(n-1, -1, -1):
next_max = min(i+1+max_word_size, n+1)
for j in range(i+1, next_max):
if s[i: j] in wordDict and memo[j] == True:
memo[i] = True
return memo[0]
__________________________________________________________________________________________________