-
Notifications
You must be signed in to change notification settings - Fork 0
/
011.py
39 lines (27 loc) · 1003 Bytes
/
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
"""
Problem:
Implement an autocomplete system. That is, given a query string s and a set of all
possible query strings, return all strings in the set that have s as a prefix.
For example, given the query string de and the set of strings [dog, deer, deal], return
[deer, deal].
Hint: Try preprocessing the dictionary into a more efficient data structure to speed up
queries.
"""
from typing import List
from DataStructures.Trie import Trie
def get_suggestion(word_list: List[str], prefix: str) -> List[str]:
# using trie data structure to get the suggestions (for deatils, check
# ./DataStructres/Trie)
trie = Trie()
trie.add_words(word_list)
prefix_match = trie.get_suggestions(prefix)
# type casting the result to list as the return type is a set
return list(prefix_match)
if __name__ == "__main__":
print(get_suggestion(["deer", "dog", "deal"], "de"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
[n = total number of characters (all words)]
"""