forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
design-search-autocomplete-system.py
78 lines (63 loc) · 2.17 KB
/
design-search-autocomplete-system.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
# Time: O(p^2), p is the length of the prefix
# Space: O(p * t + s), t is the number of nodes of trie
# , s is the size of the sentences
import collections
class TrieNode(object):
def __init__(self):
self.__TOP_COUNT = 3
self.infos = []
self.leaves = {}
def insert(self, s, times):
cur = self
cur.add_info(s, times)
for c in s:
if c not in cur.leaves:
cur.leaves[c] = TrieNode()
cur = cur.leaves[c]
cur.add_info(s, times)
def add_info(self, s, times):
for p in self.infos:
if p[1] == s:
p[0] = -times
break
else:
self.infos.append([-times, s])
self.infos.sort()
if len(self.infos) > self.__TOP_COUNT:
self.infos.pop()
class AutocompleteSystem(object):
def __init__(self, sentences, times):
"""
:type sentences: List[str]
:type times: List[int]
"""
self.__trie = TrieNode()
self.__cur_node = self.__trie
self.__search = []
self.__sentence_to_count = collections.defaultdict(int)
for sentence, count in zip(sentences, times):
self.__sentence_to_count[sentence] = count
self.__trie.insert(sentence, count)
def input(self, c):
"""
:type c: str
:rtype: List[str]
"""
result = []
if c == '#':
self.__sentence_to_count["".join(self.__search)] += 1
self.__trie.insert("".join(self.__search), self.__sentence_to_count["".join(self.__search)])
self.__cur_node = self.__trie
self.__search = []
else:
self.__search.append(c)
if self.__cur_node:
if c not in self.__cur_node.leaves:
self.__cur_node = None
return []
self.__cur_node = self.__cur_node.leaves[c]
result = [p[1] for p in self.__cur_node.infos]
return result
# Your AutocompleteSystem object will be instantiated and called as such:
# obj = AutocompleteSystem(sentences, times)
# param_1 = obj.input(c)