Search Suggestions System - Problem
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 common prefix with 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.
Input & Output
Example 1 — Basic Case
$
Input:
products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
›
Output:
[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
💡 Note:
Typing 'm': products starting with 'm' are ["mobile","moneypot","monitor","mouse","mousepad"], first 3 lexicographically are ["mobile","moneypot","monitor"]. Typing 'mo': same products match 'mo' prefix, so same result ["mobile","moneypot","monitor"]. Typing 'mou': only ["mouse","mousepad"] match, so return ["mouse","mousepad"]. For 'mous' and 'mouse': same result ["mouse","mousepad"].
Example 2 — No Matches
$
Input:
products = ["havana"], searchWord = "havana"
›
Output:
[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
💡 Note:
Each character of 'havana' matches the product 'havana', so we return ['havana'] for each prefix.
Example 3 — Empty Results
$
Input:
products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
›
Output:
[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
💡 Note:
Typing 'b': ['baggage','bags','banner']. 'ba': ['baggage','bags','banner']. 'bag': ['baggage','bags']. 'bags': ['bags'].
Constraints
- 1 ≤ products.length ≤ 1000
- 1 ≤ products[i].length ≤ 3000
- 1 ≤ searchWord.length ≤ 1000
- products[i] and searchWord consist of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code