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 share a common prefix with the typed part of searchWord. If more than three products share that prefix, return the three lexicographically smallest. Return a list of lists of the suggested products after each character of searchWord is typed.

30:00

Search Suggestions System
medium
Free written solution
Topics
Companies

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 share a common prefix with the typed part of searchWord. If more than three products share that prefix, return the three lexicographically smallest. Return a list of lists of the suggested products after each character of searchWord is typed.

Example 1:
Input: {"products":["mobile","mouse","moneypot","monitor","mousepad"],"searchWord":"mouse"}
Output: [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
Constraints:
  • 1≤products.length≤10001 \leq \text{products.length} \leq 1000

  • 1≤products[i].length≤30001 \leq \text{products}[i].\text{length} \leq 3000

  • 1≤∑products[i].length≤2×1041 \leq \sum \text{products}[i].\text{length} \leq 2 \times 10^4

  • All the strings of products are unique.

  • products[i] consists of lowercase English letters.

  • 1≤searchWord.length≤10001 \leq \text{searchWord.length} \leq 1000

  • searchWord consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"products":["mobile","mouse","moneypot","monitor","mousepad"],"searchWord":"mouse"}

Sort the 5 products. In sorted order, all words sharing a prefix sit in one contiguous block, smallest first.

search box

🔍
mouse

products as given

0

mobile

1

mouse

2

moneypot

3

monitor

4

mousepad

matched prefix / top 3 suggestions

cut this step

suggestions per prefix

nothing typed yet

Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/22