id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n unordered_map<string, set<string>> mp;\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n for (int i = 0; i < products.size(); i++) {\n string word = products[i];\n string t = \"\";\n for (int j = 0; j < word.size(); j++) {\n t += word[j];\n mp[t].insert(word);\n }\n }\n vector<vector<string>> res;\n string search = searchWord;\n string t = \"\";\n for (int j = 0; j < search.size(); j++) {\n t += search[j];\n set<string> prod = mp[t];\n int count = 3;\n vector<string> ans;\n for (auto it : prod) {\n if(count==0) break;\n ans.push_back(it);\n count--;\n }\n res.push_back(ans);\n }\n return res;\n }\n};", "memory": "155170" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n class TrieNode {\n public:\n TrieNode() {\n for (int i = 0; i < 26; ++i) {\n children[i] = nullptr;\n }\n suggestions = vector<string>();\n }\n array<shared_ptr<TrieNode>, 26> children;\n vector<string> suggestions;\n };\n\n class Trie {\n public:\n Trie() {\n root = make_shared<TrieNode>();\n }\n\n void add(string& word) {\n shared_ptr<TrieNode> curr = root;\n for (char ch : word) {\n if (!curr->children[ch-'a'])\n curr->children[ch-'a'] = make_shared<TrieNode>();\n curr = curr->children[ch-'a'];\n\n curr->suggestions.push_back(word);\n sort(curr->suggestions.begin(), curr->suggestions.end());\n if (curr->suggestions.size() > 3)\n curr->suggestions.pop_back();\n }\n }\n\n vector<string> getSuggestions(string searchWord) {\n shared_ptr<TrieNode> curr = root;\n for (char ch : searchWord) {\n if (!curr->children[ch-'a'])\n return {};\n curr = curr->children[ch-'a'];\n }\n return curr->suggestions;\n }\n\n private:\n shared_ptr<TrieNode> root;\n };\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie trie;\n for (string& prod : products)\n trie.add(prod);\n \n vector<vector<string>> ans;\n for (int i = 0; i < searchWord.size(); ++i) {\n ans.push_back(trie.getSuggestions(searchWord.substr(0, i+1)));\n }\n\n return ans;\n }\n};", "memory": "162950" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n // We can create a map of strings / substrings that point to a list of\n // words with our key as the prefix. e.g. WordMap['A'] = [\"And\",\n // \"Anduril\", \"Andersen\", \"Abigail\"] WordMap[\"And\"] = [\"And\", \"Anduril\",\n // \"Andersen\"] -> WordMap[\"Andu\"] -> [\"Anduril\"] then find first 3 in\n // list based on suggestion\n\n // slightly expensive to create our map\n // Populate our word / product map\n unordered_map<string, vector<string>> wordMap{};\n for (auto product : products) {\n for (int i = 1; i <= product.size(); i++) {\n // Get our prefix\n string prefix = product.substr(0, i);\n // If our prefix is in our word map already, add our word to the\n // list of possible words with that prefix\n if (wordMap.contains(prefix)) {\n wordMap[prefix].push_back(product);\n }\n // Other wise create a new vector of words\n else {\n wordMap[prefix] = {product};\n }\n }\n }\n\n // Create a list of list of words, with each index representing 3\n // possible words for the given number of characters of our search word\n vector<vector<string>> productList{};\n for (int i = 1; i <= searchWord.size(); i++) {\n string prefix = searchWord.substr(0, i);\n if (wordMap.contains(prefix)){\n sort(wordMap[prefix].begin(), wordMap[prefix].end());\n vector<string> possibleWords = wordMap[prefix];\n if (possibleWords.size() > 3) {\n productList.push_back(\n {possibleWords[0], possibleWords[1], possibleWords[2]});\n } else {\n productList.push_back(possibleWords);\n }\n } else {\n productList.push_back({});\n }\n }\n\n return productList;\n }\n};", "memory": "162950" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "struct Node {\n Node* links[26];\n int cntEndWith = 0;\n int cntPrefix = 0;\n vector<string> temp; \n bool containsKey(char ch) {\n return (links[ch - 'a'] != NULL); \n }\n Node* get(char ch) {\n return links[ch - 'a']; \n }\n void put(char ch, Node* node) {\n links[ch - 'a'] = node;\n }\n void addWordToTemp(string s) {\n temp.push_back(s);\n }\n void increaseEnd() {\n cntEndWith++; \n }\n void increasePrefix() {\n cntPrefix++;\n }\n void deleteEnd() {\n cntEndWith--; \n }\n void reducePrefix() {\n cntPrefix--; \n }\n};\nclass Trie {\nprivate:\n Node* root; \npublic:\n Trie() {\n root = new Node(); \n }\n void insert(string word) {\n Node* node = root;\n string s = \"\";\n for (char ch : word) { \n if (!node->containsKey(ch)) {\n node->put(ch, new Node()); \n }\n node = node->get(ch);\n node->addWordToTemp(word);\n node->increasePrefix(); \n }\n node->increaseEnd(); \n }\n vector<string> countPrefix(string word) {\n Node* node = root;\n for (char ch : word) { \n if (node->containsKey(ch)) { \n node = node->get(ch); \n } else {\n return {}; \n }\n }\n return node->temp; \n }\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string target) {\n Trie trie;\n for (const string& product : products) {\n trie.insert(product);\n }\n vector<vector<string>> ans(target.size());\n string s = \"\";\n for (int i = 0; i < target.size(); i++) {\n s += target[i];\n vector<string> some = trie.countPrefix(s);\n sort(some.begin(), some.end());\n for (int j = 0; j < min(3, (int)some.size()); j++) {\n ans[i].push_back(some[j]);\n }\n }\n return ans;\n }\n};\n", "memory": "170730" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Trie {\npublic:\n char val;\n unordered_map<char, Trie*> children;\n\n priority_queue<string> words3;\n Trie(char val_): val(val_) {}\n Trie(char val_, priority_queue<string> words3_): val(val_), words3(words3_) {}\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n // we just sort products in lexi order\n // then products sharing the same prefix with come close\n // or more efficient, we use Trie and store sibling nodes in order\n // typing is searching alone the Trie, return the first 3 in the children\n\n // build Trie\n Trie* root = new Trie('#');\n for (string product : products) {\n Trie* curr = root;\n for (char c : product) {\n if (curr->children.find(c) == curr->children.end()) {\n priority_queue<string> pq;\n pq.push(product);\n curr->children[c] = new Trie(c, pq);\n } else {\n priority_queue<string>& pq = curr->children[c]->words3;\n pq.push(product);\n if (pq.size() > 3) {\n pq.pop();\n }\n }\n curr = curr->children[c];\n cout << c << \": \" << curr->words3.size() << \", \" << curr->words3.top() << endl;\n }\n }\n\n vector<vector<string> > result;\n // search\n cout << endl << \"Searching!\" << endl;\n Trie* curr = root;\n bool endSearching = false;\n for (char c : searchWord) {\n if (endSearching || curr->children.find(c) == curr->children.end()) {\n result.push_back(vector<string>());\n endSearching = true;\n } else {\n vector<string> res3;\n priority_queue<string>& pq = curr->children[c]->words3;\n while (!pq.empty()) {\n res3.push_back(pq.top());\n pq.pop();\n }\n reverse(res3.begin(), res3.end());\n result.push_back(res3);\n curr = curr->children[c];\n }\n }\n return result;\n }\n};", "memory": "170730" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Node{\n public:\n char ch;\n unordered_map <char, Node*> mp;\n vector <string> words;\n bool terminal;\n\n public:\n Node(char ch, string word, bool terminal=false){\n this->ch = ch;\n this->terminal = terminal;\n this->words = {word}; \n }\n\n void addWord(string word){\n this->words.push_back(word);\n sort(this->words.begin(), this->words.end());\n }\n};\n\nclass Trie{\n private:\n Node* root;\n\n public:\n Trie(Node* node){\n this->root = node; \n }\n\n void insert(string word){\n Node* node = root;\n for(char ch : word){\n if(node->mp.find(ch) == node->mp.end()){\n Node* nn = new Node(ch, word);\n node->mp[ch] = nn;\n node = nn;\n }\n else{\n node = node->mp[ch];\n node->addWord(word);\n }\n }\n node->terminal = true;\n }\n\n bool search(string word){\n Node* node = root;\n for(char ch : word){\n if(!node)\n return false;\n\n if(node->mp.find(ch) == node->mp.end()){\n return false;\n }\n else{\n node = node->mp[ch];\n }\n }\n\n return node->terminal;\n }\n\n vector<vector<string>> findMatches(string x){\n Node* node = this->root;\n vector<vector<string>> result;\n\n for(char ch : x){\n vector <string> curr;\n // if(node->mp.find(ch) == node->mp.end())\n\n if(node)\n node = node->mp[ch];\n if(!node){\n result.push_back(curr);\n continue;\n }\n\n int n = node->words.size();\n int mn = min(3, n);\n for(int k=0;k<mn;k++){\n curr.push_back(node->words[k]);\n }\n result.push_back(curr);\n }\n\n return result;\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> result;\n sort(products.begin(), products.end());\n Node* root = new Node('&', \"\");\n Trie trie = Trie(root);\n\n for(string product : products){\n trie.insert(product);\n }\n\n return trie.findMatches(searchWord);\n }\n};", "memory": "178510" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n class Trie {\n public:\n Trie() {\n parent_ = nullptr;\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n }\n Trie(const std::string& prefix, Trie* parent) {\n prefix_ = prefix;\n parent_ = parent;\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n }\n std::string prefix_;\n Trie* parent_;\n Trie *children[26];\n std::set<std::string> suggestions;\n };\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n return suggestedProductsUsingTrie(products, searchWord);\n }\n\n void InsertAndKeepTop3(std::set<std::string>& suggestions, std::string word) {\n suggestions.insert(word);\n return;\n int count = 0;\n for (auto iter = suggestions.begin(); iter != suggestions.end(); ++iter) {\n if (++count > 3) {\n suggestions.erase(iter);\n }\n }\n }\n\n vector<vector<string>> suggestedProductsUsingTrie(vector<string>& products, string searchWord) {\n std::set<std::string> dict(products.begin(), products.end());\n // Return a list of lists of the suggested products after each character of searchWord is typed.\n Trie* root = new Trie;\n for (string& product : products) {\n Trie* node = root;\n std::string prefix;\n for (char ch : product) {\n prefix.append(1, ch);\n Trie* child_node = node->children[ch - 'a'];\n if (child_node == nullptr) {\n child_node = new Trie(prefix, node);\n node->children[ch - 'a'] = child_node;\n }\n node = child_node;\n }\n assert(node->prefix_ == product);\n InsertAndKeepTop3(node->suggestions, product);\n Trie* parent = node->parent_;\n while (parent != nullptr && parent->parent_ != nullptr) {\n InsertAndKeepTop3(parent->suggestions, product);\n parent = parent->parent_;\n }\n }\n\n std::vector<std::vector<std::string>> result;\n Trie* node = root;\n for (char ch : searchWord) {\n vector<string> suggestions; \n if (node != nullptr) {\n node = node->children[ch - 'a']; \n if (node != nullptr) {\n int count = 0;\n for (const string& s : node->suggestions) {\n if (++count > 3) break;\n suggestions.push_back(s);\n }\n }\n }\n result.push_back(suggestions);\n }\n return result;\n }\n};", "memory": "178510" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> suggested_products;\n\n sort(products.begin(), products.end());\n for(int i = 0; i < searchWord.length(); i++) {\n auto typed_str = searchWord.substr(0, i+1);\n vector<string> prods;\n for(const auto& p: products) {\n if(typed_str == p.substr(0, i+1)) {\n prods.push_back(p);\n if(prods.size() == 3) break;\n }\n }\n suggested_products.push_back(prods);\n }\n return suggested_products;\n }\n};", "memory": "186290" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n std::sort(products.begin(), products.end());\n vector<vector<string>> listofList;\n std::string searchString = \"\";\n for(int i = 0; i < searchWord.size(); i++) {\n searchString = searchWord.substr(0, i+1);\n vector<string> list;\n for(auto& it : products) {\n if(list.size() == 3) {\n break;\n }\n std::string checkString = it.substr(0, i+1);\n if(checkString == searchString) {\n list.push_back(it);\n }\n }\n listofList.push_back(list);\n searchString.clear();\n }\n return listofList;\n }\n};", "memory": "186290" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct Trie {\n Trie *next[128];\n bool end;\n Trie(){\n for(int i=0;i<128;i++){\n next[i]=NULL;\n }\n end=false;\n }\n};\n\nclass Solution {\n void DFS(Trie *t, string &curr, set<string> &recs){\n if(recs.count(curr) || recs.size()>=3){\n return;\n }\n if(t->end){\n recs.insert(curr);\n }\n for(int i=0;i<128;i++){\n auto tmp=t;\n if(tmp->next[i]){\n tmp=tmp->next[i];\n curr.push_back(i);\n DFS(tmp,curr,recs);\n curr.pop_back();\n }\n }\n }\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie *t=new Trie();\n for(auto &p:products){\n auto tmp=t;\n for(auto &c:p){\n if(!tmp->next[c]){\n tmp->next[c]=new Trie();\n }\n tmp=tmp->next[c];\n }\n tmp->end=true;\n }\n vector<vector<string>> ret;\n string word;\n int broken=false;\n for(auto &c:searchWord){\n if(!t->next[c]||broken){\n ret.push_back({});\n broken=true;\n continue;\n }\n word+=c;\n t=t->next[c];\n set<string> recs;\n DFS(t,word,recs);\n vector<string> toadd;\n for(auto &r:recs){\n toadd.push_back(r);\n }\n ret.push_back(toadd);\n\n }\n return ret;\n }\n};", "memory": "217410" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n map<string, vector<string>> mp;\n vector<vector<string>> ans;\n for(auto it : products){\n int n = it.size();\n string t;\n for(int i = 0; i < n; i++){\n t += it[i];\n mp[t].push_back(it);\n }\n }\n for(auto it : mp){\n vector<string> temp = it.second;\n sort(temp.begin(), temp.end());\n mp[it.first] = temp;\n }\n int n = searchWord.size();\n string t;\n for(int i = 0; i < n; i++){\n t += searchWord[i];\n int sz = mp[t].size();\n vector<string> temp;\n for(int j = 0; j < min(sz, 3); j++) temp.push_back(mp[t][j]);\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "217410" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvoid helper(vector<string>&products,string &sw, int i, vector<string>&temp)\n{\n priority_queue<string,vector<string>,greater<string>>pq;\n for(auto it: products)\n {\n if(it.size()<i+1)continue;\n int flag=0;\n for(int k=0;k<=i;k++)\n {\n if(sw[k]!=it[k])\n {\n flag=1;\n break;\n }\n }\n if(flag==0)\n {\n pq.push(it);\n }\n }\n for(int i=0;i<3;i++)\n {\n if(pq.size()==0)break;\n temp.push_back(pq.top());\n pq.pop();\n }\n}\n vector<vector<string>> suggestedProducts(vector<string>& products, string sw) {\n vector<vector<string>>ans;\n int n=sw.size();\n for(int i=0;i<n;i++)\n {\n vector<string>temp;\n helper(products,sw,i,temp);\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "225190" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n vector<vector<string>> result;\n\n for (int i = 0; i < searchWord.length(); i++) {\n vector<string> temp;\n for (auto str : products) {\n int count = 0;\n for (int j = 0; j <= i; j++) {\n // if (i == j) {\n if (searchWord[j] == str[j]) {\n count++;\n } else {\n break;\n }\n // }\n }\n if (count == i + 1) {\n temp.push_back(str);\n }\n }\n sort(temp.begin(), temp.end());\n vector<string> ans;\n int count = 0;\n for (auto str : temp) {\n if (count == 3) {\n break;\n } else {\n ans.push_back(str);\n count++;\n }\n }\n\n result.push_back(ans);\n }\n return result;\n }\n};", "memory": "225190" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n //key 값과 같거나 큰 값이 처음으로 나오는 위치 반환\n int bs_lowerbound(string typing, vector<string> products, int len){\n int l = 0, r = products.size();\n while(l < r){\n int m = l + (r - l)/2;\n //string cmp = products[m].substr(0, len);\n //curr 값이 key값과 같거나 크다면 왼쪽 부분만 탐색 \n //if(typing <= cmp){\n //compare 0 이상의 뜻은 curr값이 key값과 같거나 사전적으로 더 크다는 뜻\n if(0 <= products[m].compare(0, len, typing)){\n r = m;\n }\n else{\n l = m + 1;\n }\n }\n //cout << \"\\n\";\n return l;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> suggests;\n sort(products.begin(), products.end());\n string typing = \"\";\n int len = 1;\n int sz = products.size();\n for(auto c : searchWord){\n typing += c;\n int target = bs_lowerbound(typing, products, len);\n suggests.push_back({});\n for(int i = target; i < min(target + 3, sz) && !products[i].compare(0, len, typing); i++){\n suggests.back().push_back(products[i]);\n }\n /*while(target < products.size() && !products[target].compare(0, len, typing) && tmp.size() < 3){\n tmp.push_back(products[target++]);\n }*/\n //suggests.push_back(tmp);\n len++;\n }\n return suggests;\n }\n};", "memory": "232970" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n //key 값과 같거나 큰 값이 처음으로 나오는 위치 반환\n int bs_lowerbound(string typing, vector<string> products, int len){\n int l = 0, r = products.size();\n while(l < r){\n int m = l + (r - l)/2;\n //string cmp = products[m].substr(0, len);\n //curr 값이 key값과 같거나 크다면 왼쪽 부분만 탐색 \n //if(typing <= cmp){\n //compare 0 이상의 뜻은 curr값이 key값과 같거나 사전적으로 더 크다는 뜻\n if(0 <= products[m].compare(0, len, typing)){\n r = m;\n }\n else{\n l = m + 1;\n }\n }\n //cout << \"\\n\";\n return l;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> suggests;\n sort(products.begin(), products.end());\n string typing = \"\";\n int len = 1;\n\n for(auto c : searchWord){\n typing += c;\n vector<string> tmp;\n int target = bs_lowerbound(typing, products, len);\n\n while(target < products.size() && !products[target].compare(0, len, typing) && tmp.size() < 3){\n tmp.push_back(products[target++]);\n }\n suggests.push_back(tmp);\n len++;\n }\n return suggests;\n }\n};", "memory": "240750" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n vector<string> bs(vector<string> arr, string s){\n int n=arr.size(), m=s.size();\n int low=0, high=n-1;\n\n while(low<=high){\n int mid=low+(high-low)/2;\n\n if(s<=arr[mid]){\n high=mid-1;\n }\n else{\n low=mid+1;\n }\n }\n\n vector<string> ret;\n\n for(int i=low;i<min(low+3,n);i++){\n string str=arr[i].substr(0,m);\n if(s!=str) break;\n ret.push_back(arr[i]);\n }\n\n return ret;\n }\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n\n vector<vector<string>> ret;\n\n int m=searchWord.size();\n for(int i=0;i<m;i++){\n string s=searchWord.substr(0,i+1);\n\n ret.push_back(bs(products,s));\n }\n\n return ret;\n }\n};", "memory": "248530" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n //key 값과 같거나 큰 값이 처음으로 나오는 위치 반환\n int bs_lowerbound(string typing, vector<string> products, int len){\n int l = 0, r = products.size();\n while(l < r){\n int m = l + (r - l)/2;\n string cmp = products[m].substr(0, len);\n //curr 값이 key값과 같거나 크다면 왼쪽 부분만 탐색 \n if(typing <= cmp){\n r = m;\n }\n else{\n l = m + 1;\n }\n }\n //cout << \"\\n\";\n return l;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> suggests;\n sort(products.begin(), products.end());\n string typing = \"\";\n int len = 1;\n\n for(auto c : searchWord){\n typing += c;\n vector<string> tmp;\n int target = bs_lowerbound(typing, products, len);\n\n while(target < products.size() && !products[target].compare(0, len, typing) && tmp.size() < 3){\n tmp.push_back(products[target++]);\n }\n suggests.push_back(tmp);\n len++;\n }\n return suggests;\n }\n};", "memory": "256310" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n int n=products.size();\n sort(products.begin(),products.end());\n vector<vector<string>>ans;\n vector<string>temp;\n string curr=\"\";\n for(auto c:searchWord){\n curr+=c; // adding character, like -> m-->mo-->mou-->mous--->mouse\n temp.clear(); //reusing the same vector\n for(int i=0;i<n;i++){\n string s=products[i];\n if(s.substr(0,curr.length())==curr){ //finding the prefix containing words in the list\n temp.push_back(s);\n }\n if(temp.size()==3)break; //question asked for 3 words so we break at 3\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "264090" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n int n=products.size();\n sort(products.begin(),products.end());\n vector<vector<string>>ans;\n vector<string>temp;\n string curr=\"\";\n for(auto c:searchWord){\n curr+=c; // adding character, like -> m-->mo-->mou-->mous--->mouse\n temp.clear(); //reusing the same vector\n for(int i=0;i<n;i++){\n string s=products[i];\n if(s.substr(0,curr.length())==curr){ //finding the prefix containing words in the list\n temp.push_back(s);\n }\n if(temp.size()==3)break; //question asked for 3 words so we break at 3\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "271870" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n int n = products.size();\n sort(products.begin(), products.end());\n vector<vector<string>> ans;\n vector<string> temp;\n string curr = \"\";\n for(auto c : searchWord){\n curr += c;\n temp.clear();\n for(int i = 0; i < n; i++){\n string s = products[i];\n if(s.substr(0, curr.length()) == curr){\n temp.push_back(s);\n }\n if(temp.size() == 3) break;\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "279650" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n cin.tie(nullptr);\n cout.tie(nullptr);\n ios::sync_with_stdio(false);\n \n int n = products.size(); \n sort(products.begin(), products.end());\n vector<vector<string>> result;\n vector<string> temp;\n string curr = \"\";\n for(auto c : searchWord){\n curr += c;\n temp.clear();\n for(int i=0; i<n; i++){\n string s = products[i];\n if(s.substr(0, curr.length()) == curr)\n temp.push_back(s);\n if(temp.size() == 3) break;\n }\n result.push_back(temp);\n }\n return result;\n }\n};", "memory": "287430" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n cin.tie(nullptr);\n cout.tie(nullptr);\n ios::sync_with_stdio(false);\n \n int n = products.size(); \n sort(products.begin(), products.end());\n vector<vector<string>> result;\n vector<string> temp;\n string curr = \"\";\n for(auto c : searchWord){\n curr += c;\n temp.clear();\n for(int i=0; i<n; i++){\n string s = products[i];\n if(s.substr(0, curr.length()) == curr)\n temp.push_back(s);\n if(temp.size() == 3) break;\n }\n result.push_back(temp);\n }\n return result;\n }\n};", "memory": "287430" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n // products sorted in lexicographical order\n //Start from 0th element of word search, keep adding next element after each iteration is done\n //get substring of products for length of substring of searchword when count < 3\n //If count < 3 and product substring matches the search string, add it in vector and interResult and inc count\n //if count = 3 break the loop as we do not need to check further\n //Once the vector is iterated completely for a searchString, add this vector to final result vector\n //return result\n\n sort(products.begin(), products.end());\n string searchString = \"\";\n vector<vector<string>> result;\n for(int i = 0; i<searchWord.length(); i++) {\n searchString += searchWord[i];\n int count = 0;\n vector<string> interResult;\n for(string product : products) {\n if(product.substr(0, searchString.length()) == searchString) {\n count++;\n interResult.push_back(product);\n if (count == 3) {break;}\n }\n else if(count > 0){\n break;\n }\n }\n result.push_back(interResult);\n }\n return result;\n }\n};", "memory": "295210" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int binarySearch(string typing, vector<string> products, int len){\n int l = 0, r = products.size() - 1;\n int idx;\n\n while(l <= r){\n int m = l + (r - l)/2;\n string cmp = products[m].substr(0, len);\n if(cmp == typing){\n idx = m;\n r = m - 1;\n }\n if(cmp < typing){\n l = m + 1;\n }\n else{\n r = m - 1;\n }\n }\n //cout << \"\\n\";\n return idx;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> suggests;\n sort(products.begin(), products.end());\n string typing = \"\";\n int len = 1;\n\n for(auto c : searchWord){\n typing += c;\n vector<string> tmp;\n int target = binarySearch(typing, products, len);\n // cout << target << \": \" << products[target] << \"\\n\";\n while(target < products.size() && products[target].substr(0, len) == typing && tmp.size() < 3)\n tmp.push_back(products[target++]);\n suggests.push_back(tmp);\n len++;\n }\n return suggests;\n }\n};", "memory": "302990" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& pro, string se) {\n sort(pro.begin(),pro.end());\n vector<vector<string>>ans;\n\n string temp ;\n\n for(int i = 0;i<se.size();i++){\n temp+=se[i];\n vector<string>v;\n for(auto s:pro){\n if(s.substr(0,temp.size())==temp){\n v.push_back(s);\n }\n\n if(v.size()>=3)break;\n }\n ans.push_back(v);\n }\n return ans;\n }\n};", "memory": "310770" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> results;\n sort(products.begin(), products.end());\n string prefix = \"\";\n\n for (char ch : searchWord) {\n prefix += ch;\n vector<string> cands;\n for (string product : products) {\n if (product.substr(0, prefix.size()) == prefix) {\n cands.push_back(product);\n }\n if (cands.size() == 3) break;\n }\n results.push_back(cands);\n }\n return results;\n }\n};", "memory": "318550" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>> ans;\n string prefix=\"\";\n for(char c:searchWord){\n prefix+=c;\n vector<string> match;\n for(string s:products){\n if(s.substr(0,prefix.size())==prefix){\n match.push_back(s);\n if(match.size()==3){\n break;\n }\n }\n }\n ans.push_back(match);\n }\n return ans;\n }\n};", "memory": "326330" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n string wrd = \"\";\n vector<vector<string>> ans;\n \n for(auto c : searchWord){\n vector<string> suggestions;\n wrd += c;\n int i = 0;\n for(auto product : products){\n if(i == 3) break;\n if(product.substr(0, wrd.size()) == wrd){\n i++;\n suggestions.push_back(product);\n }\n }\n ans.push_back(suggestions);\n\n\n\n }\n\n return ans;\n\n }\n};", "memory": "334110" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) \n {\n sort(products.begin(), products.end()); //sort products to get elements in lexicographical order\n vector<vector<string>> res;\n \n string cur = \"\";\n for(auto it: searchWord) //cover searchWord letter by letter\n {\n vector<string> suggested;\n cur += it;\n int j=0;\n for(auto product:products) //find cur string in products\n {\n if(j==3) break; //when we suggested 3 products\n \n if(product.substr(0, cur.size()) == cur) //matching cur with product substring\n {\n suggested.push_back(product); //if match push it in suggested\n j++;\n }\n \n }\n res.push_back(suggested); //push vector for cur in res\n }\n return res;\n }\n};", "memory": "334110" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void givelexiluna(vector<string>v){\n vector<string> vec;\n \n\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& pro, string s) {\n map<string,int>mp;\n sort(pro.begin(),pro.end());\n int n = s.size();\n for(auto it: pro){\n mp[it]++;\n }\n string temp =\"\";\n vector<vector<string>> ans;\n for(int i=0;i<n;i++){\n temp +=s[i];\n vector<string> v;\n for(auto it: pro){\n int k = temp.size();\n if(it.substr(0,k)==temp){\n v.push_back(it);\n }\n if(v.size()==3){\n break;\n }\n }\n // sort(v.begin(),v.end());\n // givelexiluna(v);\n // if(v.size()>3){\n // ans.push_back({v[0],v[1],v[2]});\n // }else\n ans.push_back(v);\n }\n return ans;\n }\n};", "memory": "341890" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n int n = products.size();\n vector<vector<string>> ans;\n vector<string> temp;\n string current=\"\";\n sort(products.begin(),products.end());\n\n for(auto c:searchWord){\n current = current + c;\n temp.clear();\n for(int i=0;i<n;i++){//looping over all products array\n string s = products[i];\n if(current == s.substr(0,current.length())){\n temp.push_back(s);\n }\n if(temp.size() == 3) break;\n\n }\n ans.push_back(temp);\n\n }\n return ans;\n\n }\n};", "memory": "341890" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n \n vector<vector<string>> finalProducts{};\n \n int searchIndex = 0;\n string currentTerm = searchWord.substr(0, 1);\n \n for (int i = 1; i <= searchWord.size(); i++) {\n vector<string> localProducts{};\n \n for (int j = 0; j < products.size(); j++) {\n string currentProduct = products[j];\n if (currentProduct.substr(0, i) == currentTerm && localProducts.size() < 3)\n localProducts.push_back(currentProduct);\n \n }\n \n finalProducts.push_back(localProducts);\n \n currentTerm += searchWord[++searchIndex];\n }\n \n return finalProducts;\n }\n};", "memory": "349670" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n \n vector<vector<string>> finalProducts{};\n \n int searchIndex = 0;\n string currentTerm = searchWord.substr(0, 1);\n \n for (int i = 1; i <= searchWord.size(); i++) {\n vector<string> localProducts{};\n \n cout << currentTerm << \"\\n\";\n \n for (int j = 0; j < products.size(); j++) {\n string currentProduct = products[j];\n if (currentProduct.substr(0, i) == currentTerm && localProducts.size() < 3)\n localProducts.push_back(currentProduct);\n \n }\n \n finalProducts.push_back(localProducts);\n \n currentTerm += searchWord[++searchIndex];\n }\n \n return finalProducts;\n }\n};", "memory": "357450" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n \n vector<vector<string>> finalProducts{};\n \n for (int i = 1; i <= searchWord.size(); i++) {\n vector<string> localProducts{};\n string currentTerm = searchWord.substr(0, i);\n \n for (int j = 0; j < products.size(); j++) {\n string currentProduct = products[j];\n if (currentProduct.substr(0, i) == currentTerm && localProducts.size() < 3)\n localProducts.push_back(currentProduct);\n \n }\n \n //if (!localProducts.empty())\n finalProducts.push_back(localProducts);\n }\n \n return finalProducts;\n }\n};", "memory": "357450" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n struct TrieNode {\n std::array<TrieNode*, 26> children;\n bool wordEnd{false};\n\n TrieNode* getNode(char ch) {\n return children[ch - 'a'];\n }\n\n void addWord(const std::string& word, int pos = 0){\n if(pos == word.size()) {\n wordEnd = true;\n } else {\n int idx = word[pos] - 'a';\n if(!children[idx]) {\n children[idx] = new TrieNode();\n }\n children[idx]->addWord(word, pos + 1);\n }\n }\n bool getSmallestThree(const std::string& prefix, std::vector<std::string>& result) {\n if(result.size() == 3) {\n return true;\n }\n if(wordEnd) {\n result.emplace_back(prefix);\n }\n for(int i = 0; i < children.size(); ++i) {\n if(children[i] && children[i]->getSmallestThree(prefix + std::string(1, i + 'a'), result)) {\n return true;\n }\n }\n return false;\n }\n };\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n TrieNode root;\n for(const auto& product : products) {\n root.addWord(product);\n }\n TrieNode* currentNode = &root;\n vector<vector<string>> ans;\n std::string prefix;\n for(char ch : searchWord) {\n prefix.append(1, ch);\n currentNode = currentNode->getNode(ch);\n if(nullptr == currentNode) {\n break;\n }\n std::vector<std::string> smallestThree;\n currentNode->getSmallestThree(\"\", smallestThree);\n for(int i = 0; i < smallestThree.size(); ++i) {\n smallestThree[i] = prefix + smallestThree[i];\n }\n ans.emplace_back(smallestThree);\n }\n ans.resize(searchWord.size());\n return ans;\n }\n};", "memory": "365230" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode\n{\npublic:\n TrieNode* children[26];\n bool is_word;\n TrieNode();\n void insert(const std::string& s);\n void traverse(const std::string& prefix, std::string cur, int& k, std::vector<std::string>& res);\n TrieNode* find(const std::string& s);\n};\n\nTrieNode::TrieNode()\n{\n for (int i = 0; i < 26; i ++)\n {\n children[i] = NULL;\n is_word = false;\n }\n};\n\nTrieNode* TrieNode::find(const std::string& s)\n{\n TrieNode* cur = this;\n for (int i = 0; i < s.length(); i ++)\n {\n int ind = s[i] - 'a';\n if (cur->children[ind] == NULL)\n {\n return NULL;\n }\n else\n {\n cur = cur->children[ind];\n }\n }\n return cur;\n}\n\nvoid TrieNode::insert(const std::string& s)\n{\n TrieNode* cur = this;\n for (int i = 0; i < s.length(); i ++)\n {\n int ind = s[i] - 'a';\n if (cur->children[ind] == NULL)\n {\n cur->children[ind] = new TrieNode();\n }\n cur = cur->children[ind];\n }\n cur->is_word = true;\n \n};\n\nvoid TrieNode::traverse(const std::string& prefix, std::string cur, int& k, std::vector<std::string>& res)\n{\n if (k == 0)\n return ;\n \n if (is_word)\n {\n res.push_back(prefix + cur);\n k --;\n }\n\n for (int i = 0; i < 26; i ++)\n {\n if (children[i] != NULL)\n {\n children[i]->traverse(prefix, cur + (char)('a' + i), k, res);\n }\n }\n}\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n TrieNode* root = new TrieNode();\n for (int i = 0; i < products.size(); i ++)\n {\n root->insert(products[i]);\n }\n\n std::vector<std::vector<std::string>> ret(searchWord.length());\n for (int i = 0; i < searchWord.length(); i ++)\n {\n int k = 3;\n std::string prefix = searchWord.substr(0, i + 1);\n TrieNode* start = root->find(prefix);\n if (start != NULL)\n start->traverse(prefix, std::string(), k, ret[i]);\n }\n return ret;\n }\n};", "memory": "365230" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct trieNode {\n vector<trieNode*> data;\n bool endOfWord;\n trieNode() {\n endOfWord = false;\n data = vector<trieNode*> (26, nullptr);\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> ans(searchWord.length());\n // create a root\n trieNode* root = new trieNode();\n // put all products in trie tree first\n for (string w: products) {\n trieNode* ptr = root; // create a ptr starting at root\n for (char l: w) {\n if (ptr->data[l - 'a'] == nullptr) {\n // this char does not exist at this layer yet\n ptr->data[l - 'a'] = new trieNode();\n }\n ptr = ptr->data[l - 'a']; // move ptr to next layer\n }\n ptr->endOfWord = true; // mark the end of the word\n }\n trieNode* ptr = root;\n string prefix = \"\";\n for (int i = 0; i < searchWord.length(); i++) {\n char cur = searchWord[i];\n if (ptr->data[cur - 'a'] == nullptr) {\n // this char does not exist at this layer, you done!\n break;\n }\n ptr = ptr->data[cur - 'a']; // update the ptr if this char is available in trie\n prefix += cur;\n // this char exist at this layer, find out top 3 words\n cout << cur << endl;\n findThreeWord(ptr, \"\", ans[i]);\n for (int j = 0; j < ans[i].size(); j++) {\n ans[i][j] = prefix + ans[i][j];\n }\n }\n return ans;\n }\n\n void findThreeWord(trieNode* ptr, string word, vector<string>& res) {\n if (ptr->endOfWord) {\n // found a word\n res.push_back(word);\n if (res.size() >= 3) {\n return;\n }\n }\n int ref = (int)'a';\n for (int i = 0; i < ptr->data.size(); i++) {\n if (ptr->data[i] != nullptr) {\n // call dfs\n findThreeWord(ptr->data[i], word + (char)(i + ref), res);\n if (res.size() >= 3) {\n return;\n }\n }\n }\n }\n};", "memory": "373010" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& p, string s) {\n string sp ;\n vector<vector<string>>v(s.size()); \n for(int i = 0;i<s.size();i++){ \n sp+=s[i] ; \n for(int k = 0;k<p.size();k++){\n string kp = p[k] ;\n string pk = kp.substr(0,sp.size()) ;\n if(pk == sp){\n v[i].push_back(kp) ;\n }\n }\n sort(v[i].begin(),v[i].end()) ;\n while(v[i].size()>3)v[i].pop_back() ;\n \n }\n return v ;\n }\n};", "memory": "373010" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> ans;\n for(int i = 0; i < searchWord.length(); i++) {\n string substr = searchWord.substr(0, i+1);\n vector<string> to_sort;\n for(string product: products) {\n string fuck = product.substr(0, i+1);\n if(product.size() < substr.size()) continue;\n if(substr.compare(fuck) == 0) to_sort.push_back(product);\n }\n sort(to_sort.begin(), to_sort.end());\n while(to_sort.size() > 3) {\n to_sort.pop_back();\n }\n ans.push_back(to_sort);\n }\n\n return ans;\n }\n};", "memory": "380790" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n class trienode{\n \n public:\n char data;\n trienode* children[26];\n bool isterminal;\n \n trienode(char a)\n {\n data=a;\n for(int i=0;i<26;i++)\n {\n children[i]=NULL;\n }\n isterminal=false;\n }\n \n \n \n };\n \n \n \n class trie{\n \n public:\n \n trienode* root;\n \n \n trie()\n {\n root = new trienode('-');\n }\n \n \n void insertutil(string word,trienode* root)\n {\n if(word.length()==0)\n {\n root->isterminal=true;\n return;\n }\n \n int index=word[0]-'a';\n \n trienode* child;\n \n if(root->children[index]!=NULL)\n {\n child = root->children[index];\n }else{\n \n child = new trienode(word[0]);\n root->children[index]=child;\n }\n \n insertutil(word.substr(1),child);\n \n }\n \n \n \n \n \n void insert(string word)\n {\n insertutil(word,root);\n \n }\n \n \n \n void getallw(string& ans,trienode* root,vector<string>& v)\n {\n if(root==NULL)\n {\n return;\n }\n \n if(root->isterminal==true)\n {\n v.push_back(ans);\n if(v.size()==3)\n {\n return;\n }\n }\n \n \n for(int i=0;i<26;i++)\n {\n if(root->children[i]!=NULL)\n {\n ans.push_back(root->children[i]->data);\n getallw(ans,root->children[i],v);\n ans.pop_back();\n }\n \n }\n \n \n \n \n }\n \n \n \n \n void getsuggestutil(string word,trienode* root,vector<string>& v)\n {\n \n if(word.length()==0)\n {\n string ans=word;\n //cout<<\"the current ans is \"<<ans<<endl;\n getallw(ans,root,v);\n return;\n }\n \n \n int index = word[0]-'a';\n trienode* child;\n \n if(root->children[index]!=NULL)\n {\n child=root->children[index];\n }else{\n return;\n }\n \n getsuggestutil(word.substr(1),child,v);\n \n \n }\n \n \n \n void getsuggest(string word,vector<string>& v)\n {\n \n getsuggestutil(word,root,v);\n sort(v.begin(),v.end());\n //cout<<\"yhe size of the string vector is \"<<v.size()<<endl;\n if(v.size()>3)\n {\n v.erase(v.begin()+3,v.end());\n }\n \n for(int i=0;i<v.size();i++)\n {\n v[i].insert(0,word);\n }\n }\n \n \n };\n \n \n \n \n \n \n \n \n \n \n \n \n \n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n \n \n trie* t = new trie();\n \n for(int i=0;i<products.size();i++)\n {\n t->insert(products[i]);\n }\n \n vector<vector<string>> ans;\n vector<string> v;\n \n for(int i=0;i<searchWord.length();i++)\n {\n \n t->getsuggest(searchWord.substr(0,i+1),v);\n ans.push_back(v);\n v.clear();\n \n \n } \n \n \n \n return ans;\n \n \n \n \n }\n};", "memory": "411910" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "#define N ('z' - 'a' + 1)\nclass T {\npublic:\nbool leaf;\nT* child[N];\n T() {\n for (int i = 0; i < N; ++i)\n child[i] = 0;\n leaf = false;\n }\n void insert(string& w) {\n T* node = this;\n for (int i = 0; i < w.size(); ++i) {\n if (!node->child[w[i] - 'a']) {\n node->child[w[i] - 'a'] = new T();\n }\n node = node->child[w[i] - 'a'];\n }\n node->leaf = true;\n }\n vector<string> sub(string w) {\n T* node = this;\n for (int i = 0; i < w.size(); ++i) {\n if (!node->child[w[i] - 'a']) return {};\n node = node->child[w[i] - 'a'];\n }\n vector<string> ans;\n queue<T*> q;\n queue<string> qs;\n q.push(node);\n qs.push(\"\");\n while (!q.empty()) {\n if (q.front()->leaf) {\n ans.push_back(w + qs.front());\n }\n for (int i = 0; i < N; ++i) {\n if (q.front()->child[i]) {\n q.push(q.front()->child[i]); \n qs.push(qs.front() + (char)('a' + i));\n }\n }\n q.pop();\n qs.pop();\n }\n sort(ans.begin(), ans.end());\n vector<string> ret;\n for (int i = 0; i < (3 < ans.size() ? 3 : ans.size()); ++i) \n ret.push_back(ans[i]);\n return ret;\n }\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n T t;\n for (int i = 0; i < products.size(); ++i) {\n t.insert(products[i]);\n } \n vector<vector<string>> ans;\n for (int i = 1; i <= searchWord.size(); ++i) {\n ans.push_back(t.sub(searchWord.substr(0, i)));\n }\n return ans;\n }\n};", "memory": "419690" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode {\npublic:\n char value;\n TrieNode* children[26];\n bool isterminal;\n\n TrieNode(char val) {\n this->value = val;\n for (int i = 0; i < 26; i++) {\n children[i] = NULL;\n }\n isterminal = false;\n }\n};\n\nclass Solution {\npublic:\n void insertion(TrieNode* root, string s) {\n if (s.length() == 0) {\n root->isterminal = true;\n return;\n }\n\n char ch = s[0];\n int index = ch - 'a';\n TrieNode* child;\n if (root->children[index] != NULL) {\n child = root->children[index];\n } else {\n child = new TrieNode(ch);\n root->children[index] = child;\n }\n\n insertion(child, s.substr(1));\n }\n\n void printTrie(TrieNode* root, string& s, vector<string>& temp) {\n if (root->isterminal) {\n temp.push_back(s);\n }\n for (int i = 0; i < 26; i++) {\n if (root->children[i]) {\n s.push_back(i + 'a');\n printTrie(root->children[i], s, temp);\n s.pop_back();\n }\n }\n }\n\n void display(TrieNode* root, string s, string& input,\n vector<string>& temp) {\n if (root == NULL) {\n cout << \"NOT FOUND\" << endl;\n return;\n }\n if (s.length() == 0) {\n printTrie(root, input, temp);\n return;\n }\n char ch = s[0];\n input.push_back(ch);\n int index = ch - 'a';\n TrieNode* child = root->children[index];\n display(child, s.substr(1), input, temp);\n }\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n vector<vector<string>> ans;\n sort(begin(products), end(products));\n TrieNode* root = new TrieNode('-');\n for (int i = 0; i < products.size(); i++) {\n insertion(root, products[i]);\n }\n string s = \"\";\n for (int i = 0; i < searchWord.length(); i++) {\n s += searchWord[i];\n vector<string> temp = {};\n string j = \"\";\n display(root, s, j, temp);\n vector<string> temp2;\n if (temp.size() > 3) {\n temp2.push_back(temp[0]);\n temp2.push_back(temp[1]);\n temp2.push_back(temp[2]);\n } \n else {\n for (int i = 0; i < temp.size(); i++) {\n temp2.push_back(temp[i]);\n }\n }\n ans.push_back(temp2);\n }\n return ans;\n }\n};", "memory": "427470" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "auto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\nclass Trie {\npublic:\n bool ed[50007];\n int child[26][50007], num;\n Trie() {\n memset(ed, 0, sizeof(ed));\n memset(child, 0, sizeof(child));\n num = 0;\n }\n \n int getChild(int cur, char c)\n {\n return child[c - 'a'][cur];\n }\n\n bool isEd(int cur)\n {\n return ed[cur];\n }\n\n void insert(string word) {\n int cur = 0;\n for (char c : word)\n {\n if (!child[c - 'a'][cur]) child[c - 'a'][cur] = ++num;\n cur = child[c - 'a'][cur];\n }\n ed[cur] = 1;\n }\n\n void dfs(int u, string sug, vector<string>& suggestions)\n {\n if (suggestions.size() == 3) return;\n if (isEd(u)) \n {\n suggestions.push_back(sug);\n // return;\n }\n for (int i = 0; i < 26; i++)\n {\n int v = getChild(u, i + 'a');\n if (v) dfs(v, sug + char(i + 'a'), suggestions);\n }\n } \n};\n\nclass Solution {\npublic:\n Trie* trie;\n Solution() {\n trie = new Trie();\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n for (string product : products) {\n trie->insert(product);\n }\n vector<vector<string>> ans;\n string tmp;\n int cur = 0;\n for (char ch : searchWord) {\n tmp += ch;\n cur = trie->getChild(cur, ch);\n if (cur == 0) break; // If no child exists, break the loop\n vector<string> suggestions;\n trie->dfs(cur, tmp, suggestions);\n ans.push_back(suggestions);\n }\n // Fill remaining characters with empty suggestions if cur == 0\n while (ans.size() < searchWord.size()) {\n ans.push_back({});\n }\n return ans;\n }\n\n ~Solution() {\n delete trie;\n }\n};", "memory": "435250" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& prod, string sword) {\n vector<vector<string>> res;\n vector<string> tmp;\n int idx = 0, pre_idx = 0, str_size = 0;\n\n sort(prod.begin(),prod.end());\n for(int i = 0; i < sword.size(); i++)\n {\n idx = pre_idx, str_size = i+1;\n while(idx < prod.size())\n {\n if(prod[idx].substr(0,str_size) == sword.substr(0,str_size))\n {\n if(tmp.empty()) pre_idx = idx;\n tmp.push_back(prod[idx]);\n }\n if(tmp.size() == 3) break;\n idx++;\n }\n res.push_back(tmp);\n tmp.clear();\n }\n\n return res;\n }\n};", "memory": "443030" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& prod, string sword) {\n vector<vector<string>> res;\n vector<string> tmp;\n int idx = 0, pre_idx = 0, str_size = 0;\n\n sort(prod.begin(),prod.end());\n for(int i = 0; i < sword.size(); i++)\n {\n idx = pre_idx, str_size = i+1;\n while(idx < prod.size())\n {\n if(prod[idx].substr(0,str_size) == sword.substr(0,str_size))\n {\n if(tmp.empty()) pre_idx = idx;\n tmp.push_back(prod[idx]);\n }\n if(tmp.size() == 3) break;\n idx++;\n }\n res.push_back(tmp);\n tmp.clear();\n }\n\n return res;\n }\n};", "memory": "450810" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>> ans;\n // int cnt=0;\n int prev=0;\n for(int cnt=1;cnt<=searchWord.size();cnt++){\n vector<string>v;\n int temp=1;\n for(int i=prev;i<products.size();i++){\n if(products[i].substr(0,cnt)==searchWord.substr(0,cnt)){\n int temp2=i;\n prev=i;\n while(temp<=3 && temp2<products.size()){\n if(products[temp2].substr(0,cnt)==searchWord.substr(0,cnt)){\n v.push_back(products[temp2]);\n }else{\n break;\n }\n temp2++;\n temp++;\n\n }\n break;\n\n }\n }\n ans.push_back(v);\n }\nreturn ans;\n \n }\n};", "memory": "458590" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "auto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\nclass Trie {\npublic:\n bool ed[20007];\n int child[26][20007], num;\n Trie() {\n memset(ed, 0, sizeof(ed));\n memset(child, 0, sizeof(child));\n num = 0;\n }\n \n int getChild(int cur, char c)\n {\n return child[c - 'a'][cur];\n }\n\n bool isEd(int cur)\n {\n return ed[cur];\n }\n\n void insert(string word) {\n int cur = 0;\n for (char c : word)\n {\n if (!child[c - 'a'][cur]) child[c - 'a'][cur] = ++num;\n cur = child[c - 'a'][cur];\n }\n ed[cur] = 1;\n }\n\n void dfs(int u, string sug, vector<string>& suggestions)\n {\n if (suggestions.size() == 3) return;\n if (isEd(u)) \n {\n suggestions.push_back(sug);\n }\n for (int i = 0; i < 26; i++)\n {\n int v = getChild(u, i + 'a');\n if (v) dfs(v, sug + char(i + 'a'), suggestions);\n }\n } \n};\n\nclass Solution {\npublic:\n Trie* trie;\n Solution() {\n trie = new Trie();\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n for (string product : products) {\n trie->insert(product);\n }\n vector<vector<string>> ans;\n string tmp;\n int cur = 0;\n for (char ch : searchWord) {\n tmp += ch;\n cur = trie->getChild(cur, ch);\n if (cur == 0) break; \n vector<string> suggestions;\n trie->dfs(cur, tmp, suggestions);\n ans.push_back(suggestions);\n }\n while (ans.size() < searchWord.size()) {\n ans.push_back({});\n }\n return ans;\n }\n\n ~Solution() {\n delete trie;\n }\n};", "memory": "466370" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(string& s1,string& s2,int i){\n // cout<<i<<\" \"<<s1.substr(0,min((int)s1.size(),i))<<\" \"<<s2.substr(0,i)<<endl;\n return s1.substr(0,min((int)s1.size(),i))>=s2.substr(0,i);\n }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n int n=searchWord.size();\n vector<vector<string>> ans;\n \n for(int i=1;i<=n;i++){\n int lo=0,hi=(int)products.size()-1;\n int cnt=-1;\n while(lo<=hi){\n int mid=lo+(hi-lo)/2;\n if(check(products[mid],searchWord,i)){\n cnt=mid;\n hi=mid-1;\n }\n else lo=mid+1;\n }\n // cout<<i<<\" \"<<cnt<<endl;\n int j=0;\n vector<string> res;\n while(cnt!=-1 && cnt<(int)products.size()){\n // if(i>(int)products[cnt].size()){\n // cnt++;\n // continue;\n // }\n if(products[cnt].substr(0,i)==searchWord.substr(0,i)){\n res.push_back(products[cnt]);\n j++;\n }\n if(j==3)break;\n cnt++;\n }\n // for(auto it:res)cout<<it<<\" \";\n // cout<<endl;\n ans.push_back(res);\n }\n return ans;\n }\n};", "memory": "474150" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n bool check(string& s1,string& s2,int i){\n // cout<<i<<\" \"<<s1.substr(0,min((int)s1.size(),i))<<\" \"<<s2.substr(0,i)<<endl;\n return s1.substr(0,min((int)s1.size(),i))>=s2.substr(0,i);\n }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n int n=searchWord.size();\n vector<vector<string>> ans;\n \n for(int i=1;i<=n;i++){\n int lo=0,hi=(int)products.size()-1;\n int cnt=-1;\n while(lo<=hi){\n int mid=lo+(hi-lo)/2;\n if(check(products[mid],searchWord,i)){\n cnt=mid;\n hi=mid-1;\n }\n else lo=mid+1;\n }\n // cout<<i<<\" \"<<cnt<<endl;\n int j=0;\n vector<string> res;\n while(cnt!=-1 && cnt<(int)products.size()){\n // if(i>(int)products[cnt].size()){\n // cnt++;\n // continue;\n // }\n if(products[cnt].substr(0,i)==searchWord.substr(0,i)){\n res.push_back(products[cnt]);\n j++;\n }\n if(j==3)break;\n cnt++;\n }\n // for(auto it:res)cout<<it<<\" \";\n // cout<<endl;\n ans.push_back(res);\n }\n return ans;\n }\n};", "memory": "481930" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct node {\n node (char let) : l(let), isEnd(false) {}\n char l;\n bool isEnd;\n std::vector<node*> ptrs;\n};\n\nclass Solution {\npublic:\n Solution() {\n root = new node('\\0');\n }\n\n std::vector<std::vector<std::string>> suggestedProducts(std::vector<std::string>& products, std::string searchWord) {\n int len = products.size(), lenWord = searchWord.length();\n std::string prefix = \"\";\n std::vector<std::vector<std::string>> vec;\n\n std::sort(products.begin(), products.end());\n\n // Вставляем все продукты в дерево\n for (int i = 0; i < len; ++i) {\n insert(products[i]);\n }\n\n // Для каждого префикса вызываем поиск\n for (int i = 0; i < lenWord; ++i) {\n prefix += searchWord[i];\n vec.push_back(search(prefix));\n }\n\n return vec;\n }\n\nprivate:\n void insert(std::string word) {\n node* curr = root;\n\n for (char letter : word) {\n node* nextNode = nullptr;\n bool found = false;\n\n for (node* child : curr->ptrs) {\n if (child->l == letter) {\n nextNode = child;\n found = true;\n break;\n }\n }\n\n if (!found) {\n nextNode = new node(letter);\n curr->ptrs.push_back(nextNode);\n }\n curr = nextNode;\n }\n curr->isEnd = true;\n }\n\n std::vector<std::string> search(std::string prefix) {\n node* curr = root;\n std::vector<std::string> pack;\n\n // Находим узел, соответствующий последнему символу префикса\n for (char letter : prefix) {\n bool found = false;\n for (node* child : curr->ptrs) {\n if (child->l == letter) {\n curr = child;\n found = true;\n break;\n }\n }\n if (!found) return pack; // Если префикс не найден, возвращаем пустой результат\n }\n\n // Собираем все слова, начиная с узла текущего префикса\n dfs(curr, prefix, pack);\n\n // Возвращаем максимум три слова\n if (pack.size() > 3) pack.resize(3);\n\n return pack;\n }\n\n void dfs(node* curr, std::string word, std::vector<std::string>& pack) {\n if (curr->isEnd) pack.push_back(word);\n if (pack.size() == 3) return; // Прерываем поиск, если нашли три слова\n\n for (node* child : curr->ptrs) {\n dfs(child, word + child->l, pack);\n if (pack.size() == 3) return;\n }\n }\n \n node* root;\n};\n", "memory": "489710" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct node {\n node (char let) : l(let), isEnd(false) {}\n char l;\n bool isEnd;\n std::vector<node*> ptrs;\n};\n\nclass Solution {\npublic:\n Solution() {\n root = new node('\\0');\n }\n\n std::vector<std::vector<std::string>> suggestedProducts(std::vector<std::string>& products, std::string searchWord) {\n int len = products.size(), lenWord = searchWord.length();\n std::string prefix = \"\";\n std::vector<std::vector<std::string>> vec;\n\n std::sort(products.begin(), products.end());\n\n // Вставляем все продукты в дерево\n for (int i = 0; i < len; ++i) {\n insert(products[i]);\n }\n\n // Для каждого префикса вызываем поиск\n for (int i = 0; i < lenWord; ++i) {\n prefix += searchWord[i];\n vec.push_back(search(prefix));\n }\n\n return vec;\n }\n\nprivate:\n void insert(std::string word) {\n node* curr = root;\n\n for (char letter : word) {\n node* nextNode = nullptr;\n bool found = false;\n\n for (node* child : curr->ptrs) {\n if (child->l == letter) {\n nextNode = child;\n found = true;\n break;\n }\n }\n\n if (!found) {\n nextNode = new node(letter);\n curr->ptrs.push_back(nextNode);\n }\n curr = nextNode;\n }\n curr->isEnd = true;\n }\n\n std::vector<std::string> search(std::string prefix) {\n node* curr = root;\n std::vector<std::string> pack;\n\n // Находим узел, соответствующий последнему символу префикса\n for (char letter : prefix) {\n bool found = false;\n for (node* child : curr->ptrs) {\n if (child->l == letter) {\n curr = child;\n found = true;\n break;\n }\n }\n if (!found) return pack; // Если префикс не найден, возвращаем пустой результат\n }\n\n // Собираем все слова, начиная с узла текущего префикса\n dfs(curr, prefix, pack);\n\n // Возвращаем максимум три слова\n if (pack.size() > 3) pack.resize(3);\n\n return pack;\n }\n\n void dfs(node* curr, std::string word, std::vector<std::string>& pack) {\n if (curr->isEnd) pack.push_back(word);\n if (pack.size() == 3) return; // Прерываем поиск, если нашли три слова\n\n for (node* child : curr->ptrs) {\n dfs(child, word + child->l, pack);\n if (pack.size() == 3) return;\n }\n }\n \n node* root;\n};\n", "memory": "489710" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n\nprivate:\n\nstruct LetterNode_st;\n\ntypedef struct ChildNode_st {\n struct ChildNode_st* next;\n struct LetterNode_st* child;\n} ChildNode;\n\ntypedef struct LetterNode_st {\n char letter;\n bool is_word;\n ChildNode* children;\n} LetterNode;\n\nLetterNode* mRoot;\n\nvoid createTree( vector<string>& products ){\n mRoot = nullptr;\n mRoot = (LetterNode*) malloc(sizeof(LetterNode));\n mRoot->letter = '\\n';\n mRoot->children = nullptr;\n\n LetterNode* tmpNL = nullptr;\n LetterNode* tmpL = nullptr;\n ChildNode* tmpNC = nullptr;\n ChildNode* tmpC = nullptr;\n for( auto itw = products.begin(); itw != products.end(); ++itw ){\n string word = *itw;\n tmpL = mRoot;\n //cout << \"word = \" << word << endl;\n for( char chara : word ) {\n tmpC = tmpL->children;\n if(tmpC == nullptr) {\n //cout << \"Letter \" << tmpL->letter << \" has no children, adding \" << chara << \" as child\" << endl;\n tmpNL = (LetterNode*) malloc(sizeof(LetterNode));\n tmpNL->letter = chara;\n tmpNL->is_word = false;\n tmpNL->children = nullptr;\n \n tmpC = (ChildNode*) malloc(sizeof(ChildNode));\n tmpC->child = tmpNL;\n tmpC->next = nullptr;\n tmpL->children = tmpC;\n\n tmpL = tmpNL;\n continue;\n }\n\n while( tmpC->next != nullptr && tmpC->child->letter < chara ) {\n //cout << \"Moving to letter \" << tmpC->child->letter << endl;\n tmpC = tmpC->next;\n }\n\n if(tmpC->child->letter == chara ){\n //cout << \"Letter \" << chara << \" exists, following the letter\" << endl;\n tmpL = tmpC->child;\n continue;\n }\n if(tmpC->child->letter > chara){\n //cout << \"Letter \" << chara << \" does not exists, adding it in the right spot\" << endl;\n\n tmpNC = (ChildNode*) malloc(sizeof(ChildNode));\n tmpNC->child = tmpC->child;\n tmpNC->next = tmpC->next;\n tmpC->next = tmpNC;\n\n tmpNL = (LetterNode*) malloc(sizeof(LetterNode));\n tmpNL->letter = chara;\n tmpNL->is_word = false;\n tmpNL->children = nullptr;\n tmpC->child = tmpNL;\n\n tmpL = tmpC->child;\n continue;\n }\n \n if(tmpC->next == nullptr){\n //cout << \"Letter \" << chara << \" does not exists, adding it at the end\" << endl;\n tmpNL = (LetterNode*) malloc(sizeof(LetterNode));\n tmpNL->letter = chara;\n tmpNL->is_word = false;\n tmpNL->children = nullptr;\n\n tmpNC = (ChildNode*) malloc(sizeof(ChildNode));\n tmpNC->child = tmpNL;\n tmpNC->next = nullptr;\n tmpC->next = tmpNC;\n\n tmpL = tmpNC->child;\n }\n }\n tmpL->is_word = true;\n }\n}\n\nvoid takeThreeWords(LetterNode* startL, string baseWord, vector<string> &words){\n if( words.size() == 3 ){\n return;\n }\n if( startL == nullptr ){\n return;\n }\n if( startL->is_word ){\n words.push_back(baseWord);\n }\n ChildNode* tmpC = startL->children;\n while( tmpC!=nullptr && words.size() < 3 ){\n //cout << \"baseWord = \" << baseWord << endl;\n //cout << \"tmpC->child->letter = \" << tmpC->child->letter << endl;\n takeThreeWords(\n tmpC->child,\n baseWord+tmpC->child->letter,\n words);\n tmpC = tmpC->next;\n }\n\n}\n\nvector<string> searchFor(string searchWord){\n LetterNode* tmpL = mRoot;\n ChildNode* tmpC = nullptr;\n for( char chara : searchWord ){\n\n tmpC = tmpL->children;\n if(tmpC == nullptr) {\n tmpL = nullptr;\n break;\n }\n\n while( tmpC->next != nullptr && tmpC->child->letter < chara ) {\n tmpC = tmpC->next;\n }\n\n if(tmpC->child->letter == chara ){\n tmpL = tmpC->child;\n continue;\n }\n tmpL = nullptr;\n break;\n }\n\n /*\n cout << \"searchWord = \" << searchWord << endl;\n if(tmpL != nullptr){\n cout << \"tmpL->letter = \" << tmpL->letter << endl;\n } else {\n cout << \"tmpL->letter is empty\" << endl;\n }\n */\n\n // tmpL contains the last matched letter\n vector<string> words;\n takeThreeWords(tmpL, searchWord, words);\n\n return words;\n\n}\n\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n \n createTree( products );\n\n string partialWord = \"\";\n vector<vector<string>> sol;\n for( size_t idx = 0; idx < searchWord.length(); ++idx ) {\n partialWord += searchWord[idx];\n sol.push_back(searchFor(partialWord));\n }\n return sol;\n }\n};", "memory": "497490" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TrieNode {\n map<char, TrieNode*> children;\n bool isEndOfWord = false;\n };\n\n class Trie {\n public:\n TrieNode* root;\n \n Trie() {\n root = new TrieNode();\n }\n \n void insert(const string& word) {\n TrieNode* node = root;\n for (char c : word) {\n if (!node->children[c]) {\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n }\n node->isEndOfWord = true;\n }\n \n vector<string> getWordsStartingWith(const string& prefix) {\n vector<string> words;\n TrieNode* node = root;\n for (char c : prefix) {\n if (!node->children[c]) {\n return words;\n }\n node = node->children[c];\n }\n dfs(node, prefix, words);\n return words;\n }\n \n private:\n void dfs(TrieNode* node, string currentWord, vector<string>& words) {\n if (words.size() == 3) return; // limit to 3 suggestions\n if (node->isEndOfWord) {\n words.push_back(currentWord);\n }\n for (auto& child : node->children) {\n dfs(child.second, currentWord + child.first, words);\n }\n }\n };\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n Trie trie;\n for (const string& product : products) {\n trie.insert(product);\n }\n \n vector<vector<string>> result;\n string prefix = \"\";\n for (char c : searchWord) {\n prefix += c;\n result.push_back(trie.getWordsStartingWith(prefix));\n }\n \n return result;\n }\n};", "memory": "505270" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode {\npublic:\n char data;\n map<int, TrieNode*> children;\n bool isEnd;\n\n TrieNode() {\n data = '-';\n isEnd = false;\n }\n\n TrieNode(char c) {\n data = c;\n isEnd = false;\n }\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n\n Trie() {\n root = new TrieNode();\n }\n\n void insertWord(const string& word) {\n TrieNode* curr = root;\n for (char c : word) {\n int index = c - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n curr->children[index] = new TrieNode(c);\n }\n curr = curr->children[index];\n }\n curr->isEnd = true;\n }\n\n vector<string> getSuggestions(const string& prefix) {\n vector<string> suggestions;\n TrieNode* curr = root;\n\n // Traverse the Trie to the end of the prefix\n for (char ch : prefix) {\n int index = ch - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n return {}; // Prefix not found, return empty vector\n }\n curr = curr->children[index];\n }\n\n // Gather up to 3 suggestions starting from the current TrieNode\n gatherSuggestions(curr, prefix, suggestions);\n return suggestions;\n }\n\n void gatherSuggestions(TrieNode* curr, const string& prefix, vector<string>& suggestions) {\n if (!curr || suggestions.size() == 3) return;\n\n if (curr->isEnd) {\n suggestions.push_back(prefix);\n }\n\n for (auto& it : curr->children) {\n gatherSuggestions(it.second, prefix + it.second->data, suggestions);\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie trie;\n\n // Insert all products into the Trie\n for (const string& s : products) {\n trie.insertWord(s);\n }\n\n vector<vector<string>> result;\n string prefix = \"\";\n\n for (char ch : searchWord) {\n prefix += ch;\n\n // Get suggestions for the current prefix\n vector<string> suggestions = trie.getSuggestions(prefix);\n result.push_back(suggestions);\n }\n\n return result;\n }\n};\n\n", "memory": "513050" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode {\npublic:\n char data;\n map<int, TrieNode*> children;\n bool isEnd;\n\n TrieNode(): data('-'), isEnd(false) {};\n\n TrieNode(char c) {\n data = c;\n isEnd = false;\n }\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n\n Trie() {\n root = new TrieNode();\n }\n\n void insertWord(const string& word) {\n TrieNode* curr = root;\n for (char c : word) {\n int index = c - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n curr->children[index] = new TrieNode(c);\n }\n curr = curr->children[index];\n }\n curr->isEnd = true;\n }\n\n vector<string> getSuggestions(const string& prefix) {\n vector<string> suggestions;\n TrieNode* curr = root;\n\n // Traverse the Trie to the end of the prefix\n for (char ch : prefix) {\n int index = ch - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n return {}; // Prefix not found, return empty vector\n }\n curr = curr->children[index];\n }\n\n // Gather up to 3 suggestions starting from the current TrieNode\n gatherSuggestions(curr, prefix, suggestions);\n return suggestions;\n }\n\n void gatherSuggestions(TrieNode* curr, const string& prefix, vector<string>& suggestions) {\n if (!curr || suggestions.size() == 3) return;\n\n if (curr->isEnd) {\n suggestions.push_back(prefix);\n }\n\n for (auto& it : curr->children) {\n gatherSuggestions(it.second, prefix + it.second->data, suggestions);\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie trie;\n\n // Insert all products into the Trie\n for (const string& s : products) {\n trie.insertWord(s);\n }\n\n vector<vector<string>> result;\n string prefix = \"\";\n\n for (char ch : searchWord) {\n prefix += ch;\n\n // Get suggestions for the current prefix\n vector<string> suggestions = trie.getSuggestions(prefix);\n result.push_back(suggestions);\n }\n\n return result;\n }\n};\n\n", "memory": "513050" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode {\npublic:\n char data;\n map<int, TrieNode*> children;\n bool isEnd;\n TrieNode(): data('-'), isEnd(false) {};\n TrieNode(char c): data(c), isEnd(false) {};\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n Trie() : root(new TrieNode()) {};\n //function to insert word in trie\n void insertWord(const string& word) {\n TrieNode* curr = root;\n for (char c : word) {\n int index = c - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n curr->children[index] = new TrieNode(c);\n }\n curr = curr->children[index];\n }\n curr->isEnd = true;\n }\n //getting suggestions from trie\n vector<string> getSuggestions(const string& prefix) {\n vector<string> suggestions;\n TrieNode* curr = root;\n // Traverse the Trie to the end of the prefix\n for (char ch : prefix) {\n int index = ch - 'a';\n if (curr->children.find(index) == curr->children.end()) {\n return {}; // Prefix not found, return empty vector\n }\n curr = curr->children[index];\n }\n // Gather up to 3 suggestions starting from the current TrieNode\n gatherSuggestions(curr, prefix, suggestions);\n return suggestions;\n }\n //pushing string that exist in trie in suggestions vector\n void gatherSuggestions(TrieNode* curr, const string& prefix, vector<string>& suggestions) {\n if (suggestions.size() == 3) return;\n if (curr->isEnd) {\n suggestions.push_back(prefix);\n }\n\n for (auto& it : curr->children) {\n gatherSuggestions(it.second, prefix + it.second->data, suggestions);\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie trie;\n\n // Insert all products into the Trie\n for (const string& s : products) {\n trie.insertWord(s);\n }\n\n vector<vector<string>> result;\n string prefix = \"\";\n\n for (char ch : searchWord) {\n prefix += ch;\n\n // Get suggestions for the current prefix\n vector<string> suggestions = trie.getSuggestions(prefix);\n result.push_back(suggestions);\n }\n\n return result;\n }\n};\n\n", "memory": "520830" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Trie{\npublic:\n std::map<char, Trie*> children;\n bool is_end = false;\n};\n\nclass Solution {\nprivate:\n const int max_nof_strs = 3;\n void get_trie_strs(Trie* node, string buf_str, vector<string>& dst_strs){\n if(dst_strs.size() >= max_nof_strs) return;\n\n if(node->is_end){\n dst_strs.push_back(buf_str);\n }\n\n for(auto itr = node->children.begin(); itr != node->children.end(); ++itr){\n char cur_char = itr->first;\n Trie* next = itr->second;\n get_trie_strs(next, buf_str + cur_char, dst_strs);\n }\n }\n\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie* head = new Trie;\n head->is_end = false;\n for(string& cur_string : products){\n Trie* node = head;\n for(int i_char = 0; i_char < cur_string.length(); ++i_char){\n char cur_char = cur_string[i_char];\n if(node->children.find(cur_char) == node->children.end()){\n node->children[cur_char] = new Trie();\n }\n node = node->children[cur_char];\n }\n node->is_end = true;\n }\n\n vector<vector<string>> result;\n string buf_str;\n Trie* node = head;\n for(int i_char = 0; i_char < searchWord.size(); ++i_char){\n char cur_char = searchWord[i_char];\n buf_str += cur_char;\n vector<string> cur_strs;\n if(node != nullptr && node->children.find(cur_char) == node->children.end()){\n node = nullptr;\n }\n if(node != nullptr){\n node = node->children[cur_char];\n this->get_trie_strs(node, buf_str, cur_strs);\n }\n result.push_back(cur_strs);\n }\n\n return result;\n }\n};", "memory": "528610" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n class TrieNode{\n public:\n map<char, TrieNode*> children;\n bool end;\n TrieNode(){\n end = false;\n }\n };\n TrieNode* root;\npublic:\n void addProducts(vector<string>& products){\n for(int i=0; i<products.size(); i++){\n TrieNode* iter = root;\n for(int j=0; j<products[i].length(); j++){\n if(iter->children[products[i][j]] == NULL){\n iter->children[products[i][j]] = new TrieNode();\n }\n iter = iter->children[products[i][j]];\n }\n iter->end = true;\n }\n }\n\n void getThree(TrieNode* iter, int& count, vector<string>& ans, string t){\n if(!iter || count >=3)\n return;\n if(iter->end){\n ans.push_back(t);\n count++;\n }\n for(auto i: iter->children){\n if(i.second){\n getThree(i.second,count, ans, t+i.first);\n }\n }\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n root = new TrieNode();\n addProducts(products);\n vector<vector<string>> ans;\n TrieNode* iter = root;\n string w = \"\";\n for(int i=0; i<searchWord.length(); i++){\n vector<string> temp; \n if(!iter){\n ans.push_back(temp);\n continue;\n }\n iter = iter->children[searchWord[i]];\n w += searchWord[i];\n int ct = 0;\n getThree(iter, ct, temp, w);\n ans.push_back(temp);\n }\n return ans;\n }\n};", "memory": "536390" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct TrieNode {\n map<char, TrieNode*> m;\n bool isEnd = false;\n};\nclass Solution {\n TrieNode *root = new TrieNode();\npublic:\n void getThreeWord(string s, TrieNode *root, vector<string>&words){\n if (!root || words.size() == 3)\n return;\n\n if (root->isEnd)\n {\n words.push_back(s);\n }\n\n for (pair<char, TrieNode*> p : root->m)\n {\n if (words.size()<3)\n {\n getThreeWord(s+p.first, p.second, words);\n }\n }\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n for (string s: products)\n {\n TrieNode *traverse = root;\n for (char c : s)\n {\n if (!traverse->m.count(c))\n {\n traverse->m[c] = new TrieNode();\n }\n\n traverse = traverse->m[c]; \n }\n traverse->isEnd = true;\n }\n\n\n TrieNode *traverse = root;\n vector<vector<string>> result;\n string s = \"\";\n for (char c : searchWord)\n {\n vector<string> words;\n s += c;\n if (traverse)\n {\n getThreeWord(s, traverse->m[c], words);\n traverse = traverse->m[c];\n }\n \n result.push_back(words);\n \n }\n\n return result;\n \n }\n};", "memory": "536390" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct TrieNode\n{\n map<char, TrieNode*> children;\n bool isEndOfWord;\n \n TrieNode() : isEndOfWord(false) {}\n};\n\nclass Trie\n{\nprivate:\n TrieNode *root;\n \n void findWordsWithPrefix(TrieNode* node, string currentWord, vector<string>& result) {\n // Nếu đã đủ 3 từ, ta dừng việc tìm kiếm\n if (result.size() >= 3) return;\n \n // Nếu node hiện tại là kết thúc của một từ, thêm từ này vào kết quả\n if (node->isEndOfWord) {\n result.push_back(currentWord);\n }\n \n // Duyệt qua tất cả các nhánh con của node hiện tại\n for (auto& child : node->children) {\n findWordsWithPrefix(child.second, currentWord + child.first, result);\n }\n }\n \npublic:\n Trie() {\n root = new TrieNode();\n }\n \n void insert(string word) {\n TrieNode *node = root;\n for (const char ch : word)\n {\n if (node->children.find(ch) == node->children.end())\n {\n node->children[ch] = new TrieNode();\n }\n \n node = node->children[ch];\n }\n \n node->isEndOfWord = true;\n }\n \n bool search(string word) {\n TrieNode *node = root;\n for (const char& ch : word)\n {\n if (node->children.find(ch) == node->children.end())\n {\n return false;\n }\n node = node->children[ch];\n }\n \n return node->isEndOfWord;\n }\n \n bool startsWith(string prefix) {\n TrieNode *node = root;\n for (const char& ch : prefix)\n {\n if (node->children.find(ch) == node->children.end())\n {\n return false;\n }\n node = node->children[ch];\n }\n return true;\n }\n\n vector<string> getWordsWithPrefix(string prefix) {\n vector<string> result;\n TrieNode *node = root;\n // Tìm nút cuối cùng tương ứng với prefix\n for (const char& ch : prefix)\n {\n if (node->children.find(ch) == node->children.end())\n {\n return result; // Trả về rỗng nếu không tìm thấy prefix\n }\n node = node->children[ch];\n }\n // Gọi hàm bổ trợ để tìm kiếm các từ bắt đầu bằng prefix\n findWordsWithPrefix(node, prefix, result);\n return result;\n }\n};\n\nclass Solution\n{\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord)\n {\n sort(products.begin(), products.end());\n\n Trie obj;\n vector<vector<string>> result;\n \n for (const string& str : products)\n {\n obj.insert(str);\n }\n \n string curr_word = \"\";\n for (int i = 0; i < searchWord.size(); i++)\n {\n curr_word += searchWord[i];\n \n vector<string> temp = obj.getWordsWithPrefix(curr_word);\n// sort(temp.begin(), temp.end());\n \n result.push_back(temp);\n }\n \n return result;\n }\n};", "memory": "544170" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct TrieNode{\n TrieNode* children[26];\n bool isTerminal = false;\n};\nclass Solution {\npublic:\n void insert(TrieNode* root,string n){\n for(int i = 0;i < n.size();i++){\n if(root->children[n[i] - 'a']){\n root = root->children[n[i] - 'a'];\n }\n else{\n root->children[n[i] - 'a'] = new TrieNode();\n root = root->children[n[i] - 'a'];\n }\n if(i == n.size() - 1){\n root->isTerminal = true;\n }\n }\n }\n void comb(TrieNode* root, string st, vector<string>& combinations) {\n if (combinations.size() >= 3) return;\n if (root->isTerminal) {\n combinations.push_back(st);\n }\n for (int i = 0; i < 26; ++i) {\n if (root->children[i]) {\n comb(root->children[i], st + char(i + 'a'), combinations);\n }\n }\n }\n // void findResults(TrieNode* root, const string& search, vector<vector<string>>& ans) {\n // TrieNode* current = root;\n // string prefix;\n // for (char c : search) {\n // prefix += c;\n // vector<string> result;\n // int idx = c - 'a';\n // if (current->children[idx]) {\n // current = current->children[idx];\n // comb(current, prefix, result);\n // }\n // ans.push_back(result);\n // }\n // }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>> ans;\n TrieNode* root = new TrieNode();\n for(string st : products){\n insert(root,st);\n }\n TrieNode* node = root;\n string prefix;\n for (char c : searchWord) {\n prefix += c;\n int index = c - 'a';\n if (node->children[index] == nullptr) {\n node->children[index] = new TrieNode(); // Ensure all nodes exist\n }\n node = node->children[index];\n \n vector<string> suggestions;\n comb(node, prefix, suggestions);\n ans.push_back(suggestions);\n }\n return ans;\n }\n};", "memory": "567510" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct TrieNode{\n TrieNode* children[26];\n bool isTerminal = false;\n};\nclass Solution {\npublic:\n void insert(TrieNode* root,string n){\n for(int i = 0;i < n.size();i++){\n if(root->children[n[i] - 'a']){\n root = root->children[n[i] - 'a'];\n }\n else{\n root->children[n[i] - 'a'] = new TrieNode();\n root = root->children[n[i] - 'a'];\n }\n if(i == n.size() - 1){\n root->isTerminal = true;\n }\n }\n }\n void comb(TrieNode* root, string st, vector<string>& combinations) {\n if (combinations.size() >= 3) return;\n if (root->isTerminal) {\n combinations.push_back(st);\n }\n for (int i = 0; i < 26; ++i) {\n if (root->children[i]) {\n comb(root->children[i], st + char(i + 'a'), combinations);\n }\n }\n }\n // void findResults(TrieNode* root, const string& search, vector<vector<string>>& ans) {\n // TrieNode* current = root;\n // string prefix;\n // for (char c : search) {\n // prefix += c;\n // vector<string> result;\n // int idx = c - 'a';\n // if (current->children[idx]) {\n // current = current->children[idx];\n // comb(current, prefix, result);\n // }\n // ans.push_back(result);\n // }\n // }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>> ans;\n TrieNode* root = new TrieNode();\n for(string st : products){\n insert(root,st);\n }\n TrieNode* node = root;\n string prefix;\n for (char c : searchWord) {\n prefix += c;\n int index = c - 'a';\n if (node->children[index] == nullptr) {\n node->children[index] = new TrieNode(); // Ensure all nodes exist\n }\n node = node->children[index];\n \n vector<string> suggestions;\n comb(node, prefix, suggestions);\n ans.push_back(suggestions);\n }\n return ans;\n }\n};", "memory": "567510" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n vector<Solution*>child;\n Solution * head;\n bool is_end;\npublic:\n Solution()\n {\n child.resize(26);\n is_end = false;\n }\n void insert(string word)\n {\n if(head==NULL) head = new Solution();\n Solution * temp = head;\n for(char c : word)\n {\n if(temp->child[c-'a']==NULL)\n {\n temp->child[c-'a'] = new Solution();\n }\n temp = temp->child[c-'a'];\n }\n temp->is_end= true;\n }\n\n void find_word(Solution * s, string prefix, vector<string>&v)\n {\n if(!s) return;\n\n if(v.size()>=3) return;\n\n if(s->is_end)\n {\n v.push_back(prefix);\n }\n\n bool flag = false;\n\n for(int i=0; i<26; i++)\n {\n if(s->child[i])\n {\n find_word(s->child[i], prefix + char('a'+i), v);\n flag = true;\n }\n } \n\n return;\n }\n\n vector<string> find_words( string p)\n {\n vector<string>words;\n Solution * temp =head;\n for(char c : p)\n {\n if(temp->child[c-'a']) temp = temp->child[c-'a'];\n else return words;\n }\n find_word( temp,p, words);\n return words;\n }\n\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n for(int i=0; i<products.size(); i++)\n {\n insert(products[i]);\n }\n vector<vector<string>>answer;\n\n for(int i=0; i<searchWord.size(); i++)\n {\n string tmp = searchWord.substr(0,i+1);\n vector<string> temp = find_words(tmp);\n answer.push_back(temp);\n }\n return answer;\n }\n};\n\n//[[\"mobile\"],[\"mobile\",\"moneypot\",\"mousepad\"],[\"mousepad\"],[\"mousepad\"],[\"mousepad\"]]", "memory": "575290" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class TrieNode {\npublic:\n explicit TrieNode(char v) : value(v) {\n\n }\n char value;\n map<char, TrieNode*> children;\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n Trie() {\n root = new TrieNode(1);\n }\n\n void build(const string& word) {\n TrieNode* curr = root;\n for (auto c : word) {\n auto it = curr->children.find(c);\n if (it == curr->children.end()) {\n TrieNode* newNode = new TrieNode(c);\n curr->children[c] = newNode;\n curr = newNode;\n }\n else {\n curr = it->second;\n }\n }\n curr->children[0] = new TrieNode(0);\n }\n\n vector<string> search(const string& word) {\n string prefix = \"\";\n TrieNode* curr = root;\n for (auto c : word) {\n auto it = curr->children.find(c);\n if (it != curr->children.end()) {\n curr = it->second;\n prefix += curr->value;\n }\n else {\n curr = nullptr;\n return {};\n }\n }\n vector<string> result = completeWord(curr, prefix);\n return result;\n }\n\n vector<string> completeWord(TrieNode* node, string& prefix) {\n vector<string> result;\n string bt;\n dfs(node, prefix, bt, result);\n return result;\n }\n\n void dfs(TrieNode* node, string& prefix, string bt, vector<string>& result) {\n bt += node->value;\n if (node->value == 0) {\n if (bt.size() > 0) {\n result.push_back(prefix + bt.substr(1, bt.size() - 1));\n }\n return;\n }\n for (auto n : node->children) {\n dfs(n.second, prefix, bt, result);\n if (result.size() == 3) {\n return;\n }\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie trie;\n for (auto w : products) {\n trie.build(w);\n }\n vector<vector<string>> ans;\n for (int i = 1; i <= searchWord.size(); i++) {\n ans.push_back(trie.search(searchWord.substr(0, i)));\n }\n return ans;\n }\n};", "memory": "575290" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct Node {\n unique_ptr<Node> child[26];\n bool isTerminal;\n Node() : isTerminal(false) {}\n};\n\nclass Trie {\n unique_ptr<Node> root;\n\npublic:\n Trie() : root(make_unique<Node>()) {}\n\n void add(const string& word) {\n Node* temp = root.get();\n for (char c : word) {\n int index = c - 'a';\n if (!temp->child[index]) {\n temp->child[index] = make_unique<Node>();\n }\n temp = temp->child[index].get();\n }\n temp->isTerminal = true;\n }\n\n void search(const string& prefix, vector<vector<string>>& ans, int idx) {\n Node* temp = root.get();\n for (char c : prefix) {\n int index = c - 'a';\n if (!temp->child[index]) return;\n temp = temp->child[index].get();\n }\n collectSuggestions(temp, prefix, ans, idx);\n }\n\nprivate:\n void collectSuggestions(Node* node, string prefix, vector<vector<string>>& ans, int idx) {\n if (ans[idx].size() >= 3) return; // Stop collecting if we already have 3 suggestions\n if (node->isTerminal) {\n ans[idx].push_back(prefix);\n }\n for (int i = 0; i < 26; ++i) {\n if (node->child[i]) {\n collectSuggestions(node->child[i].get(), prefix + char(i + 'a'), ans, idx);\n }\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, const string& searchWord) {\n Trie trie;\n vector<vector<string>> ans(searchWord.size());\n\n for (const string& product : products) {\n trie.add(product);\n }\n\n for (int i = 1; i <= searchWord.size(); ++i) {\n trie.search(searchWord.substr(0, i), ans, i - 1);\n }\n\n return ans;\n }\n};", "memory": "583070" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "#include <cstddef>\n#include <iostream>\n#include <memory>\n#include <map>\n#include <optional>\n#include <string>\n#include <string_view>\n\n\nclass TrieNode\n{\nprivate:\n typedef std::map<char, std::unique_ptr<TrieNode>> children_t;\npublic:\n void insert(const std::string& word)\n {\n this->insert_(word);\n }\n\n void find(\n const std::size_t match_limit,\n const std::string_view& substring,\n std::vector<std::vector<string>>& output)\n {\n std::string matching;\n auto sub_iter = substring.cbegin();\n output.emplace_back();\n this->find_(match_limit, 0, substring, substring.cend(), sub_iter, matching, output);\n }\nprivate:\n void insert_(const std::string_view& substring)\n {\n if (substring.empty())\n {\n //std::cout << \"reached substring end\" << std::endl;\n children_.emplace('\\0', std::unique_ptr<TrieNode>());\n return;\n }\n auto sub_iter = substring.cbegin();\n std::optional<typename children_t::iterator> child = std::nullopt;\n if (this->children_.contains(*sub_iter))\n {\n //std::cout << \"reusing=\" << *sub_iter << std::endl;\n child.emplace(this->children_.find(*sub_iter));\n assert(child.value() != this->children_.end());\n }\n else\n {\n //std::cout << \"inserting=\" << *sub_iter << std::endl;\n const auto result = this->children_.emplace(*sub_iter, std::make_unique<TrieNode>());\n assert(result.second);\n child.emplace(result.first);\n }\n assert(child.has_value());\n ++sub_iter;\n std::string_view next(sub_iter, substring.end());\n child.value()->second->insert_(next);\n }\n\n std::size_t find_(\n const std::size_t match_limit,\n const std::size_t match_count,\n const std::string_view& substring,\n const std::string_view::iterator& sub_end,\n std::string_view::iterator& sub_iter,\n std::string current_match,\n std::vector<std::vector<string>>& output)\n {\n //std::cout << \"match_limit=\" << match_limit << \", match_count=\" << match_count;\n if (match_count == match_limit)\n {\n //std::cout << \", limit reached\" <<std::endl;\n return match_count;\n }\n std::size_t current_count = match_count;\n if (this->children_.contains('\\0') && substring.size() <= current_match.size())\n {\n //std::cout << \", match=\" << current_match;\n output.back().emplace_back(current_match);\n ++current_count;\n if (this->children_.size() == 1)\n {\n // reached a leaf in the trie\n //std::cout << \", reached leaf\" <<std::endl;\n return current_count;\n }\n }\n if (sub_iter != sub_end && this->children_.contains(*sub_iter))\n {\n current_match.push_back(*sub_iter);\n auto& node_ptr = this->children_[*sub_iter];\n //std::cout << \", sub_iter=\" << *sub_iter << \", current_match=\" << current_match << std::endl;\n ++sub_iter;\n current_count = node_ptr->find_(match_limit, current_count, substring, sub_end, sub_iter, current_match, output);\n //std::cout << \"backtracked to \" << *sub_iter << \", count is now \" << current_count << std::endl;\n current_match.pop_back();\n }\n else if (sub_iter == sub_end)\n {\n // we're already matching the requested substring, so any children from this node will match\n for (auto child_iter = this->children_.cbegin(); current_count < match_limit && child_iter != this->children_.cend(); ++child_iter)\n {\n if (child_iter->first == '\\0')\n {\n continue;\n }\n current_match.push_back(child_iter->first);\n //std::cout << \", current_match=\" << current_match << std::endl;\n current_count = child_iter->second->find_(match_limit, current_count, substring, sub_end, sub_iter, current_match, output);\n //std::cout << \"substring already matched, backtracked to \" << child_iter->first << \", count is now \" << current_count << std::endl;\n current_match.pop_back();\n }\n }\n return current_count;\n }\n\n children_t children_;\n};\n\n\nclass Solution\n{\npublic:\n std::vector<std::vector<std::string>> suggestedProducts(const vector<std::string>& products, const std::string& search_word)\n {\n //std::cout << \"search_word=\" << search_word << \", products=[\" << std::endl;\n //for (const std::string& prod: products)\n //{\n // std::cout << prod << std::endl;\n //}\n //std::cout << \"]\" << std::endl;\n TrieNode root;\n for (const string& prod: products)\n {\n root.insert(prod);\n }\n std::vector<std::vector<string>> suggestions;\n for (auto iter = search_word.cbegin(); iter != search_word.cend();)\n {\n ++iter;\n string_view substring(search_word.cbegin(), iter);\n root.find(3, substring, suggestions);\n }\n return suggestions;\n }\n};", "memory": "583070" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "#include <algorithm>\n#include <cstddef>\n#include <iostream>\n#include <memory>\n#include <map>\n#include <optional>\n#include <string>\n#include <string_view>\n\n\nclass TrieNode\n{\nprivate:\n typedef std::map<char, std::unique_ptr<TrieNode>> children_t;\npublic:\n void insert(const std::string& word)\n {\n this->insert_(word);\n }\n\n void find(\n const std::size_t match_limit,\n const std::string_view& substring,\n std::vector<std::vector<string>>& output)\n {\n std::string matching;\n auto sub_iter = substring.cbegin();\n output.emplace_back();\n this->find_(match_limit, 0, substring, substring.cend(), sub_iter, matching, output);\n }\nprivate:\n void insert_(const std::string_view& substring)\n {\n if (substring.empty())\n {\n //std::cout << \"reached substring end\" << std::endl;\n children_.emplace('\\0', std::unique_ptr<TrieNode>());\n return;\n }\n auto sub_iter = substring.cbegin();\n std::optional<typename children_t::iterator> child = std::nullopt;\n if (this->children_.contains(*sub_iter))\n {\n //std::cout << \"reusing=\" << *sub_iter << std::endl;\n child.emplace(this->children_.find(*sub_iter));\n assert(child.value() != this->children_.end());\n }\n else\n {\n //std::cout << \"inserting=\" << *sub_iter << std::endl;\n const auto result = this->children_.emplace(*sub_iter, std::make_unique<TrieNode>());\n assert(result.second);\n child.emplace(result.first);\n }\n assert(child.has_value());\n ++sub_iter;\n std::string_view next(sub_iter, substring.end());\n child.value()->second->insert_(next);\n }\n\n std::size_t find_(\n const std::size_t match_limit,\n const std::size_t match_count,\n const std::string_view& substring,\n const std::string_view::iterator& sub_end,\n std::string_view::iterator& sub_iter,\n std::string current_match,\n std::vector<std::vector<string>>& output)\n {\n //std::cout << \"match_limit=\" << match_limit << \", match_count=\" << match_count;\n if (match_count == match_limit)\n {\n //std::cout << \", limit reached\" <<std::endl;\n return match_count;\n }\n std::size_t current_count = match_count;\n if (this->children_.contains('\\0') && substring.size() <= current_match.size())\n {\n //std::cout << \", match=\" << current_match;\n output.back().emplace_back(current_match);\n ++current_count;\n if (this->children_.size() == 1)\n {\n // reached a leaf in the trie\n //std::cout << \", reached leaf\" <<std::endl;\n return current_count;\n }\n }\n if (sub_iter != sub_end && this->children_.contains(*sub_iter))\n {\n current_match.push_back(*sub_iter);\n auto& node_ptr = this->children_[*sub_iter];\n //std::cout << \", sub_iter=\" << *sub_iter << \", current_match=\" << current_match << std::endl;\n ++sub_iter;\n current_count = node_ptr->find_(match_limit, current_count, substring, sub_end, sub_iter, current_match, output);\n //std::cout << \"backtracked to \" << *sub_iter << \", count is now \" << current_count << std::endl;\n current_match.pop_back();\n }\n else if (sub_iter == sub_end)\n {\n // we're already matching the requested substring, so any children from this node will match\n for (auto child_iter = this->children_.cbegin(); current_count < match_limit && child_iter != this->children_.cend(); ++child_iter)\n {\n if (child_iter->first == '\\0')\n {\n continue;\n }\n current_match.push_back(child_iter->first);\n //std::cout << \", current_match=\" << current_match << std::endl;\n current_count = child_iter->second->find_(match_limit, current_count, substring, sub_end, sub_iter, current_match, output);\n //std::cout << \"substring already matched, backtracked to \" << child_iter->first << \", count is now \" << current_count << std::endl;\n current_match.pop_back();\n }\n }\n return current_count;\n }\n\n children_t children_;\n};\n\n\nclass Solution\n{\npublic:\n std::vector<std::vector<std::string>> suggestedProducts(vector<std::string>& products, const std::string& search_word)\n {\n //std::cout << \"search_word=\" << search_word << \", products=[\" << std::endl;\n //for (const std::string& prod: products)\n //{\n // std::cout << prod << std::endl;\n //}\n //std::cout << \"]\" << std::endl;\n std::sort(products.begin(), products.end());\n TrieNode root;\n for (const string& prod: products)\n {\n root.insert(prod);\n }\n std::vector<std::vector<string>> suggestions;\n for (auto iter = search_word.cbegin(); iter != search_word.cend();)\n {\n ++iter;\n string_view substring(search_word.cbegin(), iter);\n root.find(3, substring, suggestions);\n }\n return suggestions;\n }\n};", "memory": "590850" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Node {\npublic:\n bool flag;\n Node* links[26] = {NULL};\n Node(){\n flag = false;\n }\n};\n\nclass Solution {\n void add_word(Node* root, string &word){\n Node* curr = root;\n for(auto it: word){\n int ch = it-'a';\n if(!curr->links[ch]) curr->links[ch] = new Node();\n curr = curr->links[ch];\n }\n curr->flag = true;\n }\n\n void dfs(Node* curr, int &it, vector<string> &res, string s){\n if(it == 3) return;\n if(curr->flag) {\n it++;\n res.push_back(s);\n }\n if(it == 3) return;\n \n for(int i = 0; i<26; i++){\n if(curr->links[i]){\n s += char(i+'a');\n dfs(curr->links[i],it,res,s);\n s.erase(s.size()-1,1);\n }\n }\n }\n\n vector<string> compile(Node* root, char ch){\n Node* curr = root->links[ch-'a'];\n vector<string> res;\n string s = \"\";\n int it = 0;\n dfs(curr,it,res,s);\n return res;\n }\n\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Node* root = new Node();\n for(auto it: products) add_word(root,it);\n\n int n = searchWord.size();\n vector<vector<string>> ans(n);\n for(int i = 0; i<n; i++){\n char ch = searchWord[i];\n if(!root->links[ch-'a']) break;\n \n vector<string> res = compile(root,ch);\n for(int j = 0; j<res.size(); j++){\n res[j] = searchWord.substr(0,i+1)+res[j];\n }\n root = root->links[ch-'a'];\n ans[i] = res;\n }\n\n return ans;\n }\n};", "memory": "590850" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "// To find lexicographically smallest, do dfs for each character from 'a' till 'z'\n\nstruct TrieNode {\n unordered_map<int, TrieNode*> links;\n bool isEnd;\n\n TrieNode() {\n isEnd = 0;\n }\n\n bool containsKey(char ch) {\n return links[ch - 'a'];\n }\n\n void putKey(TrieNode* node, char ch) {\n links[ch - 'a'] = node;\n }\n\n TrieNode* next(char ch) {\n return links[ch - 'a'];\n }\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n\n Trie() {\n root = new TrieNode();\n }\n\n void insert(string& word) {\n TrieNode* node = root;\n int level = 0;\n for(auto& ch : word) {\n if(!node->containsKey(ch)) {\n node->putKey(new TrieNode(), ch);\n }\n node = node->next(ch);\n }\n node->isEnd = 1;\n }\n\n vector<string> search(string& pre) {\n vector<string> res;\n TrieNode* node = root;\n for(auto& ch : pre) {\n if(!node->containsKey(ch)) {\n return {};\n }\n node = node->next(ch);\n }\n dfs(node, pre, res);\n return res;\n }\n\n void dfs(TrieNode* node, string pre, vector<string>& res) {\n if(node->isEnd) {\n if(res.size() < 3) {\n res.push_back(pre);\n }\n }\n if(res.size() == 3) {\n return;\n }\n\n for(char ch = 'a'; ch <= 'z'; ch++) {\n if(node->containsKey(ch)) {\n dfs(node->next(ch), pre + ch, res);\n }\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> res;\n Trie trie;\n for(auto& product : products) {\n trie.insert(product);\n }\n string pre;\n for(auto& ch : searchWord) {\n pre += ch;\n vector<string> cur = trie.search(pre);\n res.push_back(cur);\n }\n return res;\n }\n};", "memory": "598630" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class SolutionUsingBinarySearch {\npublic:\n vector<vector<string>> suggestedProducts(\n vector<string>& products,\n string searchWord) {\n vector<vector<string>> ret;\n \n sort(products.begin(), products.end());\n \n string q = \"\";\n for (size_t i = 0; i < searchWord.size(); i++) {\n q += searchWord[i];\n \n auto it = lower_bound(products.begin(), products.end(), q);\n if (it == products.end()) {\n vector<string> results;\n ret.push_back(results);\n continue;\n }\n \n vector<string> results;\n size_t pos = it - products.begin();\n size_t endPos = min(pos + 3, products.size());\n for (size_t j = pos; j < endPos; j++) {\n string product = products[j];\n if (product.find(q) != 0) {\n break;\n }\n \n results.push_back(product);\n }\n \n ret.push_back(results);\n }\n \n return ret;\n }\n};\n\nstruct Node {\n map<char, Node> children;\n bool isWord;\n};\n\nclass SolutionUsingTrie {\npublic:\n vector<vector<string>> suggestedProducts(\n vector<string>& products,\n string searchWord) {\n vector<vector<string>> ret;\n \n Node trie = buildTrie(products);\n \n string word = \"\";\n for (size_t i = 0; i < searchWord.size(); i++) {\n word += searchWord[i];\n \n vector<char> collectedChars;\n vector<string> collectedProducts;\n searchInTrie(trie, word, collectedChars, collectedProducts);\n \n ret.push_back(collectedProducts);\n }\n \n return ret;\n }\n \nprivate:\n Node buildTrie(const vector<string>& products) {\n Node ret;\n ret.isWord = false;\n \n for (string product : products) {\n Node* currentNode = &ret;\n \n for (char c : product) {\n if (currentNode->children.find(c) == currentNode->children.end()) {\n Node child;\n child.isWord = false;\n currentNode->children[c] = child;\n }\n \n currentNode = &(currentNode->children[c]);\n }\n \n currentNode->isWord = true;\n }\n \n return ret;\n }\n \n void searchInTrie(\n const Node& node,\n const string& word,\n const vector<char>& collectedChars,\n vector<string>& collectedProducts) {\n if (node.isWord && word.size() <= collectedChars.size()) {\n string product = \"\";\n for (char c : collectedChars) {\n product += c;\n }\n collectedProducts.push_back(product);\n }\n \n for (auto it = node.children.begin();\n it != node.children.end() && collectedProducts.size() < 3;\n it++) {\n const char cha = it->first;\n const Node& child = it->second;\n \n if (collectedChars.size() >= word.size() ||\n word[collectedChars.size()] == cha) {\n vector<char> copy(collectedChars);\n copy.push_back(cha);\n searchInTrie(child, word, copy, collectedProducts);\n }\n }\n }\n};\n\n//class Solution : public SolutionUsingBinarySearch {};\nclass Solution : public SolutionUsingTrie {};\n", "memory": "598630" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool startsWith(string prefix, string s) {\n if(prefix.length()>s.length()){\n return false;\n }\n\n return (s.substr(0, prefix.length())==prefix);\n }\n\n vector<string> getThreeOrLessStringsHavingPrefix(string prefix, vector<string>& products) {\n vector<string> subResult;\n for(int i=0;i<products.size();i++) {\n if(subResult.size()<3){\n if(startsWith(prefix, products[i])) {\n subResult.push_back(products[i]);\n }\n }\n }\n\n return subResult;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n string prefix=\"\";\n sort(products.begin(), products.end());\n vector<vector<string>> result;\n for(int i=0;i<searchWord.length();i++) {\n prefix+=searchWord[i];\n vector<string> subResult = getThreeOrLessStringsHavingPrefix(prefix, products);\n result.push_back(subResult);\n }\n\n return result;\n }\n};", "memory": "606410" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>>ans;\n for(int i=0;i<searchWord.length();i++){\n vector<string>temp;\n int j=0;\n int k=0;\n while(j<products.size()&&k<3){\n if(products[j].substr(0,i+1)==searchWord.substr(0,i+1)){\n temp.push_back(products[j]);\n k++;\n }\n j++;\n }\n ans.push_back(temp);\n }\n return ans;\n\n }\n};", "memory": "606410" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\n\npublic:\n // Solution* children[26];\n // bool isEnd;\n // Solution() {\n // for (int i = 0; i < 26; i++)\n // children[i] = NULL;\n // isEnd = false;\n // }\n // void insert(string word) {\n // Solution* curr = this;\n // for (char c : word) {\n // if (!(curr->children[c - 'a'])) {\n // curr->children[c - 'a'] = new Solution();\n // }\n // curr = curr->children[c - 'a'];\n // }\n // curr->isEnd = true;\n // }\n bool startsWith(string prefix, string str) {\n\n int n = prefix.length();\n for (int i = 0; i < n; i++) {\n if (prefix[i] == str[i])\n continue;\n return false;\n }\n\n return true;\n }\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n int word = searchWord.length();\n int n = products.size();\n // Solution* obj = new Solution();\n // for (int i = 0; i < n; i++) {\n // obj->insert(products[i]);\n // }\n int i = 0;\n vector<vector<string>> ans;\n string temp;\n while (i < word) {\n temp += searchWord[i];\n vector<string> s;\n for (int j = 0; j < n; j++) {\n if (startsWith(temp, products[j])) {\n s.push_back(products[j]);\n }\n }\n sort(s.begin(), s.end());\n if (s.size() > 3) {\n s.resize(3);\n }\n ans.push_back(s);\n i++;\n }\n return ans;\n }\n};", "memory": "614190" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n //foreach product try matching startswith(searchWorld)\n //put into a minHeap of strings\n //pop top 3\n int n = searchWord.size();\n vector<vector<string>> res(n);\n for(int i=1; i<=searchWord.size(); i++) {\n priority_queue<string, vector<string>, greater<string>> pq;\n for(auto p:products) {\n if(p.starts_with(searchWord.substr(0,i))) {\n pq.push(p);\n }\n }\n int count = 3;\n while(pq.size() && count--) {\n res[i-1].push_back(pq.top());\n pq.pop();\n }\n }\n\n return res;\n }\n};", "memory": "621970" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(string text,string prefix){\n return mismatch(prefix.begin(), prefix.end(), text.begin(), text.end()).first == prefix.end();\n }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n //We can try to do brute force in this problem. we can consider every possible prefix in the search words and then\n //find all the possible words which are having that word a prefix.\n int n = products.size();\n int i =0,m=searchWord.size();\n vector<vector<string>> ans;\n while(i<m){\n string prefix = searchWord.substr(0,i+1);\n vector<string> temp;\n //with this pre\n for(int j=0;j<n;j++){\n if(solve(products[j],prefix)){\n temp.push_back(products[j]);\n }\n }\n if(temp.size()>3){\n //only take the top three\n sort(temp.begin(),temp.end());\n while(temp.size()>3){\n temp.pop_back();\n }\n ans.push_back(temp);\n }\n else{\n sort(temp.begin(),temp.end());\n ans.push_back(temp);\n }\n i++;\n }\n return ans;\n }\n};", "memory": "621970" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n struct Node {\n unordered_map<char, unique_ptr<Node>> children;\n bool isEnd;\n Node() : isEnd(false) {}\n };\n\n unique_ptr<Node> root;\n\n void insert(const string& word) {\n Node* node = root.get();\n for (char ch : word) {\n if (!node->children.count(ch)) {\n node->children[ch] = make_unique<Node>();\n }\n node = node->children[ch].get();\n }\n node->isEnd = true;\n }\n\n void dfs(Node* node, const string& word, priority_queue<string>& pq) {\n if (node->isEnd) {\n pq.push(word);\n if (pq.size() > 3) pq.pop();\n }\n\n for (auto& [key, value] : node->children) {\n dfs(value.get(), word + key, pq);\n }\n }\n\n vector<string> startWith(const string& word) {\n Node* node = root.get();\n priority_queue<string> pq;\n for (char ch : word) {\n if (!node->children.count(ch)) return {};\n node = node->children[ch].get();\n }\n dfs(node, word, pq);\n\n vector<string> generatedWords;\n while (!pq.empty()) {\n generatedWords.insert(generatedWords.begin(), pq.top());\n pq.pop();\n }\n\n return generatedWords;\n }\n\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n root = make_unique<Node>();\n int length = searchWord.size();\n vector<vector<string>> words;\n\n for (const string& product : products) {\n insert(product);\n }\n\n string word = \"\";\n for (int i = 0; i < length; i++) {\n word += searchWord[i];\n words.push_back(startWith(word));\n }\n\n return words;\n }\n};\n", "memory": "629750" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "struct Node {\n Node* links[26];\n bool flag = false;\n Node() {\n for (int i = 0; i < 26; i++) {\n links[i] = nullptr;\n }\n }\n};\n\nclass Trie {\npublic:\n Node* root;\n Trie() {\n root = new Node();\n }\n void insert(string s) {\n Node* node = root;\n for (auto it : s) {\n if (node->links[it - 'a'] != nullptr) {\n node = node->links[it - 'a'];\n } else {\n node->links[it - 'a'] = new Node();\n node = node->links[it - 'a'];\n }\n }\n node->flag = true;\n }\n vector<string> f(string word) {\n Node* node = root;\n priority_queue<string, vector<string>, greater<string>> pq;\n for (auto it : word) {\n if (node->links[it - 'a'] == nullptr) {\n return {}; // Prefix not found\n }\n node = node->links[it - 'a'];\n }\n f(node, pq, word);\n vector<string> res;\n while (!pq.empty()) {\n res.push_back(pq.top());\n pq.pop();\n }\n return res;\n }\n\n void f(Node* root, priority_queue<string, vector<string>, greater<string>>& pq, string word) {\n if (root->flag == true) {\n if(pq.size()>3){\n if(pq.top()>word){\n pq.push(word);\n }\n }else{\n pq.push(word);\n }\n \n }\n for (int i = 0; i < 26; i++) {\n if (root->links[i] != nullptr) {\n char res = i + 'a';\n f(root->links[i], pq, word + res);\n }\n }\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie tr;\n for (auto it : products) {\n tr.insert(it);\n }\n vector<vector<string>> result;\n string prefix = \"\";\n for (char c : searchWord) {\n prefix += c;\n vector<string> suggestions = tr.f(prefix);\n if (suggestions.size() > 3) {\n suggestions.resize(3);\n }\n result.push_back(suggestions);\n }\n return result;\n }\n};\n", "memory": "629750" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TreeNode {\n bool isWord;\n char letter;\n vector<TreeNode*> children;\n };\n\n TreeNode* getChildAt(char letter, TreeNode* node) {\n for (auto child : node->children) {\n if (child->letter == letter) {\n return child;\n }\n }\n return nullptr;\n }\n\n void addKey(TreeNode* node, string key) {\n TreeNode* current = node;\n for (int i = 0; i < key.size(); i++) {\n char letter = key[i];\n TreeNode* temp = getChildAt(letter, current);\n if (temp == nullptr) {\n temp = new TreeNode();\n temp->letter = letter;\n current->children.push_back(temp);\n sort(current->children.begin(), current->children.end(),\n [](TreeNode* node1, TreeNode* node2) \n { return node2->letter >= node1->letter; });\n }\n if (i == key.size() - 1) {\n temp->isWord = true;\n }\n current = temp;\n }\n }\n\n void getKeys(TreeNode* node, string prefix, vector<string>& result) {\n if (result.size() == 3) {\n return;\n }\n prefix += node->letter;\n if (node->isWord) {\n result.push_back(prefix);\n }\n \n if (node->children.size() > 0) {\n for (TreeNode* child : node->children) {\n getKeys(child, prefix, result);\n }\n }\n }\n\n vector<string> findKeys(TreeNode* node, string prefix) {\n vector<string> result;\n TreeNode* current = node;\n for (int i = 0; i < prefix.size(); i++) {\n char letter = prefix[i];\n TreeNode* temp = getChildAt(letter, current);\n if (!temp) {\n return result;\n }\n current = temp;\n }\n\n if (current->isWord) {\n result.push_back(prefix);\n }\n\n for (TreeNode* child : current->children) {\n getKeys(child, prefix, result);\n }\n\n vector<string> test;\n test.push_back(to_string(result.size()));\n return result;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n TreeNode* root = new TreeNode();\n for (int i = 0; i < products.size(); i++) {\n addKey(root, products[i]);\n }\n\n vector<vector<string>> answer;\n for (int i = 1; i <= searchWord.size(); i++) {\n answer.push_back(findKeys(root, searchWord.substr(0, i)));\n }\n //answer.push_back(findKeys(root, searchWord));\n return answer;\n }\n};", "memory": "637530" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "\nclass Solution {\n public:\n class Tire {\n struct TireNode {\n string value;\n unique_ptr<TireNode> child[26] = {};\n };\n\n TireNode root;\n\n void InsertInternal(TireNode* root, const string& word, int i) {\n if (i == word.size()) {\n root->value = word;\n return;\n }\n\n int idx = word[i] - 'a';\n if (root->child[idx] == nullptr) {\n root->child[idx] = std::make_unique<TireNode>();\n }\n\n InsertInternal(root->child[idx].get(), word, i + 1);\n }\n\n public:\n void Insert(const string& word) {\n InsertInternal(&root, word, 0);\n }\n\n void SearchWord(const string& t, vector<vector<string>>& ans) {\n auto* curr = &root;\n for (auto& c : t) {\n curr = curr->child[c - 'a'].get();\n if (curr == nullptr) {\n ans.push_back({});\n return;\n }\n }\n\n ans.push_back(SearchWord(curr));\n // if (root.child[t[i] - 'a'] != nullptr) {\n // SearchWord(root.child[t[i] - 'a'].get(), t, 0, t[0], ans);\n // }\n }\n\n vector<string> SearchWord(TireNode* root) {\n vector<string> res;\n\n if (!root->value.empty()) {\n res.push_back(root->value);\n }\n\n for (int i = 0; i < 26; ++i) {\n if (root->child[i] != nullptr) {\n for (auto x : SearchWord(root->child[i].get())) {\n if (res.size() == 3) {\n break;\n }\n res.push_back(std::move(x));\n }\n }\n\n if (res.size() == 3) {\n break;\n }\n }\n\n return res;\n }\n };\n\n vector<vector<string>> BinarySearch(vector<string>& products, string& searchWord) {\n ranges::sort(products);\n\n string prefix;\n int n = products.size();\n vector<vector<string>> ans(searchWord.size());\n\n for (int i = 0; i < searchWord.size(); ++i) {\n prefix.push_back(searchWord[i]);\n\n int start = ranges::lower_bound(products, prefix) - products.begin();\n\n for (int j = start; j < min(start + 3, n) && products[j].starts_with(prefix); ++j) {\n ans[i].push_back(products[j]);\n }\n }\n\n return ans;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n // return BinarySearch(products, searchWord);\n\n Tire t;\n for (auto& product : products) {\n t.Insert(product);\n }\n vector<vector<string>> ans;\n\n string prefix;\n\n for (auto c : searchWord) {\n prefix.push_back(c);\n t.SearchWord(prefix, ans);\n }\n\n return ans;\n }\n};\n", "memory": "637530" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n vector<vector<string>> ans (searchWord.size());\n\n for(int i=0;i<searchWord.size();i++){\n for(string p:products){\n if (p.substr(0,i+1)==searchWord.substr(0,i+1)){\n if (ans[i].size()<3) ans[i].push_back(p);\n else break;\n }\n }\n }\n return ans;\n }\n};", "memory": "645310" }
1,397
<p>You are given an array of strings <code>products</code> and a string <code>searchWord</code>.</p> <p>Design a system that suggests at most three product names from <code>products</code> after each character of <code>searchWord</code> is typed. Suggested products should have common prefix with <code>searchWord</code>. If there are more than three products with a common prefix return the three lexicographically minimums products.</p> <p>Return <em>a list of lists of the suggested products after each character of </em><code>searchWord</code><em> is typed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> products = [&quot;mobile&quot;,&quot;mouse&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mousepad&quot;], searchWord = &quot;mouse&quot; <strong>Output:</strong> [[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;],[&quot;mouse&quot;,&quot;mousepad&quot;]] <strong>Explanation:</strong> products sorted lexicographically = [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;,&quot;mouse&quot;,&quot;mousepad&quot;]. After typing m and mo all products match and we show user [&quot;mobile&quot;,&quot;moneypot&quot;,&quot;monitor&quot;]. After typing mou, mous and mouse the system suggests [&quot;mouse&quot;,&quot;mousepad&quot;]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> products = [&quot;havana&quot;], searchWord = &quot;havana&quot; <strong>Output:</strong> [[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;],[&quot;havana&quot;]] <strong>Explanation:</strong> The only word &quot;havana&quot; will be always suggested while typing the search word. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= products.length &lt;= 1000</code></li> <li><code>1 &lt;= products[i].length &lt;= 3000</code></li> <li><code>1 &lt;= sum(products[i].length) &lt;= 2 * 10<sup>4</sup></code></li> <li>All the strings of <code>products</code> are <strong>unique</strong>.</li> <li><code>products[i]</code> consists of lowercase English letters.</li> <li><code>1 &lt;= searchWord.length &lt;= 1000</code></li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& p, string w) {\n sort(p.begin(),p.end());\n int n=w.size();\n vector<vector<string>> ans(n);\n for(int i=1;i<=n;i++){\n for(auto x:p) if(x.substr(0,i)==w.substr(0,i)){\n if(ans[i-1].size()>2) break;\n ans[i-1].push_back(x);\n }\n }\n return ans;\n }\n};", "memory": "645310" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n #define mod 1000000007\n #define ll long long\n vector< vector<int> >dp;\n int helper(int n,int idx,int steps){\n if(steps==0){\n if(idx==0) return 1;\n else return 0;\n }\n if(dp[idx][steps]!=-1) return dp[idx][steps];\n ll ops1=0;\n ll ops2=0;\n ll ops3=0;\n if(steps>0){\n if(idx+1<n){\n ops1=(ll)helper(n,idx+1,steps-1)%mod;\n }\n if(idx-1>=0){\n ops2=(ll)helper(n,idx-1,steps-1)%mod;\n }\n ops3=(ll)helper(n,idx,steps-1)%mod;\n }\n return dp[idx][steps]=(ops1%mod+ops2%mod+ops3%mod)%mod;\n }\n int numWays(int steps, int arrLen) {\n int maxLen = min(arrLen, steps / 2 + 1);\n dp.clear();\n dp.resize(maxLen+1,vector<int>(steps+1,-1));\n return helper(maxLen,0,steps);\n }\n};", "memory": "11616" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int mod = 1e9+7;\n int solve(int i, int steps, int arrLen, vector<vector<int>>& memo) {\n if (i < 0 || i >= arrLen) return 0;\n if (steps < 0) return 0;\n if (steps == 0) return i == 0 ? 1 : 0;\n\n if (memo[i][steps] != -1) return memo[i][steps];\n\n long long way1 = solve(i - 1, steps - 1, arrLen, memo);\n long long way2 = solve(i + 1, steps - 1, arrLen, memo);\n long long way3 = solve(i, steps - 1, arrLen, memo);\n\n memo[i][steps] = (way1%mod + way2%mod + way3%mod) % mod ;\n return memo[i][steps];\n }\n\n int numWays(int steps, int arrLen) {\n // Limit the arrLen to a maximum of steps/2 + 1\n arrLen = min(arrLen, steps / 2 + 1);\n vector<vector<int>> memo(arrLen, vector<int>(steps + 1, -1));\n return solve(0, steps, arrLen, memo);\n }\n};\n", "memory": "11616" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int numWays(int s, int n) {\n int mxm = min(n, s/2+1), mod= 1e9 + 7;\n vector dp(mxm, vector<int>(s+1, -1));\n \n function<int(int, int)> f = [&] (int index, int steps) -> int {\n if(steps == 0 && index == 0)return 1;\n if(steps == 0 || index < 0 || index >= mxm)return 0;\n if(dp[index][steps] != -1)return dp[index][steps];\n return dp[index][steps] = ((f(index+1, steps-1) + f(index-1, steps - 1))%mod + f(index, steps - 1))%mod;\n };\n \n return f(0, s);\n }\n};", "memory": "12793" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n int N = 1000000007;\n if (steps == 1 || arrLen == 1)\n return 1;\n vector<int> dp(min(steps + 1, arrLen));\n dp[0] = 1;\n for (int i = 0; i < steps; ++i) {\n vector<int> dp2(dp);\n for (int j = 0; j < dp.size(); ++j) {\n if (j > 0)\n dp2[j] = (dp2[j] + dp[j - 1]) % N;\n if (j < dp.size() - 1)\n dp2[j] = (dp2[j] + dp[j + 1]) % N;\n }\n dp = dp2;\n }\n return dp[0];\n }\n};", "memory": "12793" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n int lens = min(steps+1, arrLen);\n vector<int> dp(lens);\n dp[0] = 1;\n int mod = 1000000007;\n for(int i = 0; i<steps; i++)\n {\n vector<int> nxt(lens);\n for(int j=0; j<min(i+2, arrLen); j++)\n {\n if(j>0) nxt[j]=(nxt[j]+dp[j-1])%mod;\n nxt[j]=(nxt[j]+dp[j])%mod;\n if(j<min(i+2, arrLen)-1) nxt[j]=(nxt[j]+dp[j+1])%mod;\n }\n swap(nxt, dp);\n }\n return dp[0];\n }\n};", "memory": "13969" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n const int mod = 1e9 + 7;\n arrLen = min(arrLen, steps);\n\n // dp[i] - no. of ways to reach at i after steps\n\n // dp[i][j] -> dp[i + 1][j - 1]\n // dp[i][j] -> dp[i][j]\n // dp[i][j] -> dp[i + 1][j + 1]\n\n vector<int> dp(arrLen);\n dp[0] = 1;\n\n for (int i = 0; i < steps; i++) {\n vector<int> ndp(arrLen, 0);\n\n for (int j = 0; j < arrLen; j++) {\n if (j - 1 >= 0)\n ndp[j - 1] = (ndp[j - 1] + dp[j]) % mod;\n\n ndp[j] = (ndp[j] + dp[j]) % mod;\n\n if (j + 1 < arrLen)\n ndp[j + 1] = (ndp[j + 1] + dp[j]) % mod;\n }\n \n dp = ndp;\n }\n\n return dp[0];\n }\n};", "memory": "13969" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n const int mod=1e9+7;\n vector<vector<long long>>dp;\n int solve(int &n,int tot,int i ){\n if(i>=n){\n return 0;\n }\n \n long long op2=0,op3=0;\n if(tot==0)return i==0;\n if(dp[i][tot]!=-1)return dp[i][tot];\n long long op1=solve(n,tot-1,i);\n if(i!=n)\n op2=solve(n,tot-1,i+1);\n if(i!=0)\n op3=solve(n,tot-1,i-1);\n\n return dp[i][tot]=(op1+op2+op3)%mod;\n }\n int numWays(int steps, int arrLen) {\n int n=min(steps/2+1, arrLen);\n dp.resize(n+1,vector<long long>(steps+1,-1));\n return solve(n,steps,0);\n }\n};", "memory": "15145" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long mod=1e9+7;\n long long m;\n int solve(int ind,int steps,vector<vector<int>>&dp){\n\n if(ind==0 && steps==0){\n return 1;\n }\n if(ind<0 || ind>=m || steps==0 || ind>steps){\n return 0;\n }\n if(dp[ind][steps]!=-1){\n return dp[ind][steps];\n }\n long long l=solve(ind-1,steps-1,dp);\n long long r=solve(ind+1,steps-1,dp);\n long long s=solve(ind,steps-1,dp);\n\n dp[ind][steps]= (l%mod + r%mod + s%mod)%mod;\n return dp[ind][steps];\n }\n int numWays(int steps, int arrLen) {\n vector<vector<int>> dp(steps+1,vector<int> (steps+1,-1));\n m=arrLen;\n return solve(0,steps,dp);\n }\n};", "memory": "15145" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long int mod = 1e9 + 7 ;\n long long int dp[1000][1000] ;\n long long int fxn(long long int i , long long int cnt , long long int steps , long long int arrLen){\n if(cnt == steps){\n if(i == 0) return 1 ;\n return 0 ;\n }\n if(dp[i][cnt] != -1) return dp[i][cnt] ;\n long long int left = 0 , right = 0 , stay = 0 ;\n if(i - 1 >= 0){\n left = fxn(i-1 , cnt+1 , steps , arrLen) % mod ;\n }\n if(i + 1 < arrLen){\n right = fxn(i + 1 , cnt + 1 , steps , arrLen) % mod;\n }\n stay = fxn(i , cnt + 1 , steps , arrLen) % mod ;\n return dp[i][cnt] = (left + right + stay) % mod ;\n }\n int numWays(int steps, int arrLen) {\n memset(dp , -1 , sizeof(dp));\n long long int cnt = 0 ;\n return fxn(0 , cnt , steps , arrLen) % mod ;\n }\n};", "memory": "16321" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int recur(int n,int i,int s,vector<vector<int>>&dp){\n if(s==0){\n if(i==0)return 1;\n return 0;\n }\n if(dp[i][s]!=-1)return dp[i][s];\n int a=recur(n,i,s-1,dp);\n a%=mod;\n if(i<n-1){\n a+=recur(n,i+1,s-1,dp);\n a%=mod;\n }\n if(i>0){\n a+=recur(n,i-1,s-1,dp);\n a%=mod;\n }\n return dp[i][s]=a;\n\n }\n int numWays(int steps, int arrLen) {\n vector<vector<int>> dp(steps+100,vector<int>(steps+1,-1));\n return recur(arrLen,0,steps,dp);\n }\n};", "memory": "16321" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n int MOD = 1e9+7;\n vector<vector<int>>dp;\n int F(int steps, int i){\n if(steps==0 && i==0) return 1;\n if(steps==0 || i==n || i==-1) return 0;\n if(dp[steps][i]!=-1) return dp[steps][i];\n int stay=0, left=0, right=0;\n stay = F(steps-1, i);\n left = F(steps-1, i-1);\n right = F(steps-1, i+1);\n return dp[steps][i]=((stay+left)%MOD + right)%MOD;\n }\n\n int numWays(int steps, int arrLen) {\n n=arrLen;\n dp.resize(steps+1, vector<int>(min(1+(steps)*2, arrLen+1), -1));\n return F(steps, 0);\n }\n};", "memory": "17498" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n const static int MOD = 1e9 + 7;\npublic:\n int numWays(int steps, \n int arrLen // arrLen\n ) {\n\n int n = min(steps, arrLen);\n vector<long> dp(n + 2);\n dp[1] = 1;\n for(int i = 0;i<steps;i++) {\n vector<long> new_dp(n + 2);\n for(int j = 1;j<=n;j++) {\n new_dp[j] = (dp[j - 1] + dp[j] + dp[j + 1]) % MOD;\n }\n dp = new_dp;\n }\n return dp[1];\n }\n};", "memory": "18674" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n // long long int solve(int steps, int arrLen,int i,vector<vector<long long int>> &dp)\n // {\n // if(i>=arrLen||i<0)\n // return 0;\n // if(steps==0)\n // {\n // if(i==0)\n // return 1;\n // return 0;\n // }\n // long long int mo=1e9+7;\n // if(dp[steps][i]!=-1)\n // return dp[steps][i];\n // long long int ans=0;\n // ans+=solve(steps-1,arrLen,i,dp);\n // ans+=solve(steps-1,arrLen,i-1,dp);\n // ans+=solve(steps-1,arrLen,i+1,dp);\n // return dp[steps][i]=ans%mo;\n // }\n int numWays(int steps, int arrLen) {\n if(arrLen>steps+1)\n arrLen=steps+1;\n vector<vector<long long int>> dp(steps+1,vector<long long int>(arrLen+1,0));\n dp[0][0]=1;\n long long int mo=1e9+7;\n for(int i=1;i<=steps;i++)\n {\n for(int j=0;j<arrLen;j++)\n {\n if(j!=0)\n dp[i][j]+=dp[i-1][j-1]%mo;\n if(j!=arrLen-1)\n dp[i][j]+=dp[i-1][j+1]%mo;\n dp[i][j]+=dp[i-1][j]%mo;\n \n }\n }\n return int(dp[steps][0])%mo;\n }\n};", "memory": "19850" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "\n// breakDown Hard to easy [recursion]\n// Trust on yourself - you are doing better 99% students \n\n// point-1 You have a pointer at index 0 in an array of size arrLen.\n// point-2 each steps, \n// you can move left by 1 position\n// you can move right by 1 position\n// you can stay into same position \n// you have number of steps that you can take\n\n// helper(index,steps) => helper(0,0) (how many times hits- the number of ways such that your pointer is still at index 0 after exactly step)\n\n// Striver DP Series [rules]\n// Try all the possible ways\n//1. express everything in terms of indexes\n//2. explore all paths\n//3. sum up all possible ways [paths]\n \n\nclass Solution {\nprivate:\n// Since the answer may be too large, return it modulo 109 + 7.\n int mod = 1e9+7;\n int helper(int index,int steps, int n, vector<vector<long long int>> & dp){\n // base case 1\n // left side ja rahe ho (negative mat jana)\n // right side ja rahe ho (arrayLen se mat jana)\n if(index < 0 || index >= n ){\n return 0;\n }\n // base case-2 \n // there are no number of steps\n if(steps < 0) return 0;\n\n // base case-2\n // your pointer is still at index 0 \n if(index == 0 && steps == 0){\n return 1;\n }\n\n // memorization of recursive calls\n // time complexity 0(1)\n if(dp[steps][index] != -1){\n return dp[steps][index];\n }\n\n // large number of value (store used long long int)\n // every recusive need to do modificatin by large number MOD\n long long int left = helper(index-1,steps-1,n,dp) % mod;\n long long int right = helper(index+1,steps-1,n,dp) % mod;\n long long int staySame = helper(index,steps-1,n,dp) % mod;\n long long int TotalNumberWays = (left + right + staySame) % mod;\n return dp[steps][index] = TotalNumberWays;\n }\npublic:\n int numWays(int steps, int arrLen) {\n if(arrLen > steps) \n arrLen = steps;\n int n = arrLen;\n int index = 0;\n // TLE\n // DP matrix used -> long long int (to store the large value)\n vector<vector<long long int>> dp(steps+1,vector<long long int>(arrLen+1,-1));\n return helper(index,steps,n,dp);\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n\n", "memory": "19850" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n static const int mod=1000000007;\n // int helper(int d,int s,int l,vector<vector<int>>&dp)\n // {\n // if(s==0)\n // {\n // if(d==0)\n // return 1;\n // return 0;\n // }\n // if(d>=l)\n // return 0;\n // if(dp[d][s]!=-1)\n // return dp[d][s];\n // int ans=0;\n // //stay\n // if(s-1>=d)\n // ans=(ans+helper(d,s-1,l,dp))%mod;\n // //left\n // if(d>0&&s-1>=d-1)\n // ans=(ans+helper(d-1,s-1,l,dp))%mod;\n // //right\n // if(s-1>=d+1&&d<l)\n // ans=(ans+helper(d+1,s-1,l,dp))%mod;\n // return dp[d][s]=ans%mod;\n // }\npublic:\n int numWays(int s, int l) {\n vector<vector<long long>>dp(min(l,s)+2,vector<long long>(s+1,0));\n dp[0][0]=1;\n \n for(int i=1;i<=s;i++)\n {for(int d=0;d<min(l,s);d++)\n {\n int ans=0;\n //stay\n if(i-1>=d)\n ans=(ans+dp[d][i-1])%mod;\n //left\n if(d>0&&i-1>=d-1)\n ans=(ans+dp[d-1][i-1])%mod;\n //right\n if(i-1>=d+1&&d<l)\n ans=(ans+dp[d+1][i-1])%mod;\n dp[d][i]=ans%mod;\n }\n }\n return dp[0][s];\n }\n};", "memory": "21026" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n#define MOD 1000000007\n int numWays(int steps, int arrLen) {\n int n = min(arrLen, steps)+5;\n vector<vector<long long>> dp(steps+1, vector<long long>(n));\n dp[0][0] = 1;\n\n for(int i = 1; i <= steps; i++){\n for(int j = 0; j < min(i, arrLen); j++){\n for(int k = -1; k <= 1; k++){\n if(j + k >= 0 && j + k < n){\n dp[i][j+k] += dp[i-1][j];\n dp[i][j+k] %= MOD;\n }\n }\n }\n }\n return dp[steps][0];\n\n }\n};", "memory": "21026" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n int mod=(int)(1e9+7);\n long long solve(long long i ,long long steps,long long arrlen,vector<vector<long long>>&dp){\n if(i<0 || i>=arrlen) return 0;\n\n if(steps==0){\n if(i==0) return 1;\n return 0;\n }\n\n if(dp[i][steps]!=-1) return dp[i][steps];\n\n //3 options\n long long stay=solve(i,steps-1,arrlen,dp);\n long long left=solve(i-1,steps-1,arrlen,dp);\n long long right=solve(i+1,steps-1,arrlen,dp);\n\n return dp[i][steps]=((stay)%mod+(left)%mod+(right)%mod)%mod;\n }\npublic:\n int numWays(int steps, int arrLen) {\n // vector<vector<long long>>dp(arrLen+1,vector<long long>(steps+1,-1));\n // return solve(0,steps,arrLen,dp);\n //above gives a memory limit exceed\n \n\n //note that i jo hai woh kitna aage badh sakta hai max jinta steps ho isliye \n //dp array mein instead of arrlen+1 \n //we can use steps+1\n\n vector<vector<long long>>dp(steps+1,vector<long long>(steps+1,-1));\n return solve(0,steps,arrLen,dp);\n }\n};", "memory": "22203" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n vector<long long> curr(arrLen, 0);\n curr[0] = 1;\n for (int i = 0; i < steps; i++) {\n long long prev = 0;\n for (int j = 0; j < min(arrLen, steps-i+1); j++) {\n long long c = curr[j] + prev;\n if (j+1 < arrLen) {\n c += curr[j+1];\n }\n prev = curr[j];\n curr[j] = c % 1000000007;\n }\n }\n return curr[0];\n }\n};", "memory": "22203" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n std::vector<unsigned int> cur(arrLen);\n std::vector<unsigned int> prev(arrLen);\n\n int modulo = 1e9 + 7;\n prev[0] = 1;\n for (int i = 1; i < steps + 1; ++i) {\n for (int j = 0; j < std::min({i + 1, steps - i + 1, arrLen}); ++j) {\n cur[j] = ((prev[j] % modulo\n + (j > 0 ? prev[j - 1] : 0) % modulo\n + (j < arrLen - 1 ? prev[j + 1] : 0) % modulo)) % modulo;\n }\n std::swap(prev, cur);\n }\n return prev[0];\n }\n};\n\n// ", "memory": "23379" }