A search box shows product suggestions as the user types. You get products, the names it can suggest, and typed, the word the user enters one letter at a time.
Each time a letter is added, look at everything typed so far. The box lists the products whose names begin with exactly those letters. It only has room for 3. When more match, it keeps the 3 that sort first alphabetically. Compare with plain string order, the same as a < b does for lowercase strings.
Write suggestProducts(products, typed). It returns an array with one entry per letter of typed. Entry i lists the suggestions shown after typed.slice(0, i + 1) was typed, in alphabetical order.
- Product names are distinct, and each is 1 to 30 lowercase English letters. There can be up to 20,000 of them, in any order.
typedis 1 to 1,000 lowercase English letters.- If no product starts with the letters typed so far, that entry is an empty array
[]. Every entry after it is also[].
Input: products = ["lamp", "laptop", "ladder", "lake", "lantern", "lemon", "lane"], typed = "lan"
Output: [["ladder", "lake", "lamp"], ["ladder", "lake", "lamp"], ["lane", "lantern"]]Six products start with "l", and only the first 3 alphabetically are shown. Two start with "lan".
Input: products = ["pear", "peach"], typed = "pex"
Output: [["peach", "pear"], ["peach", "pear"], []]