Autocomplete top-3
Standard · Write code · about 15 min
Each word in words has a usage count in counts at the same index. Given a prefix, return the three most used words that start with it, most used first. Break ties alphabetically. Return fewer than three words when fewer match. An empty prefix matches every word.
The words are all different.
Example 1
Input:
[["cat","car","cart","cap","dog"],[9,5,3,2,4],"ca"]
Output:
["cat","car","cart"]
cap is used least among the four ca words.
Example 2
Input:
[["cat","car","cart","cap","dog"],[9,5,3,2,4],"x"]
Output:
[]
No edge x from the root, so there is nothing to suggest.
Constraints
- 0 ≤ words.length ≤ 2,000, and counts.length equals words.length
- Each word has 1 to 20 lowercase letters; 1 ≤ counts[i] ≤ 10,000
- 0 ≤ prefix.length ≤ 20
Learn the idea first: Trie applications: autocomplete, word search, XOR maximum, a visual lesson.
More Trie applications: autocomplete, word search, XOR maximum problems
- Replace words Warm-up
- Maximum XOR pair Standard
- Trace the XOR walk Standard
- Word search on a board Challenge