-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBreak.py
34 lines (28 loc) · 985 Bytes
/
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
class WordBreak(object):
def __init__(self, dictionary):
self.dictionary = dictionary
def breakWordMain(self, word):
output = []
if self.breakWord(word, output):
return output
else:
return "Cannot break word"
def breakWord(self, word, output):
match = ""
for i in range(len(word)+1):
match = word[0:i]
if match in self.dictionary:
output.append(match)
rest = word[i:]
if len(rest) > 0:
restResult = self.breakWord(rest, output)
if not restResult:
output.remove(match)
else:
return True
else:
return True
return False
wb = WordBreak(["I", "am", "this", "is", "Super", "Man", "dog", "cat"])
print(wb.breakWordMain("SuperManisMe"))
print(wb.breakWordMain("thisdogisSuper"))