A shop has a list of product names, products, and a customer is typing searchWord into the search box one letter at a time.
After each letter, show up to three suggestions: products whose name begins with everything typed so far. When more than three products qualify, show the three that come first in dictionary order. Return one list of suggestions per typed letter, in typing order.
Example 1
Input: products = ["pear", "peach", "pea", "plum", "peanut"]
searchWord = "pear"
Output: [["pea", "peach", "peanut"], ["pea", "peach", "peanut"],
["pea", "peach", "peanut"], ["pear"]]
Why: sorted, the first three p-words are pea, peach, peanut;
only "pear" starts with "pear"2 more examples and the constraints
Example 2
Input: products = ["kiwi"], searchWord = "kiwis" Output: [["kiwi"], ["kiwi"], ["kiwi"], ["kiwi"], []] Why: "kiwi" matches until the fifth letter, which it does not have
Example 3
Input: products = ["cab", "car", "cow"], searchWord = "dog" Output: [[], [], []] Why: no product starts with "d"
Constraints
- 1 ≤ products.length ≤ 1,000
- 1 ≤ products[i].length ≤ 3,000, and all names together have at most 20,000 letters
- Product names are distinct
- 1 ≤ searchWord.length ≤ 1,000
- Every string holds only lower-case English letters
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Sort, then scan per prefixO(m · S)
- Trie with top threeO(S log n + m)
- Sort + shrinking windowO(S log n + m)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 1,000 products and a 1,000-letter search | Verdict |
|---|---|---|---|
| Sort, then scan per prefix | O(m · S) | Passes; re-reads everything | |
| Trie with top three | O(S log n + m) | Fast; heavy to build | |
| Sort + shrinking window | O(S log n + m) | Best; sort plus two pointers |
n = number of products, S = total letters in them, L = longest name, m = length of searchWord.
Edge cases to test
- No product matches. Every row is an empty list, one per letter:
[[], [], []]for a 3-letter search. - Matches run out partway. Once a prefix matches nothing, every longer prefix matches nothing too, but each still needs its empty row.
- A product is shorter than the prefix.
kiwistops matching atkiwis. Check the length before reading index i. - A product is a prefix of another.
carsorts beforecardandcards, so it is suggested first while it still matches.
Common mistakes
- Returning only the rows that have suggestions, instead of one row per typed letter.
- Forgetting to sort, or sorting by length, so the three suggestions are not the smallest in dictionary order.
- Reading
products[lo][i]without checking that the word is long enough.
How to talk through it
- 1Clarify. Are names unique? Do I return a row even when nothing matches? Is the order plain dictionary order?
- 2Give the direct version. Sort once, then scan every product for each prefix. Say it passes but repeats work.
- 3Spot the block. In sorted order the matches are contiguous and shrink as letters arrive, so two pointers can track them.
- 4Code the window. Move
loandhiinward on the i-th letter, then slice up to three words. Mention the trie as the design-scale option. - 5Test out loud. Run a search longer than every product and one that matches nothing before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?