Search Suggestions System

You are given an array of strings products and a string searchWord.

Design a system that suggests at most three product names from products after each character of searchWord is typed.

Suggested products should have a common prefix with the currently typed prefix of searchWord. If there are more than three products with a common prefix, return the three lexicographically minimum products.

Return a list of lists of the suggested products after each character of searchWord is typed.

Example 1
Inputproducts = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
Output[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
After sorting products lexicographically, the prefixes m and mo suggest the first three matching products, while mou, mous, and mouse suggest only mouse and mousepad.
Example 2
Inputproducts = ["havana"], searchWord = "havana"
Output[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
The only word havana is suggested after each character is typed.

Constraints

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • 1 <= sum(products[i].length) <= 2 * 10^4
  • All the strings of products are unique.
  • products[i] consists of lowercase English letters.
  • 1 <= searchWord.length <= 1000
  • searchWord consists of lowercase English letters.

Asked at 19 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate