Search Suggestions System - Complete Solution Guide
Search Suggestions System is LeetCode problem 1268, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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","mou
Detailed Explanation
The problem requires us to design a search suggestion system. Given a list of product names (`products`) and a search word (`searchWord`), the system should suggest at most three product names from the `products` list after each character of `searchWord` is typed. The suggested products must have a common prefix with the current prefix of `searchWord`. If there are more than three products with a common prefix, the system should return the three lexicographically smallest products. The output should be a list of lists, where each inner list contains the suggested products after each character of `searchWord` has been typed.
Solution Approach
The solution uses a sorted array of products and a two-pointer approach to find the lexicographically smallest products that share a common prefix with the current prefix of the search word. The main idea is to maintain two pointers, `l` and `r`, representing the left and right boundaries of the search space within the sorted `products` array. For each character typed in the `searchWord`, the `l` and `r` pointers are adjusted to narrow down the range of products that start with the current prefix. After narrowing the search space, the solution extracts at most three products from the range [l, r] and adds them to the result list.
Step-by-Step Algorithm
- Step 1: Sort the `products` array lexicographically using `Arrays.sort(products)` or equivalent.
- Step 2: Initialize an empty list called `result` to store the suggested products for each prefix of `searchWord`.
- Step 3: Initialize two pointers, `l` to 0 and `r` to `products.length - 1`.
- Step 4: Iterate through each character `c` of `searchWord` using a `for` loop with index `i`.
- Step 5: Within the loop, adjust the left pointer `l` to the right until `products[l]` starts with the prefix of `searchWord` up to index `i`. If `products[l]`'s length is less than or equal to `i`, or `products[l][i]` is not equal to `c`, increment `l`.
- Step 6: Similarly, adjust the right pointer `r` to the left until `products[r]` starts with the prefix of `searchWord` up to index `i`. If `products[r]`'s length is less than or equal to `i`, or `products[r][i]` is not equal to `c`, decrement `r`.
- Step 7: Create a new list `suggestion` to store the suggested products for the current prefix.
- Step 8: Iterate from `l` to `min(l + 2, r)`, adding `products[j]` to the `suggestion` list. This ensures that at most three products are added.
- Step 9: Add the `suggestion` list to the `result` list.
- Step 10: After the loop finishes, return the `result` list.
Key Insights
- Insight 1: Sorting the `products` list lexicographically is crucial for returning the lexicographically smallest suggestions when multiple products share a common prefix.
- Insight 2: Binary search (or a two-pointer approach that mimics binary search) can efficiently find the range of products that have a common prefix with the current prefix of `searchWord`.
- Insight 3: We need to limit the number of suggestions to at most three, and ensure to handle edge cases like empty prefixes or prefixes with fewer than three matching products.
Complexity Analysis
Time Complexity: O(n log n + m*n)
Space Complexity: O(m)
Topics
This problem involves: Array, String, Binary Search, Trie, Sorting, Heap (Priority Queue).
Companies
Asked at: Anduril, Atlassian, Citadel, Coursera, Deliveroo, Docusign, DoorDash, J.P. Morgan, ServiceNow, Snap, Two Sigma, UBS, Wix.