-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWordBreak.py
49 lines (40 loc) · 1.44 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
47
48
49
"""
@author Anirudh Sharma
Given a string s, and a dictionary of n words wordDictionary,
find out if A can be segmented into a space-separated sequence of dictionary words.
1 <= N <= 12
1 <= s <= 1000, where s = length of string A
The length of each word is less than 15.
"""
def wordBreakHelper(s, wordDictionary, lookup):
# Special case
if s is None or len(s) == 0:
return True
# If the subproblem is already solved
if s in lookup:
return lookup[s]
# Loop for every word in the dictionary
for word in wordDictionary:
# Check if the string starts with the
# current word
if s.startswith(word):
# Get the remaining string
suffix = s[len(word):]
if wordBreakHelper(suffix, wordDictionary, lookup):
lookup[s] = True
return True
lookup[s] = False
return False
def wordBreak(s, wordDictionary):
# Lookup for memoization
lookup = {}
return wordBreakHelper(s, wordDictionary, lookup)
if __name__ == "__main__":
s = "ilike"
wordDictionary = ["i", "like", "sam", "sung", "samsung", "mobile",
"ice", "cream", "icecream", "man", "go", "mango"]
print(wordBreak(s, wordDictionary))
s = "ilikesamsung"
wordDictionary = ["i", "like", "sam", "sung", "samsung", "mobile",
"ice", "cream", "icecream", "man", "go", "mango"]
print(wordBreak(s, wordDictionary))