Design Search Autocomplete System — LeetCode 642 Python Solution
HardLeetCode PremiumDepth-First SearchDesignTrieStringData StreamSortingHeap (Priority Queue)
- Problem
- #642
- Pattern
- Trie
- Reading time
- 11 min
- Source
- leetcode.com
The problem
Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#').
Example
- Input
- ["AutocompleteSystem", "input", "input", "input", "input"]
- Output
- [null, ["i love you", "island", "i love leetcode"], ["i love you", "i love leetcode"], [], []]
- Explanation
- AutocompleteSystem obj = new AutocompleteSystem(["i love you", "island", "iroman", "i love leetcode"], [5, 3, 2, 2]);
Python solution
Python
class Trie:
def __init__(self):
self.children = [None] * 27
self.v = 0
self.w = ''
def insert(self, w, t):
node = self
for c in w:
idx = 26 if c == ' ' else ord(c) - ord('a')
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.v += t
node.w = w
def search(self, pref):
node = self
for c in pref:
idx = 26 if c == ' ' else ord(c) - ord('a')
if node.children[idx] is None:
return None
node = node.children[idx]
return node
class AutocompleteSystem:
def __init__(self, sentences: List[str], times: List[int]):
self.trie = Trie()
for a, b in zip(sentences, times):
self.trie.insert(a, b)
self.t = []
def input(self, c: str) -> List[str]:
def dfs(node):
if node is None:
return
if node.v:
res.append((node.v, node.w))
for nxt in node.children:
dfs(nxt)
if c == '#':
s = ''.join(self.t)
self.trie.insert(s, 1)
self.t = []
return []
res = []
self.t.append(c)
node = self.trie.search(''.join(self.t))
if node is None:
return res
dfs(node)
res.sort(key=lambda x: (-x[0], x[1]))
return [v[1] for v in res[:3]]
# Your AutocompleteSystem object will be instantiated and called as such:
# obj = AutocompleteSystem(sentences, times)
# param_1 = obj.input(c)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(n) auxiliary |
Pattern: Trie
Store a set of words by their shared prefixes so lookups cost the length of the word. LeetCode 642. Design Search Autocomplete System is filed here because LeetCode tags it Trie, which is the vocabulary this hub collects.
The trie guide has the Python template for the pattern and the 49 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 642. Design Search Autocomplete System?
- LeetCode 642. Design Search Autocomplete System is rated Hard on LeetCode.
- What is the time complexity of LeetCode 642. Design Search Autocomplete System?
- The Python solution on this page runs in O(n log n).
- What is the space complexity of LeetCode 642. Design Search Autocomplete System?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 642. Design Search Autocomplete System cover?
- LeetCode 642. Design Search Autocomplete System is tagged Depth-First Search, Design, Trie, String, Data Stream, Sorting and Heap (Priority Queue) on LeetCode.
- Is LeetCode 642. Design Search Autocomplete System a premium problem?
- Yes. LeetCode 642. Design Search Autocomplete System is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.