-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_011.py
49 lines (37 loc) · 1.44 KB
/
problem_011.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
def add_to_trie(s, trie):
if not s:
return trie
character = s[0]
if character not in trie:
trie[character] = dict()
trie[character] = add_to_trie(s[1:], trie[character])
return trie
def get_dictionary_trie(dictionary):
trie = dict()
for word in dictionary:
trie = add_to_trie(word, trie)
return trie
def get_possible_completions(trie):
possible_completions = list()
for character in trie:
if trie[character]:
child_completions = get_possible_completions(trie[character])
for child_completion in child_completions:
possible_completions.append(character + child_completion)
else:
possible_completions.append(character)
return possible_completions
def get_autocomplete_suggestions(s, dictionary):
trie = get_dictionary_trie(dictionary)
current_trie = trie
for character in s:
if character not in current_trie:
return []
current_trie = current_trie[character]
completions = get_possible_completions(current_trie)
completions = [s + x for x in completions]
return completions
assert get_autocomplete_suggestions("de", ["dog", "deer", "deal"]) == ["deer", "deal"]
assert get_autocomplete_suggestions("ca", ["cat", "car", "cer"]) == ["cat", "car"]
assert get_autocomplete_suggestions("ae", ["cat", "car", "cer"]) == []
assert get_autocomplete_suggestions("ae", []) == []