-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWordBreak.py
46 lines (36 loc) · 1.2 KB
/
WordBreak.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
"""
@author Anirudh Sharma
Given a string s and a dictionary of strings wordDict, return true if s can be segmented
into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
"""
def wordBreakHelper(s, wordDict, start, maxLength, dp):
if start == len(s):
return True
if start in dp.keys():
return dp[start]
i = start
while i < (start + maxLength) and i < len(s):
newWord = s[start:i+1]
if newWord in wordDict and wordBreakHelper(s, wordDict, i + 1, maxLength, dp):
dp[start] = True
return True
i += 1
dp[start] = False
return False
def wordBreak(s, wordDict):
dp = {}
maxLength = 0
for word in wordDict:
maxLength = max(maxLength, len(word))
return wordBreakHelper(s, wordDict, 0, maxLength, dp)
if __name__ == "__main__":
s = "leetcode"
wordDict = ["leet", "code"]
print(wordBreak(s, wordDict))
s = "applepenapple"
wordDict = ["apple", "pen"]
print(wordBreak(s, wordDict))
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
print(wordBreak(s, wordDict))