
Problem
Solution
Submissions
Search Autocomplete System
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 15
Design a search autocomplete system that supports the following operations:
Example 1
- Input: ["AutocompleteSystem", "input", "input", "input", "input"]
[[["i love you", "island", "iroman", "i love leetcode"], [5, 3, 2, 2]], ["i"], [" "], ["a"], ["#"]] - Output: [null, ["i love you", "island", "i love leetcode"], ["i love you", "i love leetcode"], [], []]
- Explanation:
- Step 1: Initialize the system with sentences ["i love you", "island", "iroman", "i love leetcode"] and their frequencies [5, 3, 2, 2].
- Step 2: User inputs 'i'. System returns ["i love you", "island", "i love leetcode"] sorted by frequency.
- Step 3: User inputs ' '. System returns ["i love you", "i love leetcode"] with the prefix "i ".
- Step 4: User inputs 'a'. No sentences have the prefix "i a", so return [].
- Step 5: User inputs '#' which indicates the end of a search. System records "i a" but returns [].
Example 2
- Input: ["AutocompleteSystem", "input", "input", "input", "input", "input"]
[[["hello world", "hello python", "coding is fun"], [5, 4, 3]], ["h"], ["e"], ["l"], ["l"], ["o"]] - Output: [null, ["hello world", "hello python"], ["hello world", "hello python"], ["hello world", "hello python"], ["hello world", "hello python"], ["hello world", "hello python"]]
- Explanation:
- Step 1: Initialize the system with sentences ["hello world", "hello python", "coding is fun"] and their frequencies [5, 4, 3].
- Step 2: User inputs 'h'. System returns ["hello world", "hello python"] sorted by frequency.
- Step 3: User inputs 'e'. System returns ["hello world", "hello python"] with prefix "he".
- Step 4: User inputs 'l'. System returns ["hello world", "hello python"] with prefix "hel".
- Step 5: User inputs 'l'. System returns ["hello world", "hello python"] with prefix "hell".
- Step 6: User inputs 'o'. System returns ["hello world", "hello python"] with prefix "hello".
Constraints
- The input sentences contain only lowercase English letters, spaces, and special characters like '#'.
- 1 ≤ sentences.length ≤ 100
- 1 ≤ sentences[i].length ≤ 100
- 1 ≤ times[i] ≤ 50
- 1 ≤ input.length ≤ 1000
- The character '#' marks the end of a search query.
- Time Complexity: Constructor O(k*l), Input O(p + q + mlog(m))
- Space Complexity: O(k*l)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a Trie data structure to efficiently store and search for sentences with a given prefix.
- Each Trie node should store a map of all sentences passing through that node along with their frequencies.
- Implement a priority queue or sorting mechanism to retrieve the top k matching sentences by frequency.
- Handle the special character '#' to indicate the end of a sentence and update its frequency.
- For efficient storage and retrieval, consider using a combination of a Trie and a HashMap.