Sorting · Easy
A command palette receives a small dictionary and needs a single batch of prefix suggestions. Duplicate dictionary entries must not repeat in the menu. Results need deterministic ASCII ordering rather than machine-dependent locale ordering or original insertion order.
Return at most k distinct dictionary words beginning with the exact prefix in ascending ASCII lexicographic order.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ words: string[], prefix: string, k: integer }
Fields
- words is a dictionary array; prefix is the exact beginning to match; k is the maximum result count.
- Strings contain printable ASCII characters only. Comparisons are case-sensitive; spaces count as characters.
Output
Return value
- Return distinct matching strings in lexicographic order: compare ASCII character codes, with a shorter matching prefix ordered first.
- k = 0 returns []; an empty prefix matches all dictionary entries, including the empty word.
Constraints
- 0 <= words.length <= 10000; total dictionary characters <= 200000; each word and prefix has at most 100 characters.
- 0 <= k <= 10000.
Examples
Example 1
{
"k": 2,
"prefix": "ap",
"words": [
"api",
"app",
"api",
"apple",
"cache"
]
}Returns
[
"api",
"app"
]Example 2
{
"k": 3,
"prefix": "",
"words": [
"b",
"A",
"a"
]
}Returns
[
"A",
"a",
"b"
]Required complexity
O(total characters + w log w × L) time and O(total characters + w) space for w words of maximum length L