-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path38.autoComplete.py
65 lines (54 loc) · 2.02 KB
/
38.autoComplete.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
# Source: https://www.youtube.com/watch?v=QGVCnjXmrNg
# Source: https://www.byte-by-byte.com/autocomplete/
# Problem: "Autocomplete"
# Example: Write an autocomplete class that returns all
# dictionary words with a given prefix.
# dict: {"abc", "acd", "bcd", "def", "a", "aba"}
# prefix: "a" -> "abc", "acd", "a", "aba"
# prefix: "b" -> "bcd"
# Brute-force Solution: Iterate over each word in dict
# Approach: Trie data structure (tree)
class Node():
def __init__(self, prefix):
self.prefix = prefix
self.children = {}
#Does this node represent the last character in a word?
self.isWord = False
class Solution():
def __init__(self):
self.trie = None
self.results = []
def constructTrie(self, dictionary):
self.trie = Node('')
for string in dictionary:
self.insertWord(string)
def insertWord(self, string):
# Iterate through each character in the string. If the character is not
# already in the trie then add it
currNode = self.trie
for i in range(len(string)):
if string[i] not in currNode.children:
currNode.children[string[i]] = Node(string[0:i+2])
currNode = currNode.children[string[i]]
if i == len(string) - 1:
currNode.isWord = True
def getWordsForPrefix(self, prefix):
self.results = []
curr = self.trie
for char in prefix:
if char in curr.children:
curr = curr.children[char]
else:
return self.results
self.findAllChildWords(curr)
return self.results
def findAllChildWords(self, node):
if node.isWord:
self.results.append(node.prefix)
for char in node.children.keys():
self.findAllChildWords(node.children[char])
if __name__ == '__main__':
sol = Solution()
sol.constructTrie(["abc", "acd", "bcd", "def", "a", "aba"])
print(sol.getWordsForPrefix("a"))
print(sol.getWordsForPrefix("d"))