Description
Given an array of products and a searchWord, after each character typed, return up to 3 product suggestions sorted lexicographically.
Examples
Input:
products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"Output:
[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]Explanation:
Matching products for each prefix.
Input:
products = ["bag","baggage","banner","box","cloths"], searchWord = "bags"Output:
[["bag","baggage","banner"],["bag","baggage"],["baggage"],[]]Explanation:
For prefix 'b': bag, baggage, banner all start with 'b'. For 'ba': bag, baggage start with 'ba'. For 'bag': only baggage starts with 'bag'. For 'bags': no products start with 'bags', so empty array is returned.
Input:
products = ["apple","application","apply"], searchWord = "app"Output:
[["apple","application","apply"],["apple","application","apply"],["apple","application","apply"]]Explanation:
All three products start with 'a', 'ap', and 'app' respectively. Since there are exactly 3 products and all match each prefix, the same sorted list [apple, application, apply] is returned for all three characters of the search word.
Constraints
- •
1 ≤ products.length ≤ 1000