#1268
Search Suggestions System
specialist · 630 · lc medium +30 · verified · 65.1% accepted · 5,114 likes · top 69%
Description
You are given a string array products and a string searchWord. Build a type-ahead suggestion system: after each additional character of searchWord is typed, suggest up to three products whose names begin with the current prefix. If more than three products share the prefix, return only the three that are lexicographically smallest.
Return a list of lists where the ith list contains suggestions after the first i characters of searchWord have been 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"]]
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"].
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"].
After typing mou, mous and mouse the system suggests ["mouse","mousepad"].
Example 2:
Input: products = ["havana"], searchWord = "havana"
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
Explanation: The only word "havana" will be always suggested while typing the search word.
Code
1
2
3