forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-suggestions-system.py
118 lines (99 loc) · 3.47 KB
/
search-suggestions-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
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
111
112
113
114
115
116
117
118
# Time: ctor: O(n * l), n is the number of products
# , l is the average length of product name
# suggest: O(l^2)
# Space: O(t), t is the number of nodes in trie
import collections
class TrieNode(object):
def __init__(self):
self.__TOP_COUNT = 3
self.leaves = collections.defaultdict(TrieNode)
self.infos = []
def insert(self, words, i):
curr = self
for c in words[i]:
curr = curr.leaves[c]
curr.add_info(words, i)
def add_info(self, words, i):
self.infos.append(i)
self.infos.sort(key=lambda x: words[x])
if len(self.infos) > self.__TOP_COUNT:
self.infos.pop()
class Solution(object):
def suggestedProducts(self, products, searchWord):
"""
:type products: List[str]
:type searchWord: str
:rtype: List[List[str]]
"""
trie = TrieNode()
for i in xrange(len(products)):
trie.insert(products, i)
result = [[] for _ in xrange(len(searchWord))]
for i, c in enumerate(searchWord):
if c not in trie.leaves:
break
trie = trie.leaves[c]
result[i] = map(lambda x: products[x], trie.infos)
return result
# Time: ctor: O(n * l * log(n * l)), n is the number of products
# , l is the average length of product name
# suggest: O(l^2)
# Space: O(t), t is the number of nodes in trie
class TrieNode2(object):
def __init__(self):
self.__TOP_COUNT = 3
self.leaves = collections.defaultdict(TrieNode2)
self.infos = []
def insert(self, words, i):
curr = self
for c in words[i]:
curr = curr.leaves[c]
curr.add_info(i)
def add_info(self, i):
if len(self.infos) == self.__TOP_COUNT:
return
self.infos.append(i)
class Solution2(object):
def suggestedProducts(self, products, searchWord):
"""
:type products: List[str]
:type searchWord: str
:rtype: List[List[str]]
"""
products.sort()
trie = TrieNode2()
for i in xrange(len(products)):
trie.insert(products, i)
result = [[] for _ in xrange(len(searchWord))]
for i, c in enumerate(searchWord):
if c not in trie.leaves:
break
trie = trie.leaves[c]
result[i] = map(lambda x: products[x], trie.infos)
return result
# Time: ctor: O(n * l * log(n * l)), n is the number of products
# , l is the average length of product name
# suggest: O(l^2 * n)
# Space: O(n * l)
import bisect
class Solution3(object):
def suggestedProducts(self, products, searchWord):
"""
:type products: List[str]
:type searchWord: str
:rtype: List[List[str]]
"""
products.sort() # Time: O(n * l * log(n * l))
result = []
prefix = ""
for i, c in enumerate(searchWord): # Time: O(l)
prefix += c
start = bisect.bisect_left(products, prefix) # Time: O(log(n * l))
new_products = []
for j in xrange(start, len(products)): # Time: O(n * l)
if not (i < len(products[j]) and products[j][i] == c):
break
new_products.append(products[j])
products = new_products
result.append(products[:3])
return result