Skip to content

Search Suggestions System

MediumTrees
Decode it first. Clues, constraints, pattern and plan before any code.

You are building the suggestion box of a shop's search bar. You are given a list of products and the searchWord a customer types, one letter at a time.

After each letter typed, suggest at most three products that start with what has been typed so far. If more than three match, pick the three that come first in alphabetical order. Return one list of suggestions per typed letter.

products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"], searchWord = "mouse"
→ [["mobile", "moneypot", "monitor"],
   ["mobile", "moneypot", "monitor"],
   ["mouse", "mousepad"],
   ["mouse", "mousepad"],
   ["mouse", "mousepad"]]
 
products = ["havana"], searchWord = "tatiana"  →  [[], [], [], [], [], [], []]

Constraints

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • 1 <= sum(products[i].length) <= 2 * 10^4
  • All products are unique and lowercase.
  • 1 <= searchWord.length <= 1000