Search Suggestions System
Given an array of product names and a search word typed one character at a time, after each character return up to three products from the list that share the currently typed prefix. When more than three match, return the three that are smallest in lexicographic order. Produce one such list for every prefix of the search word.
Open official problem prompt ↗For every prefix of the search word, list the (up to three) lexicographically smallest product names that begin with that prefix.
It works like a search box autocomplete: each keystroke drills one level deeper into a filing cabinet whose drawers are already sorted, and you read off the top three folders in the drawer you land in.
- Input
- products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
- Output
- [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
- Why
- After typing "m" and "mo" the three smallest matches are mobile, moneypot, monitor; once "mou" narrows it, only mouse and mousepad remain.
1 <= products.length <= 10001 <= products[i].length <= 30001 <= sum of products[i].length <= 2 * 10^4products[i] consists of lowercase English letters1 <= searchWord.length <= 1000searchWord consists of lowercase English letters