Imagine building the autocomplete system for a major search engine like Google! Your task is to create an intelligent system that suggests the most relevant search queries as users type.
You'll implement an AutocompleteSystem class that:
- Learns from historical search data - both the sentences and how many times each was searched
- Provides real-time suggestions as users type character by character
- Returns the top 3 hottest matching sentences based on search frequency
- Handles tie-breaking using ASCII order (lexicographical)
- Completes searches when users type the special character
'#'
Example: If historical data shows "i love you" was searched 5 times and "island" was searched 3 times, then typing "i" should return both suggestions, with "i love you" ranked higher due to its popularity.
The system must be responsive and intelligent, just like the autocomplete you use every day!
Input & Output
Visualization
Time & Space Complexity
m is prefix length to navigate Trie, k log k for priority queue operations on matching sentences
Trie storage where N is number of sentences, M is average length, ALPHABET_SIZE is character set size
Constraints
- 1 ≤ sentences.length ≤ 100
- 1 ≤ sentences[i].length ≤ 100
- 1 ≤ times[i] ≤ 50
- At most 5000 calls will be made to input
- Each character in input will be a lowercase English letter, space ' ', or '#'
- Sentences will only contain lowercase English letters and spaces