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 minimums products.
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"]] 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.
Constraints:
1 <= products.length <= 1000
1 <= products[i].length <= 3000
1 <= sum(products[i].length) <= 2 * 104
products
are unique.products[i]
consists of lowercase English letters.1 <= searchWord.length <= 1000
searchWord
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you're typing a search query, and as you type, the system suggests products. The brute force way to do this means, for each letter you type, we check all the products to see which ones start with what you've typed so far. We then pick the top few suggestions to display.
Here's how the algorithm would work step-by-step:
def search_suggestions_system(products, search_word):
search_results = []
prefix = ""
for character in search_word:
prefix += character
matching_products = []
# Iterate over products to find those that start with the current prefix
for product in products:
if product.startswith(prefix):
matching_products.append(product)
# Sort the matching products alphabetically
matching_products.sort()
# Select the top 3 matching products (or fewer, if there aren't 3)
suggestions = matching_products[:3]
# Add the suggestions to the overall results
search_results.append(suggestions)
return search_results
The most efficient way to provide search suggestions is to first organize all the products alphabetically. Then, as someone types, we quickly narrow down the possibilities to only those products that start with what they've typed so far, returning only the first three matching results.
Here's how the algorithm would work step-by-step:
def search_suggestions(products, search_word):
products.sort()
result = []
search_string = ""
for char in search_word:
search_string += char
matching_products = []
# Only iterate products starting with the prefix
for product_name in products:
if product_name.startswith(search_string):
matching_products.append(product_name)
# Limit the number of suggestions to a maximum of 3
suggestions = sorted(matching_products)[:3]
result.append(suggestions)
return result
Case | How to Handle |
---|---|
products is null or empty | Return an empty list if the input products array is null or empty. |
searchWord is null or empty | Return an empty list if the searchWord is null or empty. |
products array contains empty strings | Filter out empty strings from the products list before processing. |
products array contains strings that don't start with a prefix of searchWord | The solution should correctly filter out products that do not match the prefix. |
searchWord is longer than any string in products | The solution should return the top 3 lexicographically smallest matches until the prefix is longer than any word. |
products array contains duplicate strings | The solution should handle duplicate strings correctly by including them in the ranked matches if they are among the top 3 lexicographically smallest. |
products array contains strings with mixed case | Convert both the products and searchWord to lowercase for case-insensitive matching. |
When there are fewer than 3 products that match a given prefix | Return all matching products for that prefix, even if fewer than 3. |