Search Suggestions System

Medium#1268insert and search

There is a list of products and a word being typed. After each typed letter, suggest up to three products that start with the letters typed so far, the smallest in alphabetical order first. Return one list per letter. Our example: products [ox, on, of, or], typed "oxen" — the answer is [[of, on, or], [ox], [], []].

input
products: different words of lowercase letters, in any order; searchWord: the word being typed, letter by letter.
returns
A list for each typed letter: up to three products that start with the letters typed so far, the first ones in alphabetical order, sorted. A list is empty when no product fits.
constraints
  • 1 ≤ products.length ≤ 1000
  • 1 ≤ products[i].length ≤ 3000
  • 1 ≤ sum(products[i].length) ≤ 2·10⁴
  • all products are different
  • 1 ≤ searchWord.length ≤ 1000
  • products and searchWord are lowercase English letters
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
function suggestedProducts(products, searchWord) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more trie (prefix tree) problems

Pattern “Trie (prefix tree)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.