I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1268. Search Suggestions System, the solution in this repository is mainly a trie solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: trie, stack, prefix sums.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are put, search3Prefix.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- Substring checks are convenient but not free, so they are part of the real complexity story.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 136 ms, faster than 42.73% of C++ online submissions for Search Suggestions System.
02//Memory Usage: 96.6 MB, less than 100.00% of C++ online submissions for Search Suggestions System.
03
04class TrieNode{
05public:
06 vector<TrieNode*> children;
07 string word;
08 TrieNode(){
09 children = vector<TrieNode*>(26, NULL);
10 word = "";
11 }
12};
13
14class Trie{
15public:
16 TrieNode* root;
17
18 Trie(){
19 root = new TrieNode();
20 }
21
22 void put(string& word){
23 TrieNode* cur = root;
24 for(char c : word){
25 if(cur->children[c-'a'] == NULL){
26 cur->children[c-'a'] = new TrieNode();
27 }
28 cur = cur->children[c-'a'];
29 }
30 cur->word = word;
31 }
32
33 vector<string> search3Prefix(string& prefix){
34 TrieNode* cur = root;
35 for(char c : prefix){
36 if(cur->children[c-'a'] == NULL){
37 return vector<string>();
38 }
39 cur = cur->children[c-'a'];
40 }
41 //find all product with the prefix
42 //dfs
43 vector<string> res;
44 stack<TrieNode*> stk;
45 stk.push(cur);
46 while(!stk.empty()){
47 TrieNode* cur = stk.top(); stk.pop();
48
49 if(cur->word != ""){
50 res.push_back(cur->word);
51 if(res.size() == 3) break;
52 }
53
54 for(int i = 25; i >= 0; i--){
55 if(cur->children[i] != NULL){
56 stk.push(cur->children[i]);
57 }
58 }
59 }
60
61 return res;
62 }
63};
64
65class Solution {
66public:
67 vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
68 Trie* trie = new Trie();
69 for(string& product : products){
70 trie->put(product);
71 }
72
73 int N = searchWord.size();
74 vector<vector<string>> ans(N, vector<string>());
75
76 TrieNode* cur = trie->root;
77 for(int i = 0; i < N; i++){
78 char c = searchWord[i];
79 // cout << i << " " << c << endl;
80 if(cur->children[c-'a'] != NULL){
81 string prefix = searchWord.substr(0, i+1);
82 // cout << "prefix: " << prefix << endl;
83 ans[i] = trie->search3Prefix(prefix);
84
85 cur = cur->children[c-'a'];
86 }else{
87 //cannot find c
88 break;
89 }
90 }
91
92 return ans;
93 }
94};
95
96//https://leetcode.com/problems/search-suggestions-system/discuss/436674/C%2B%2BJavaPython-Sort-and-Binary-Search-the-Prefix
97//sort and binary search the prefix
98//Runtime: 48 ms, faster than 80.56% of C++ online submissions for Search Suggestions System.
99//Memory Usage: 37.3 MB, less than 100.00% of C++ online submissions for Search Suggestions System.
100//sort: time: O(nlogn), space: O(logn)
101//each query: time: O(logn), space: O(query word's size)
102class Solution {
103public:
104 vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
105 auto it = products.begin();
106 sort(it, products.end());
107
108 // for(string& product: products){
109 // cout << product << " ";
110 // }
111 // cout << endl;
112
113 vector<vector<string>> res;
114 string cur = "";
115 for (char c : searchWord) {
116 cur += c;
117 vector<string> suggested;
118 //the position of first element >= cur
119 it = lower_bound(it, products.end(), cur);
120 // cout << cur << " " << it - products.begin() << endl;
121 for (int i = 0; i < 3 && it + i != products.end(); i++) {
122 string& s = *(it + i);
123 // cout << s << " " << s.find(cur) << " | ";
124 if (s.find(cur) != 0){
125 //s.find(cur) returns the position of substring
126 //if not found(18446744073709551615) or not at the start,
127 //then stop searching
128 break;
129 }
130 suggested.push_back(s);
131 }
132 // cout << endl;
133 res.push_back(suggested);
134 }
135 return res;
136 }
137};
Cost