|
| 1 | +## Explanation |
| 2 | + |
| 3 | +### Strategy (The "Why") |
| 4 | + |
| 5 | +**1.1 Constraints & Complexity:** |
| 6 | + |
| 7 | +* **Input Size:** Products array can have up to 1000 strings, each up to 3000 characters. SearchWord can be up to 1000 characters. |
| 8 | +* **Time Complexity:** O(m * log m + n * m) where m is products length and n is searchWord length. Sorting takes O(m log m), and for each character we scan products. |
| 9 | +* **Space Complexity:** O(m) for sorted products plus O(n * 3) for results. |
| 10 | +* **Edge Case:** If no products match a prefix, return an empty list for that prefix. |
| 11 | + |
| 12 | +**1.2 High-level approach:** |
| 13 | + |
| 14 | +The goal is to suggest up to 3 products that have a common prefix with the search word as each character is typed. We sort products first, then for each prefix, find matching products. |
| 15 | + |
| 16 | +![Prefix matching showing how products are filtered as each character is typed] |
| 17 | + |
| 18 | +**1.3 Brute force vs. optimized strategy:** |
| 19 | + |
| 20 | +* **Brute Force:** For each prefix, scan all products and filter those starting with the prefix. This is O(n * m) where n is searchWord length. |
| 21 | +* **Optimized (Sorting + Linear Scan):** Sort products once, then for each prefix, scan from the beginning and collect up to 3 matches. This is O(m log m + n * m). |
| 22 | +* **Why it's better:** Sorting ensures we get lexicographically minimum products first, and we can stop after finding 3 matches. |
| 23 | + |
| 24 | +**1.4 Decomposition:** |
| 25 | + |
| 26 | +1. Sort the products array lexicographically. |
| 27 | +2. For each character in searchWord: |
| 28 | + - Build the current prefix. |
| 29 | + - Scan products from the beginning. |
| 30 | + - Collect products that start with the prefix (up to 3). |
| 31 | + - Add to results. |
| 32 | +3. Return the list of suggestion lists. |
| 33 | + |
| 34 | +### Steps (The "How") |
| 35 | + |
| 36 | +**2.1 Initialization & Example Setup:** |
| 37 | + |
| 38 | +Let's use the example: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse" |
| 39 | + |
| 40 | +We initialize: |
| 41 | +* Sort products: ["mobile","moneypot","monitor","mouse","mousepad"] |
| 42 | +* `res = []` |
| 43 | +* `prefix = ""` |
| 44 | + |
| 45 | +**2.2 Start Checking/Processing:** |
| 46 | + |
| 47 | +We iterate through each character in searchWord. |
| 48 | + |
| 49 | +**2.3 Trace Walkthrough:** |
| 50 | + |
| 51 | +| Char | prefix | Matching Products | Suggestions | |
| 52 | +|------|--------|-------------------|-------------| |
| 53 | +| 'm' | "m" | mobile, moneypot, monitor | ["mobile","moneypot","monitor"] | |
| 54 | +| 'o' | "mo" | mobile, moneypot, monitor | ["mobile","moneypot","monitor"] | |
| 55 | +| 'u' | "mou" | mouse, mousepad | ["mouse","mousepad"] | |
| 56 | +| 's' | "mous" | mouse, mousepad | ["mouse","mousepad"] | |
| 57 | +| 'e' | "mouse" | mouse, mousepad | ["mouse","mousepad"] | |
| 58 | + |
| 59 | +**2.4 Increment and Loop:** |
| 60 | + |
| 61 | +After processing each character, we append the suggestions list to results and continue. |
| 62 | + |
| 63 | +**2.5 Return Result:** |
| 64 | + |
| 65 | +After processing all characters, `res = [["mobile","moneypot","monitor"], ["mobile","moneypot","monitor"], ["mouse","mousepad"], ["mouse","mousepad"], ["mouse","mousepad"]]` is returned. |
| 66 | + |
0 commit comments