Design Search Autocomplete System - 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 '#'). You are given a string array sentences and an integer array times both of length n where sentences[i] is a previously typed sentence and times[i] is the corresponding number of times the sentence was typed.
For each input character except '#', return the top 3 historical hot sentences that have the same prefix as the part of the sentence already typed. Here are the specific rules:
- The hot degree for a sentence is defined as the number of times a user typed the exactly same sentence before.
- The returned top 3 hot sentences should be sorted by hot degree (The first is the hottest one).
- If several sentences have the same hot degree, use ASCII-code order (smaller one appears first).
- If less than 3 hot sentences exist, return as many as you can.
- When the input is a special character '#', it means the sentence ends, and in this case, you need to return an empty list.
Implement the AutocompleteSystem class:
AutocompleteSystem(String[] sentences, int[] times)Initializes the object with the sentences and times arrays.List<String> input(char c)This indicates that the user typed the character c. Returns an empty array [] if c == '#' and stores the inputted sentence in the system. Returns the top 3 historical hot sentences that have the same prefix as the part of the sentence already typed.
Input & Output
Example 1 — Basic Autocomplete
$
Input:
sentences = ["i love you", "island", "iroman", "i love leetcode"], times = [5,3,2,2], operations = ["i", "#"]
›
Output:
[["i love you","island","i love leetcode"], []]
💡 Note:
When user types 'i', system returns top 3 sentences starting with 'i' sorted by frequency: 'i love you'(5) > 'island'(3) > 'i love leetcode'(2). When '#' is typed, sentence ends and empty list is returned.
Example 2 — Progressive Typing
$
Input:
sentences = ["i love you", "island", "iroman"], times = [5,3,2], operations = ["i", " ", "a", "#"]
›
Output:
[["i love you","island","iroman"], ["i love you"], [], []]
💡 Note:
Type 'i': all 3 sentences match. Type ' ': only 'i love you' matches. Type 'a': no sentence starts with 'i a'. Type '#': end sentence, return empty.
Example 3 — New Sentence Added
$
Input:
sentences = ["i love you", "island"], times = [5,3], operations = ["i", "s", "#", "i", "s"]
›
Output:
[["i love you","island"], [], [], ["i love you","island","is"], []]
💡 Note:
First query 'i' shows existing sentences. After typing 'is#', new sentence 'is' is added with frequency 1. Next 'i' query now includes the new sentence 'is' as 3rd result.
Constraints
- 1 ≤ sentences.length ≤ 100
- 1 ≤ sentences[i].length ≤ 100
- sentences[i] consists of lowercase English letters and spaces.
- 1 ≤ times[i] ≤ 50
- At most 5000 calls will be made to input.
Visualization
Tap to expand
Understanding the Visualization
1
Input Data
Historical sentences with their frequencies
2
Build System
Organize sentences for efficient prefix-based retrieval
3
Query Process
Return top 3 hot sentences matching current prefix
Key Takeaway
🎯 Key Insight: Trie structure with pre-sorted top-k sentences at each node enables efficient autocomplete
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code