-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_break.py
executable file
·110 lines (78 loc) · 2.46 KB
/
word_break.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Given an input string and a dictionary of words,
segment the input string into a space-separated
sequence of dictionary words if possible. For
example, if the input string is "applepie" and
dictionary contains a standard set of English words,
then we would return the string "apple pie" as output.
See : http://thenoisychannel.com/2011/08/08/retiring-a-great-interview-problem
"""
def segment_string_into_two(input_string, dictionary):
"""
Simple solution
Assumes that a string can only be broken into
2 words
"""
n = len(input_string)
for i in range(1, n):
prefix = input_string[:i]
if prefix in dictionary:
suffix = input_string[i:]
if suffix in dictionary:
return prefix + " " + suffix
def segment_string_recursive(input_string, dictionary):
"""
Recursive solution
Exponential complexity, eg. Consider a pathological dictionary
containing the words "a", "aa", "aaa", ...
"""
print "Input String = %s" % input_string
if input_string in dictionary:
return input_string
n = len(input_string)
for i in range(1, n):
prefix = input_string[:i]
if prefix in dictionary:
suffix = input_string[i:]
segmented_suffix = segment_string_recursive(suffix, dictionary)
if segmented_suffix:
return prefix + " " + segmented_suffix
return None
def segment_string(input_string, dictionary, memo):
print 'Input String = %s' % input_string
if input_string in dictionary:
return input_string
if input_string in memo:
return memo[input_string]
n = len(input_string)
for i in range(1, n):
prefix = input_string[:i]
if prefix in dictionary:
suffix = input_string[i:]
segmented_suffix = segment_string(suffix, dictionary, memo)
#memo[suffix] = segmented_suffix
print 'Memo = %s' % memo
if segmented_suffix:
return prefix + " " + segmented_suffix
memo[input_string] = None
return None
def segment_string_memoized(input_string, dictionary):
"""
Dynamic programming solution with memoization.
"""
memo = {}
out = segment_string(input_string, dictionary, memo)
return out
if __name__ == '__main__':
input_string = 'aaaaaab'
# dictionary containing vocabulary
dictionary = {
'aaa': 1,
'aaaa': 1,
'aaaaa': 1,
'aaaaa': 1,
'ab': 1
}
print "Simple Segmentation into two = %s" % segment_string_into_two(input_string, dictionary)
print "Recursive Segmentation = %s" % segment_string_recursive(input_string, dictionary)
print "Memoized Segmentation = %s" % segment_string_memoized(input_string, dictionary)