id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n \n int k = magazine.length()-1;\n int c = 0;\n for(int i=0;i<ransomNote.length();i++)\n {\n for(int j=0;j<=k;j++)\n {\n char temp;\n if(ransomNote[i] == magazine[j])\n {\n c++; \n temp = magazine[k];\n magazine[k] = magazine[j];\n magazine[j] = temp;\n k--;\n break;\n }\n \n }\n cout << k << endl;\n }\n\n cout << c << \" \" << ransomNote.length();\n if(c == ransomNote.length())\n {\n return true;\n }\n return false;\n }\n};", "memory": "10200" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n vector<int>a(26,0);\n for(auto i:magazine){\n a[i-'a']++;\n }\n for(auto i:ransomNote){\n if(a[i-'a']==0)return false;\n else a[i-'a']--;\n }\n return true;\n }\n};", "memory": "10200" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n int cap[26] = {0};\n\n for (char c : magazine) {\n cap[c - 'a']++;\n }\n\n for (char c : ransomNote) {\n cap[c - 'a']--;\n if (cap[c - 'a'] < 0) return false;\n }\n return true;\n }\n};", "memory": "10300" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n vector<int> letters(26, 0);\n for (int i = 0; i < magazine.size(); i++)\n {\n letters[magazine[i] - 'a']++;\n }\n for (int i = 0; i < ransomNote.size(); i++)\n {\n if (letters[ransomNote[i] - 'a'] < 1)\n {\n return false;\n }\n else\n {\n letters[ransomNote[i] - 'a']--;\n }\n }\n return true;\n }\n};", "memory": "10300" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n long long a[26]={0};\n int c=0;\n for(int i=0;magazine[i]!='\\0';i++){\n a[magazine[i]-'a']++;\n }\n for(int i=0;ransomNote[i]!='\\0';i++){\n a[ransomNote[i]-'a']--;\n }\n for(int i=0;i<26;i++){\n if(a[i]<0){\n c=1;\n break;\n }\n }\n if(c==1)\n return false;\n else\n return true;\n }\n};", "memory": "10400" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n int x[26]={0};\n for(char i: ransomNote){\n x[i-'a']--;\n } \n for(char i: magazine){\n x[i-'a']++;\n }\n for(int i:x){\n if(i<0) return false;\n }\n return true;\n }\n};", "memory": "10400" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n int count[256] = {0};\n for (char c : magazine) count[c]++;\n for (char c : ransomNote) {\n if (count[c] == 0) return false;\n count[c]--;\n }\n return true;\n }\n};\n", "memory": "10500" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n vector<int> map(128, 0);\n\n for(int i = 0; i < magazine.length(); i++)\n {\n map[magazine[i]] += 1;\n }\n\n for(int i = 0; i <ransomNote.length(); i++)\n {\n char c = ransomNote[i];\n if(map[c] > 0)\n {\n map[c] -= 1;\n }\n else\n {\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "10500" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string r, string m) {\n int hash[256]={0};\n for(auto i :m){\n hash[i]++;\n }\n for(auto i :r){\n hash[i]--;\n }\n for(int i=0;i<256;i++){\n if(hash[i]<0) return false;\n }\n return true;\n\n \n }\n};", "memory": "10600" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n // Keys (Unique Characters): 'a', 'b', 'c'\n // Values (Count): 0, 0, 0\n unordered_map<char, int> magazine_letters;\n\n for (int i = 0; i < magazine.size(); ++i) {\n const char character = magazine[i];\n\n if (!magazine_letters.contains(character)) {\n magazine_letters.insert({ character, 1 });\n } else {\n int& count = magazine_letters.at(character);\n count++;\n }\n }\n\n for (int i = 0; i < ransomNote.size(); ++i) {\n const char character = ransomNote[i];\n\n if (!magazine_letters.contains(character))\n return false;\n\n int& count = magazine_letters.at(character);\n if (count == 0)\n return false;\n\n count--;\n }\n\n return true;\n }\n};", "memory": "10600" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n int arr[256];\n for(int i=0;i<magazine.size();i++)\n {\n arr[magazine[i]]++;\n }\n for(int i=0;i<ransomNote.size();i++)\n {\n arr[ransomNote[i]]--;\n if(arr[ransomNote[i]]<0) return false;\n }\n return true;\n }\n};", "memory": "10700" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n int rs[256];\n int mg[256];\n if(ransomNote.length()>magazine.length())\n return false;\n for (int i = 0; i < ransomNote.length(); i++) {\n rs[ransomNote[i]]++;\n }\n\n // Count character frequencies in magazine\n for (int i = 0; i < magazine.length(); i++) {\n mg[magazine[i]]++;\n }\n for(int i=0;i<256;i++)\n {\n if(rs[i]>mg[i]){\n return false;\n }\n \n \n }\n return true;\n \n \n }\n};", "memory": "10800" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "#define DEBUG 0\n\nclass Solution {\npublic:\n unordered_map<char, int> convertMagazine(string magazine)\n {\n unordered_map<char, int> magazineMap;\n char curLetter = '/0';\n\n for (unsigned int i = 0; i < magazine.size(); i++)\n {\n curLetter = magazine.at(i);\n\n if (magazineMap.contains(curLetter))\n magazineMap.at(curLetter)++;\n\n else\n magazineMap.insert(make_pair(curLetter, 1));\n }\n\n return magazineMap;\n }\n\n bool canConstruct(string ransomNote, string magazine) \n {\n unordered_map<char, int> magazineMap = convertMagazine(magazine);\n char curLetter = '/0';\n \n for (unsigned int i = 0; i < ransomNote.size(); i++)\n {\n curLetter = ransomNote.at(i);\n if (!magazineMap.contains(curLetter))\n return false;\n\n if (magazineMap.at(curLetter) <= 0)\n return false;\n\n magazineMap.at(curLetter)--;\n }\n\n return true;\n }\n};", "memory": "10900" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n string a = ransomNote, b = magazine;\n sort(a.begin(), a.end());\n sort(b.begin(), b.end());\n cout << a << endl << b;\n int i = 0;\n int j = 0;\n while (i < a.size() && j < b.size()) {\n if (a[i] == b[j]) {\n i++; j++;\n } else {\n j++;\n }\n }\n return i == a.size();\n }\n};", "memory": "10900" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(const string& ransomNote, const string& magazine) {\n std::unordered_map<char, int> magazineMap;\n for (char c : magazine) {\n magazineMap[c]++;\n }\n for (char c : ransomNote) {\n magazineMap[c]--;\n if (magazineMap[c] < 0) {\n return false;\n }\n }\n return true;\n }\n};", "memory": "11000" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n multimap<char, int> lettersMap;\n for (int i = 0; i < ransomNote.length(); i++){\n lettersMap.insert({ransomNote[i], 0});\n }\n\n int lettersFound = 0;\n for (int i = 0; i < magazine.length(); i++){\n if (lettersMap.find(magazine[i]) != lettersMap.end()){\n lettersFound++;\n if (lettersFound == ransomNote.length())\n return true;\n lettersMap.erase(lettersMap.find(magazine[i]));\n }\n }\n return false;\n\n /*\n int lettersFound = 0;\n for (int i = ransomNote.length() - 1; i > -1; i--){\n for (int j = magazine.length() - 1; j > -1; j--){\n if (magazine[j] == ransomNote[i]){\n lettersFound++;\n magazine.erase(j, 1);\n if (lettersFound == ransomNote.length())\n return true;\n break;\n }\n }\n }\n return false;*/\n }\n};", "memory": "11100" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char,int>count;\n for(int i=0;i<magazine.size();i++){\n count[magazine[i]]++;\n }\n \n \n for(int i=0;i<ransomNote.size();i++){\n count[ransomNote[i]]--;\n if(count[ransomNote[i]]<0)return false;\n }\n return true;\n }\n};", "memory": "11200" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string r, string m) {\n unordered_map<char,int>mp;\nfor(char ch:m){\n if(mp.find(ch)==mp.end())mp[ch]=1;\n else mp[ch]++;\n}\nfor(char c:r){\n if(mp.find(c)!=mp.end()&&mp[c]>0)mp[c]--;\n else return false;\n}\n\nreturn true;\n }\n};", "memory": "11300" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char, int> letters;\n for (char letter: magazine) {\n if (letters.find(letter) == letters.end()) {\n letters[letter] = 1;\n } else {\n letters[letter] += 1;\n }\n }\n \n for (char letter: ransomNote) {\n if (letters.find(letter) == letters.end()) {\n return false;\n } else {\n if (letters[letter] <= 0) {\n return false;\n }\n letters[letter] -= 1;\n }\n }\n return true;\n }\n};", "memory": "11300" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char,int> m;\n for (auto mag:magazine){\n m[mag]++;\n }\n for (auto ran:ransomNote){\n if(m[ran]==0)\n return false;\n m[ran]--;\n }\n return true;\n }\n};", "memory": "11400" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char,int> data;\n for(char e : ransomNote)\n data[e]++;\n for(char e : magazine)\n data[e]--;\n for(auto e : data){\n if(e.second>0)\n return false;\n }\n return true;\n }\n};", "memory": "11400" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string r, string m) {\n unordered_map<char,int>mp;\nfor(char ch:m){\n if(mp.find(ch)==mp.end())mp[ch]=1;\n else mp[ch]++;\n}\nfor(char c:r){\n if(mp.find(c)!=mp.end()&&mp[c]>0)mp[c]--;\n else return false;\n}\n\nreturn true;\n }\n};", "memory": "11500" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char,int> map;\n for(int i = 0;i<magazine.size();i++){\n map[magazine[i]]++;\n }\n for(int i = 0;i<ransomNote.size();i++){\n if(map[ransomNote[i]]){\n map[ransomNote[i]]--;\n }else{\n return false;\n }\n }\n return true;\n }\n};", "memory": "11500" }
383
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> ransomNote = "a", magazine = "b" <strong>Output:</strong> false </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab" <strong>Output:</strong> false </pre><p><strong class="example">Example 3:</strong></p> <pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab" <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ransomNote.length, magazine.length &lt;= 10<sup>5</sup></code></li> <li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n unordered_map<char,int> hashmap;\n \n for(char ch:magazine){\n hashmap[ch]++;\n }\n for(char ch: ransomNote){\n if(hashmap[ch]>0){\n hashmap[ch]--;\n }\n else return false;\n }\n return true;\n }\n};", "memory": "11600" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> v, c;\npublic:\n Solution(const vector<int>& nums) : v(nums), c(nums) {}\n \n const vector<int>& reset() const {\n return v;\n }\n \n const vector<int>& shuffle() {\n static std::random_device rd;\n static std::mt19937 g(rd());\n std::shuffle(c.begin(), c.end(), g);\n return c;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "60400" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> v;\npublic:\n Solution(const vector<int>& nums) : v(nums) {}\n \n const vector<int>& reset() {\n return v;\n }\n \n vector<int> shuffle() {\n static std::random_device rd;\n static std::mt19937 g(rd());\n auto c = v;\n std::shuffle(c.begin(), c.end(), g);\n return c;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61100" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> v;\npublic:\n Solution(const vector<int>& nums) : v(nums) {}\n \n const vector<int>& reset() const {\n return v;\n }\n \n vector<int> shuffle() const {\n static std::random_device rd;\n static std::mt19937 g(rd());\n auto c = v;\n std::shuffle(c.begin(), c.end(), g);\n return c;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61200" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> orig;\n int n;\n Solution(vector<int>& nums) {\n orig = nums;\n n = nums.size();\n }\n\n vector<int> reset() { return orig; }\n\n vector<int> shuffle() {\n vector<int> s = orig;\n for (int i = 0; i < n; i++) {\n int next = rand() % (n - i);\n swap(s[i], s[next + i]);\n }\n return s;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61600" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\nvector<int>orignal;\nint n;\n Solution(vector<int>& nums) {\n orignal = nums;\n n=nums.size();\n }\n \n vector<int> reset() {\n return orignal;\n }\n \n vector<int> shuffle() {\n vector<int>shuffled = orignal;\n int back = n;\n for(int i=n-1; i>0; i--){\n int j = rand()%back;\n swap(shuffled[i],shuffled[j]);\n back--;\n }\n return shuffled;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61700" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> nums;\n vector<int> a;\npublic:\n Solution(vector<int>& nums) {\n this->nums=nums;\n a=nums;\n }\n \n vector<int> reset() {\n return nums;\n }\n \n vector<int> shuffle() {\n for(int i=0;i<a.size()/2;i++){\n swap(a[i],a[rand()%a.size()]);\n }\n return a;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61800" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> original;\n int n;\npublic:\n Solution(vector<int>& nums) {\n original = nums;\n n = original.size();\n }\n \n vector<int> reset() {\n return original;\n }\n \n vector<int> shuffle() {\n vector<int> shuffled = original;\n int leftSize = n;\n for(int i=n-1;i>=0;i--){\n int j = rand()%leftSize;\n swap(shuffled[i],shuffled[j]);\n leftSize--;\n }\n return shuffled;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61800" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> nums;\npublic:\n Solution(vector<int>& nums) {\n this->nums = nums;\n }\n \n vector<int> reset() {\n return nums;\n }\n \n vector<int> shuffle() {\n vector<int> shuffledArray = nums;\n\n int n = nums.size();\n for(int i=1; i<=n; i++) {\n int index = rand() % i;\n\n if(index != i-1) {\n swap(shuffledArray[i-1], shuffledArray[index]);\n }\n }\n return shuffledArray;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61900" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
0
{ "code": "class Solution {\n vector<int> nums;\npublic:\n Solution(vector<int>& nums) {\n this->nums=nums;\n }\n \n vector<int> reset() {\n return nums;\n }\n \n vector<int> shuffle() {\n vector<int> ans=nums;\n for(int i=0;i<100;i++){\n swap(ans[rand()%ans.size()],ans[rand()%ans.size()]);\n }\n return ans;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "61900" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> v1,v2;\n Solution(vector<int>& nums) {\n v1=v2=nums;\n }\n \n vector<int> reset() {\n return v1;\n }\n \n vector<int> shuffle() {\n int x = rand()%v1.size();\n int y = rand()%v1.size();\n\n swap(v2[x],v2[y]);\n return v2;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "62000" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> nums;\n vector<int> original;\n Solution(vector<int>& n) {\n original=n;\n nums=n;\n }\n \n vector<int> reset() {\n return original;\n }\n \n vector<int> shuffle() {\n int i=rand();\n i%=nums.size();\n swap(nums[0],nums[i]);\n return nums;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "62000" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\n vector<int> nums;\n vector<int> original;\n\npublic:\n Solution(vector<int>& nums) {\n this->nums = nums;\n this->original = nums;\n }\n \n vector<int> reset() {\n this->nums = original;\n return original;\n }\n \n vector<int> shuffle() {\n int n = nums.size();\n for(int i=1; i<=n; i++) {\n int index = rand() % i;\n\n if(index != i-1) {\n swap(nums[i-1], nums[index]);\n }\n }\n return nums;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "62100" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "#include <random>\n\nclass Solution {\n // Seed with a real random value, if available\n default_random_engine re{std::random_device{}()};\n\npublic:\n const vector<int> original;\n vector<int> arr; // for return\n Solution(vector<int>& nums) : original{nums}, arr{nums} {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n vector<int> reset() {\n arr = original;\n return arr;\n }\n\n vector<int> shuffle() {\n for (int i = arr.size() - 1; i > 0; --i) {\n const auto idx = std::uniform_int_distribution<int>(0, i)(re);\n std::swap(arr[i], arr[idx]);\n }\n return arr;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "62200" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> arr;\n vector<int> result;\n int shuffleIndex=0;\n Solution(vector<int>& nums) {\n arr=nums;\n }\n \n vector<int> reset() {\n return arr;\n }\n \n vector<int> shuffle() {\n \n result=arr;\n std::random_device rd;\n std::mt19937 g(rd());\n std::shuffle(result.begin(), result.end(), g);\n return result;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "63100" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "#include <random>\n#include <iterator> \nclass Solution {\n vector<int> original; \n random_device rd;\npublic:\n Solution(vector<int>& nums) {\n for(auto it: nums){\n original.push_back(it);\n }\n }\n \n vector<int> reset() {\n return original; \n }\n \n vector<int> shuffle() {\n mt19937 g(rd());\n vector<int> v = original; \n std::shuffle(v.begin(), v.end(), g);\n return v; \n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "63200" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>arr;\n vector<int>orig;\n int n;\n Solution(vector<int>& nums) {\n arr = nums;\n orig = nums;\n n = arr.size();\n }\n \n vector<int> reset() {\n return orig;\n }\n \n vector<int> shuffle() {\n vector<int>s;\n for(int i=n-1;i>=0;i--){\n int random = rand()%(i+1);\n s.push_back(arr[random]);\n swap(arr[random], arr[i]);\n }\n return s;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "63400" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\n map<int, int> map;\n int len = 0;\npublic:\n Solution(vector<int>& nums) {\n len = nums.size();\n for(int i = 0; i < len; ++i)\n {\n map[i] = nums[i];\n }\n }\n \n vector<int> reset() {\n vector<int> result;\n for(auto &pair: map){\n result.push_back(pair.second);\n }\n return result;\n }\n \n vector<int> shuffle() {\n vector<int> res(len, 0);\n // int random_i = rand() % len;\n for(int i = 0; i < len; i++){\n res[i] = map[i];\n }\n uint seed = 0;\n random_shuffle(res.begin(), res.end());\n return res;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "63500" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> arr;\n Solution(vector<int>& nums) {\n for(int i : nums) arr.push_back(i);\n }\n \n vector<int> reset() {\n\n return arr;\n \n }\n \n vector<int> shuffle() {\n int n = arr.size();\n\n vector<int> res;\n\n for(int i : arr) res.push_back(i);\n int i = n;\n\n while(i > 0) {\n int swapIndex = rand()%i;\n int temp = res[swapIndex];\n res[swapIndex] = res[i-1];\n res[i-1] = temp;\n i--;\n }\n\n return res;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "63600" }
384
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> <li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li> <li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;Solution&quot;, &quot;shuffle&quot;, &quot;reset&quot;, &quot;shuffle&quot;] [[[1, 2, 3]], [], [], []] <strong>Output</strong> [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] <strong>Explanation</strong> Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li>All the elements of <code>nums</code> are <strong>unique</strong>.</li> <li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> org;\n vector<int> suf;\n Solution(vector<int>& nums) {\n org=nums;\n suf=nums;\n }\n \n vector<int> reset() {\n org=suf;\n return org;\n }\n \n vector<int> shuffle() {\n vector<int> nums=suf;\n vector<int> ans;\n int n=suf.size();\n for(int i=0;i<n;i++){\n int curr=n-i;\n int r=rand()%curr;\n \n swap(nums[r],nums[nums.size()-1]);\n ans.push_back(nums[nums.size()-1]);\n nums.pop_back();\n }\n \n return ans;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(nums);\n * vector<int> param_1 = obj->reset();\n * vector<int> param_2 = obj->shuffle();\n */", "memory": "64099" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n return 0;\n }\n};\n\nauto _ = [](){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::noskipws(std::cin);\n \n string s;\n while (getline(cin, s)) {\n int seen[26] = {0};\n for (int i=1; i<s.size()-1; i++) {\n int j = s[i] - 'a';\n if (seen[j] == 0) seen[j] = i;\n else seen[j] = -1;\n }\n int ans = -1;\n for (auto& pos : seen) {\n if (pos != 0 && pos != -1) {\n if (ans == -1) ans = 100001;\n ans = min(ans, pos - 1);\n }\n }\n out << ans << \"\\n\";\n }\n\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n}();", "memory": "7700" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tint firstUniqChar(const string &word) {\n\t\tvector<int> characterCount(26, 0);\n\n\t\tfor (const char &ch : word) {\n\t\t\t++characterCount[ch - 'a'];\n\t\t}\n\n\t\tfor (int index = 0; index < word.size(); ++index) {\n\t\t\tif (characterCount[word[index] - 'a'] == 1) {\n\t\t\t\treturn index;\n\t\t\t}\n\t\t}\n\n\t\treturn -1;\n\t}\n};", "memory": "11700" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "\nclass Solution { \n int min_index = INT_MAX;\npublic: \n int firstUniqChar(const string& s) {\n vector<pair<int,int>> vec(26, {0,0}); \n \n for(int i=0; i<s.size();++i) {\n int index = s[i]-'a';\n if(vec[index].first == 0)\n vec[index].second = i;\n vec[index].first++;\n } \n\n for(const auto& p : vec) {\n if(p.first == 1)\n min_index = std::min(p.second, min_index);\n }\n return (min_index == INT_MAX ? -1 : min_index);\n } \n};", "memory": "11800" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int firstUniqChar(string &s){\n int n=s.size();\n vector<int> freq(26, INT_MAX);\n for(int i=0;i<n;i++){\n int ch=s[i]-'a';\n if(freq[ch]==INT_MAX) freq[ch]=(i+1);\n else freq[ch]=-(i+1);\n }\n int ans=INT_MAX;\n for(int i=0;i<26;i++){\n if(freq[i]>0) ans=min(freq[i], ans);\n }\n return ans==INT_MAX?-1:ans-1;\n }\n};", "memory": "11900" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int firstUniqChar(const string& s) {\n std::array<std::pair<int,int>,'z'-'a'+1> table;\n for (size_t i=0;i<table.size();++i) table[i].first=INT_MAX;\n for (int i=0;i<s.size();++i) {\n auto& t = table[s[i]-'a'];\n t.first=std::min(t.first,i);\n ++t.second;\n }\n int min=INT_MAX;\n for (auto p : table) min=p.second==1?std::min(min,p.first):min;\n return min==INT_MAX?-1:min;\n }\n};", "memory": "12000" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n static const int CHAR=256;\n int firstUniqChar(string &s) {\n int fI[CHAR];\n fill(fI,fI+CHAR,-1);\n \n for(int i=0;i<s.length();i++){\n if(fI[s[i]]==-1)\n fI[s[i]]=i;\n else\n fI[s[i]]=-2;\n }\n int res=INT_MAX;\n for(int i=0;i<CHAR;i++){\n if(fI[i]>=0)res=min(res,fI[i]);\n }\n return (res==INT_MAX)?-1:res;\n }\n};", "memory": "12100" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\nstatic const int CHAR=256;\n int firstUniqChar(string &s) {\n int count[CHAR]={0};\n\n for(int i=0;i<s.length();i++){\n count[s[i]]++;\n }\n for(int i=0;i<s.length();i++){\n if(count[s[i]]==1){\n return i;\n }\n }\n return -1;\n }\n};", "memory": "12200" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "template<typename T>\nclass LinkedListNode {\n public:\n T data;\n LinkedListNode<T>* next;\n LinkedListNode<T>* prev;\n\n LinkedListNode(T data, LinkedListNode<T>* next=nullptr, LinkedListNode<T>* prev=nullptr) : data(data), next(next), prev(prev){}\n LinkedListNode() = default;\n\n static void deleteNode(LinkedListNode<T>* node) {\n if (!node) return;\n if (node->prev) {\n node->prev->next = node->next;\n }\n if (node->next) {\n node->next->prev = node->prev;\n }\n delete node;\n }\n\n static void printList(LinkedListNode<T>* head) {\n while (head) {\n cout << head->data;\n cout << (head->next ? \" \" : \"\\n\");\n head = head->next;\n }\n }\n};\n\nclass Solution {\npublic:\n int firstUniqChar(const string& s) {\n typedef LinkedListNode<int> IntLinkedListNode;\n typedef LinkedListNode<int>* IntLinkedListNodePtr;\n IntLinkedListNodePtr head = new IntLinkedListNode();\n IntLinkedListNodePtr mover = head;\n unordered_map<int, IntLinkedListNodePtr> uniqueOrder;\n int hash[26] = {0};\n for (int i = 0 ; i < s.length(); i++) {\n int currChar = s[i]-'a';\n if (hash[currChar]) {\n const auto it = uniqueOrder.find(currChar);\n if (it != uniqueOrder.end()) {\n if (mover == it->second) {\n mover = mover->prev;\n }\n IntLinkedListNode::deleteNode(it->second);\n uniqueOrder.erase(currChar);\n }\n }\n else{\n hash[currChar] = 1;\n mover->next = new IntLinkedListNode(i, nullptr, mover);\n uniqueOrder[currChar] = mover->next;\n mover = mover->next;\n }\n }\n return head->next ? head->next->data : -1;\n }\n};", "memory": "12500" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n int n = s.length();\n\n // Step 1: Iterate over each character in the string\n for (int i = 0; i < n; ++i) {\n bool found = true;\n\n // Step 2: Check if the character repeats in the\n // rest of the string\n for (int j = 0; j < n; ++j) {\n if (i != j && s[i] == s[j]) {\n found = false;\n break;\n }\n }\n\n // Step 3: If character does not repeat, return its\n // index\n if (found) {\n return i;\n }\n }\n\n // Step 4: If no such character is found, return -1\n return -1;\n }\n};", "memory": "12700" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\n#include <string>\n\nclass Solution {\npublic:\n int firstUniqChar(const std::string& s) {\n std::unordered_map<char,int>pos, occ;\n for(int i = 0; i < s.size(); i++){\n if(pos.contains(s[i]))\n occ[s[i]]++;\n else{\n pos[s[i]] = i;\n occ[s[i]] = 1;\n }\n }\n for(char i : s)\n if(occ[i]==1)\n return pos[i];\n return -1;\n }\n};", "memory": "13400" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "#include <unordered_map>\n#include <string>\n\nclass Solution {\npublic:\n int firstUniqChar(const std::string& s) {\n std::unordered_map<char,int>pos, occ;\n for(int i = 0; i < s.size(); i++){\n if(pos.contains(s[i]))\n occ[s[i]]++;\n else{\n pos[s[i]] = i;\n occ[s[i]] = 1;\n }\n }\n for(char i : s)\n if(occ[i]==1)\n return pos[i];\n return -1;\n }\n};", "memory": "13500" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n int min = s.size();\n int c[26] {0};\n queue<pair<char,int>> q;\n for(int i=0; i<s.size(); i++) {\n c[s[i]-'a']++;\n if(c[s[i]-'a']==1)\n q.push({s[i], i});\n }\n while(!q.empty()) {\n auto [ch, ind] = q.front();\n q.pop();\n if(c[ch-'a']==1)\n return ind;\n }\n return -1;\n }\n};", "memory": "13600" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n vector<int> freq(26, 0);\n queue<pair<char, int>> q;\n \n for (int i = 0; i < s.length(); i++) {\n char c = s[i];\n freq[c - 'a']++;\n\n if (freq[c - 'a'] == 1) {\n q.push({c, i});\n }\n \n while (!q.empty() && freq[q.front().first - 'a'] > 1) {\n q.pop();\n }\n }\n \n if (!q.empty()) {\n return q.front().second;\n }\n \n return -1;\n }\n};", "memory": "13700" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n map<char ,int> m;\n \n for(int i=0 ;i<s.size();i++){\n auto it = m.find(s[i]);\n if(it == m.end()){\n m[s[i]] = 1;\n }else{\n it->second += 1;\n }\n }\n\n for(int i=0 ;i<s.size() ;i++){\n auto it = m.find(s[i]);\n\n if(it->second == 1)return i;\n }\n\n return -1;\n }\n};", "memory": "13800" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_set<char>se;\n unordered_set<char>se1;\n for (int i=0;i<s.length();i++)\n {\n if (se1.count(s[i])==0)\n {\n se.insert(s[i]);\n se1.insert(s[i]);\n }\n else if (se1.count(s[i])!=0)\n {\n se.erase(s[i]);\n }\n }\n for (int i=0;i<s.length();i++)\n {\n if (se.count(s[i])!=0)\n {\n return i;\n }\n }\n return -1;\n \n }\n};", "memory": "13900" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char, int> hashh;\n for(int i = 0; i< s.size(); i++){\n hashh[s[i]]++;\n }\n\n for(int i = 0; i< s.size(); i++){\n if(hashh[s[i]] == 1){return i;}\n }\n return -1;\n\n }\n};", "memory": "14000" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char,int> mpp;\n for(int i=0;i< s.length();i++){\n mpp[s[i]]++;\n }\n for(int i=0;i< s.length();i++){\n if(mpp[s[i]]==1){\n return i;\n }\n \n }\n return -1;\n }\n};", "memory": "14100" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n std::unordered_map<char, int> m;\n\n for (char c: s) {\n m[c]++;\n }\n\n for (int i=0; i<s.size(); i++) {\n if (m[s[i]] == 1) {\n return i;\n }\n }\n return -1;\n }\n};", "memory": "14200" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char, int> m;\n for (char c:s) {\n if (m.find(c) == m.end()) {\n m[c] = 1;\n }\n else {\n m[c]+=1;\n }\n }\n \n for (int i=0;i<s.size();i++) {\n if (m[s[i]] == 1) {\n return i;\n }\n }\n \n return -1;\n }\n};", "memory": "14200" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char,int> mapp(26);\n for(int i=0;i<s.size();i++){\n mapp[s[i]]++;\n }\n for(int i=0;i<s.size();i++){\n if(mapp[s[i]]==1){\n return i;\n }\n }\n return -1;\n }\n};", "memory": "14300" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s)\n {\n map<char,int> M;\n int index,n;\n for (auto val : s) \n {\n if (M.find(val)!=M.end()) M[val]++;\n else M[val]=1;\n }\n index=-1,n=s.length();\n for (int i=0;i<n;i++)\n {\n if (M[s[i]]>1) continue;\n else \n {\n index=i;\n break;\n }\n }\n return index;\n\n }\n};", "memory": "14300" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char, int> countMap;\n \n // Count occurrences of each character\n for (char c : s) {\n countMap[c]++;\n }\n \n // Find the first character with a count of 1\n for (int i = 0; i < s.size(); ++i) {\n if (countMap[s[i]] == 1) {\n return i;\n }\n }\n \n return -1; \n }\n};", "memory": "14400" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char , int> mp;\n int i = 0;\n \n for(char c : s){\n mp[c]++;\n }\n\n for(char c : s){\n if(mp[c] == 1){\n return i;\n }\n ++i;\n }\n\n return -1;\n }\n};", "memory": "14400" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char,int>mp;\n for(auto x:s){\n mp[x]++;\n }\n\n for(int i=0;i<s.size();i++){\n if(mp[s[i]]==1){\n return i;\n break;\n }\n }\n return -1;\n }\n};", "memory": "14500" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char,int> hashtable;\n for(int i=0;i<s.length();i++){\n hashtable[s[i]]++;\n }\n for(int i=0;i<s.length();i++){\n if(hashtable[s[i]] == 1){\n return i;\n }\n }\n return -1;\n }\n};", "memory": "14500" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char, int> freq;\n for (char c : s) {\n freq[c]++;\n }\n\n for (int i = 0; i < s.size(); i++) {\n if (freq[s[i]] == 1) {\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "14600" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n std::unordered_map<char, int> charCount;\n std::set<char> uniqueChars;\n\n // Count the frequency of each character\n for (char c : s) {\n charCount[c]++;\n uniqueChars.insert(c);\n }\n\n // Traverse the string again to find the first unique character\n for (int i = 0; i < s.size(); ++i) {\n if (charCount[s[i]] == 1) {\n return i;\n }\n }\n\n return -1;\n }\n};", "memory": "14600" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n map<char,int> mp;\n for(int i=0;i<s.length();i++)\n {\n mp[s[i]]++;\n }\n vector<int> v;\n for(auto i:mp)\n {\n if(i.second==1)\n {\n v.push_back(i.first);\n }\n }\n int mini=INT_MAX;\n int x;\n for(int i=0;i<v.size();i++)\n {\n for(int j=0;j<s.length();j++)\n {\n if(v[i]==s[j])\n {\n x=j;\n } \n }\n mini=min(mini,x);\n }\n if(v.size()==0)\n {\n mini=-1;\n }\n return mini;\n }\n};", "memory": "14700" }
387
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The character <code>&#39;l&#39;</code> at index 0 is the first character that does not occur at any other index.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;loveleetcode&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aabb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int firstUniqChar(string s) {\n int freq[26] = {0};\n queue<char> q;\n\n for(int i=0; i<s.length(); i++)\n {\n char ch = s[i];\n freq[ch-'a']++;\n q.push(ch);\n\n while(!q.empty())\n {\n char frontChar = q.front();\n if(freq[frontChar-'a']>1)\n {\n q.pop();\n }\n else{\n break;\n }\n }\n }\n\n if(q.empty())\n {\n return -1;\n }\n\n \n return s.find(q.front());\n }\n};", "memory": "14700" }
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>
0
{ "code": "class Solution {\npublic:\n std::vector<std::vector<std::string>> suggestedProducts(std::vector<std::string> &products, std::string searchWord)\n {\n std::vector<std::vector<std::string>> results(searchWord.size());\n\n std::sort(products.begin(), products.end());\n\n std::size_t left = 0;\n\n for (std::size_t i = 0; i < searchWord.size(); ++i)\n {\n for (std::size_t l = left; l < products.size(); ++l)\n {\n if (products[l].starts_with({searchWord.begin(), searchWord.begin() + i + 1}))\n {\n left = l;\n std::size_t right = left + 1;\n for (std::size_t r = right; r < left + 3 && r < products.size(); ++r)\n {\n if (products[r].starts_with({searchWord.begin(), searchWord.begin() + i + 1}))\n {\n ++right;\n }\n }\n results[i].insert(results[i].end(), products.begin() + left, products.begin() + right);\n break;\n }\n }\n\n if (!results[i].size())\n {\n break;\n }\n }\n\n return results;\n }\n};", "memory": "30690" }
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>
0
{ "code": "class Solution\n{\npublic:\n vector<vector<string>> suggestedProducts(vector<string> &products, string searchWord)\n {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n sort(products.begin(), products.end());\n\n vector<vector<string>> ans(searchWord.size());\n\n int s = 0;\n int e = products.size() - 1;\n\n for (int i = 0; i < searchWord.size(); i++)\n {\n\n while (s < products.size() && (i >= products[s].size() || products[s][i] < searchWord[i]))\n s++;\n\n while (e >= 0 && (i >= products[e].size() || products[e][i] > searchWord[i]))\n e--;\n\n if (e < s)\n break;\n\n ans[i] = (vector<string>(products.begin() + s, e - s >= 3 ? products.begin() + s + 3 : products.begin() + e + 1));\n }\n\n return ans;\n }\n};", "memory": "30690" }
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>
1
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n // Sort the product list alpha\n std::sort(products.begin(), products.end());\n\n vector<vector<string>> result;\n\n int leftIndex = 0;\n int rightIndex = products.size();\n string currentWord = \"\";\n\n for(char c: searchWord){\n vector<string> currentWordRes = {};\n currentWord += c;\n auto it = std::lower_bound(products.begin() + leftIndex, products.begin() + rightIndex, currentWord);\n\n // If iterator not at end, update index values\n if(it != products.end()) leftIndex = std::distance(products.begin(), it);\n else{\n leftIndex = products.size();\n result.push_back({});\n continue;\n }\n\n rightIndex = leftIndex; // Start rightIndex from leftIndex\n while (rightIndex < products.size() && products[rightIndex].substr(0, currentWord.size()) == currentWord) {\n rightIndex++;\n }\n\n // while(currentWordRes.size() < 3 && )\n for(int i=0; i<std::min(3, rightIndex-leftIndex); i++){\n currentWordRes.push_back(products[leftIndex+i]);\n }\n\n result.push_back(currentWordRes);\n\n\n }\n\n return result;\n }\n\n};", "memory": "38470" }
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>
1
{ "code": "class Solution {\npublic:\n pair<int, int> search(string word, vector<string>& products, int start, int end){\n bool is_start_set = false;\n int new_start = -1;\n int new_end = -1;\n for(int i=start; i<=end; i++){\n if(products[i].size()<word.size()) continue;\n if(products[i].substr(0, word.size()) == word){\n if(!is_start_set){\n new_start = i;\n new_end = i;\n is_start_set = true;\n } \n else{\n new_end++;\n }\n\n }\n else if(!is_start_set) continue;\n else break;\n }\n return make_pair(new_start, new_end);\n\n }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n int start =0;\n int end = products.size()-1;\n vector<vector<string>> out;\n for(int i=0; i<searchWord.size(); i++){\n vector<string> sub_out;\n if(start == -1){ // No matches found for current substring and hence no matches will be found for substrings greater\n out.emplace_back(sub_out);\n continue;\n }\n pair<int, int> new_loc = search(searchWord.substr(0, i+1), products, start, end); // mou, products, 0, 4\n start = new_loc.first;\n end = new_loc.second;\n if(start == -1){ // No matches found for current substring and hence no matches will be found for substrings greater\n out.emplace_back(sub_out);\n continue;\n }\n cout<<start<<\" \"<<end<<endl;\n int counter = 0;\n for(int j=start; j<=end; j++){\n if (counter==3) break;\n sub_out.emplace_back(products[j]);\n counter++;\n }\n out.emplace_back(sub_out);\n }\n return out;\n }\n};", "memory": "38470" }
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>
1
{ "code": "class Solution{\npublic:\n vector<vector<string>> suggestedProducts(vector<string> &products, string searchWord){\n cin.tie(NULL);\n cout.tie(NULL);\n vector<string> havePrefix;\n vector<vector<string>> result;\n sort(products.begin(),products.end());\n for (int i = 0; i < searchWord.length(); i++){\n\n string PrefitillFormed = searchWord.substr(0, i + 1);\n for (int j = 0; j < products.size(); j++){\n bool startwithprefix = products[j].find(PrefitillFormed) == 0;\n if (startwithprefix){\n //if product[i] start with the prefix till formed in loop\n havePrefix.push_back(products[j]);\n }\n }\n if(havePrefix.size()<=3)\n result.push_back(havePrefix);\n else{\n result.push_back({havePrefix[0],havePrefix[1],havePrefix[2]});\n }\n havePrefix.clear(); \n }\n return result;\n }\n};", "memory": "46250" }
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>
1
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(), products.end());\n\n int i = 0;\n vector<string> prev(products.begin(), products.end());\n vector<vector<string>> res;\n while(i < searchWord.size()){\n char ch = searchWord[i];\n vector<string> curr;\n\n for(auto st : prev){\n if(i < st.length() and st[i] == ch) curr.push_back(st);\n else if(curr.size() > 0) break;\n }\n\n prev = curr;\n\n vector<string> pushtores;\n for(int i = 0; i < min(3, (int) curr.size()); i++) pushtores.push_back(curr[i]);\n res.push_back(pushtores);\n\n i++;\n }\n\n return res;\n }\n};", "memory": "69590" }
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>
1
{ "code": "class Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> ans(searchWord.size());\n\n vector<string> temp = products;\n priority_queue<string> pq;\n for(int i =0;i<searchWord.size();i++){\n vector<string> s;\n for(int j =0;j<temp.size();j++){\n if(temp[j][i] == searchWord[i]){\n s.push_back(temp[j]);\n pq.push(temp[j]);\n if(pq.size() > 3)\n pq.pop();\n\n }\n }\n temp = s;\n while(!pq.empty()){\n string d = pq.top();\n pq.pop();\n ans[i].push_back(d);\n }\n\n sort(ans[i].begin(),ans[i].end());\n }\n return ans;\n }\n\n \n};", "memory": "69590" }
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>
1
{ "code": "class TrieNode {\npublic:\n TrieNode* links[26];\n bool isEnd;\n TrieNode() {\n memset(links, 0, sizeof(links));\n isEnd = false;\n }\n};\n\nclass Trie {\npublic:\n TrieNode* root;\n Trie() { root = new TrieNode(); }\n\n void insert(string& word) {\n TrieNode* node = root;\n for (char ch : word) {\n if (!node->links[ch - 'a']) {\n node->links[ch - 'a'] = new TrieNode();\n }\n node = node->links[ch - 'a'];\n }\n node->isEnd = true;\n }\n\n vector<string> search(string& word) {\n TrieNode* node = root;\n vector<string> prod;\n for (char ch : word) {\n if (node->links[ch - 'a'])\n node = node->links[ch - 'a'];\n else\n return prod;\n }\n dfs(node, word, prod);\n return prod;\n }\n void dfs(TrieNode* node, string& s, vector<string>& prod) {\n if (prod.size() == 3)\n return;\n if (node->isEnd) {\n prod.push_back(s);\n }\n for (char i = 'a'; i <= 'z'; i++) {\n if (prod.size() == 3)\n return;\n if (node->links[i - 'a']) {\n s.push_back(i);\n dfs(node->links[i - 'a'], s, prod);\n s.pop_back();\n }\n }\n }\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string search) {\n Trie trie = Trie();\n for (string& word : products) {\n trie.insert(word);\n }\n vector<vector<string>> ans;\n string pre = \"\";\n for (char& ch : search) {\n pre += ch;\n ans.push_back(trie.search(pre));\n }\n return ans;\n }\n};", "memory": "77370" }
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>
1
{ "code": "class Solution {\npublic:\n vector<string> startWith(vector<string>& products,string match){\n vector<string> newVec;\n for(auto str:products){\n string newStr = str.substr(0,match.size());\n if(newStr==match){\n newVec.push_back(str);\n }\n }\n return newVec;\n }\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n sort(products.begin(),products.end());\n string searchStr = \"\";\n vector<vector<string>> ans;\n for(int i=0;i<searchWord.size();i++){\n searchStr+=searchWord[i];\n products = startWith(products,searchStr);\n vector<string>tmp;\n for(int i=0;i<3&&i<products.size();i++){\n tmp.push_back(products[i]);\n }\n ans.push_back(tmp);\n }\n return ans;\n }\n};", "memory": "77370" }
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 {\nprivate:\n class Trie {\n private:\n struct Node {\n Node *next[26];\n bool last;\n \n Node() {for (int i = 0; i < 26; i++) next[i] = nullptr; last = 0;}\n };\n Node *head = new Node;\n\n public:\n void insert(string str) {\n Node *cur = head;\n for (int i = 0; i < str.size(); cur = cur->next[str[i]-'a'], i++) {\n if (cur->next[str[i]-'a'] == nullptr) cur->next[str[i]-'a'] = new Node;\n }\n cur->last = 1;\n }\n\n vector<string> search(string str) {\n Node *cur = head;\n vector<string> ret;\n for (int i = 0; i < str.size(); cur = cur->next[str[i]-'a'], i++) {\n if (cur->next[str[i]-'a'] == nullptr) return {};\n }\n\n int cnt = 3; dfs(str, cur, cnt, ret);\n return ret;\n }\n\n void dfs(string& str, Node *cur, int& cnt, vector<string>& ret) {\n if (cur->last == 1) {\n ret.push_back(str);\n cnt--;\n }\n\n for (int i = 0; i < 26; i++) {\n if (cur->next[i] == nullptr or cnt == 0) continue;\n str.push_back('a' + i);\n dfs(str, cur->next[i], cnt, ret);\n str.pop_back();\n }\n }\n };\n\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& prod, string word) {\n vector<vector<string>> ans;\n Trie *obj = new Trie;\n for (int i = 0; i < prod.size(); i++) obj->insert(prod[i]);\n\n string cur = \"\";\n for (int i = 0; i < word.size(); i++) {\n cur.push_back(word[i]);\n ans.push_back(obj->search(cur));\n }\n\n return ans;\n }\n};", "memory": "85150" }
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 TrieNode {\n public:\n // Initialize your data structure here.\n bool isWord;\n TrieNode *children[26];\n \n TrieNode() {\n isWord = false;\n \n for (int i = 0; i < 26; i++)\n children[i] = NULL;\n }\n};\nclass Solution {\nprivate:\n TrieNode* root;\n void insert(string word) {\n int len = word.length();\n TrieNode* curr = root;\n\n for(int i=0;i<len;i++) {\n int k = word[i]-'a';\n if(curr->children[k] == nullptr) {\n curr->children[k] = new TrieNode();\n }\n curr = curr->children[k];\n }\n curr->isWord = true;\n \n }\npublic:\n Solution() {\n root = new TrieNode();\n }\n void suggestionsRec(TrieNode* curr, string& cp, vector<string>& t){\n // found a string in Trie with the given prefix\n if(t.size() == 3) {\n return;\n }\n if (curr->isWord) {\n t.push_back(cp);\n // return;\n }\n // cout << currPrefix << endl;\n \n for (int i = 0; i < 26; i++)\n if (curr->children[i]) {\n // child node character value\n char child = 'a' + i;\n cp+=child;\n suggestionsRec(curr->children[i],\n cp, t);\n cp.pop_back();\n }\n }\n vector<string> autoSuggest(string q) {\n TrieNode* curr = root;\n\n int len = q.length();\n\n for(int i=0;i<len;i++) {\n int k = q[i]-'a';\n if(!curr->children[k]) return {};\n curr = curr->children[k];\n }\n vector<string> t;\n suggestionsRec(curr, q, t);\n return t;\n\n }\n vector<vector<string>> suggestedProducts(vector<string>& ps, string sw) {\n for(auto p: ps) {\n insert(p);\n }\n vector<vector<string>> ans;\n for(int i=0;i<sw.length();i++) {\n // cout<<sw.substr(0, i+1)<<endl;\n ans.push_back(autoSuggest(sw.substr(0, i+1)));\n }\n return ans;\n\n\n \n }\n};", "memory": "85150" }
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": "#include <vector>\n#include <string>\n#include <iostream>\nusing namespace std;\n\nclass Solution {\npublic:\n class TrieNode {\n public:\n char data;\n TrieNode* children[26];\n bool isTerminal;\n\n TrieNode(char ch) {\n data = ch;\n for(int i = 0; i < 26; i++) {\n children[i] = NULL;\n }\n isTerminal = false;\n }\n };\n\n class Trie {\n public:\n TrieNode* root;\n\n Trie() {\n root = new TrieNode('\\0');\n }\n\n void insertUtil(TrieNode* root, const string& word) {\n if (word.empty()) {\n root->isTerminal = true;\n return;\n }\n\n int index = word[0] - 'a';\n TrieNode* child;\n\n if (root->children[index] != NULL) {\n child = root->children[index];\n } else {\n child = new TrieNode(word[0]);\n root->children[index] = child;\n }\n\n insertUtil(child, word.substr(1));\n }\n\n void insertWord(const string& word) {\n insertUtil(root, word);\n }\n\n void printSuggestion(TrieNode* curr, vector<string>& temp, string& prefix, int& count) {\n if (count >= 3) {\n return; // Stop if we already have 3 suggestions\n }\n if (curr->isTerminal) {\n temp.push_back(prefix);\n count++;\n }\n for (char ch = 'a'; ch <= 'z'; ch++) {\n TrieNode* next = curr->children[ch - 'a'];\n if (next != NULL) {\n prefix.push_back(ch);\n printSuggestion(next, temp, prefix, count);\n prefix.pop_back();\n }\n }\n }\n\n vector<vector<string>> getSuggestion(const string& str) {\n TrieNode* prev = root;\n vector<vector<string>> output;\n string prefix = \"\";\n for (char lastCh : str) {\n prefix.push_back(lastCh);\n\n TrieNode* curr = (prev != NULL) ? prev->children[lastCh - 'a'] : NULL;\n\n if (curr == NULL) {\n output.push_back({}); // Add an empty list for no suggestions\n prev = NULL; // Ensure we don't process further\n } else {\n vector<string> temp;\n int count = 0; // Initialize count to 0\n printSuggestion(curr, temp, prefix, count);\n output.push_back(temp);\n prev = curr;\n }\n }\n return output;\n }\n };\n\n vector<vector<string>> suggestedProducts(vector<string>& products, const string& searchWord) {\n Trie* t = new Trie();\n\n for (const string& str : products) {\n t->insertWord(str);\n }\n\n vector<vector<string>> result = t->getSuggestion(searchWord);\n\n // Cleanup\n delete t;\n\n return result;\n }\n};\n\n", "memory": "92930" }
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 TrieNode {\n public:\n char data;\n TrieNode* children[26];\n bool isTerminal;\n\n TrieNode(char d) {\n this->data = d;\n for (int i = 0; i < 26; i++) {\n children[i] = nullptr;\n }\n this->isTerminal = false;\n }\n };\n\n TrieNode* root;\n\n Solution() {\n root = new TrieNode('\\0'); // Initialize root with a dummy character\n }\n\n void insertWord(TrieNode* root, const string& word) {\n // Base case\n if (word.length() == 0) {\n root->isTerminal = true;\n return;\n }\n\n char ch = word[0];\n int index = ch - 'a';\n TrieNode* child;\n\n // Check if the character node exists\n if (root->children[index] != nullptr) {\n child = root->children[index];\n } else {\n // Create a new TrieNode if not present\n child = new TrieNode(ch);\n root->children[index] = child;\n }\n\n // Recursive call for the next part of the word\n insertWord(child, word.substr(1));\n }\n\n bool searchWord(TrieNode* root, const string& word) {\n // Base case: if the word is empty, check if the current node is terminal\n if (word.length() == 0)\n return root->isTerminal;\n\n char ch = word[0];\n int index = ch - 'a'; // Changed to 'a' since input is in lowercase\n TrieNode* child;\n\n // If the character node exists, continue searching\n if (root->children[index] != nullptr) {\n child = root->children[index];\n } else {\n // If the character node doesn't exist, the word is not in the trie\n return false;\n }\n\n // Recursive call for the next part of the word\n return searchWord(child, word.substr(1));\n }\n\n void storeSuggestion(TrieNode* curr, vector<string>& temp, string& prefix, int limit = 3) {\n if (curr->isTerminal) {\n temp.push_back(prefix);\n if (temp.size() == limit) return;\n }\n\n // Explore all possible children from 'a' to 'z'\n for (char ch = 'a'; ch <= 'z'; ch++) {\n int index = ch - 'a';\n TrieNode* next = curr->children[index];\n if (next != nullptr) {\n // If a valid child exists, add it to the prefix and explore further\n prefix.push_back(ch);\n storeSuggestion(next, temp, prefix, limit);\n // Backtrack by removing the last character added to the prefix\n prefix.pop_back();\n if (temp.size() == limit) return; // Stop if we've already collected enough suggestions\n }\n }\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, const string& searchWord) {\n // Insert all products into the Trie\n for (const string& product : products) {\n insertWord(root, product);\n }\n\n TrieNode* prev = root;\n vector<vector<string>> output;\n string prefix = \"\";\n\n // Iterate through each character of the searchWord\n for (int i = 0; i < searchWord.length(); i++) {\n char lastCh = searchWord[i];\n int index = lastCh - 'a';\n TrieNode* curr = prev->children[index];\n\n // If no children exist for the current character, break out of the loop\n if (curr == nullptr) {\n break;\n } else {\n // Otherwise, continue storing suggestions\n vector<string> temp;\n prefix.push_back(lastCh);\n storeSuggestion(curr, temp, prefix);\n output.push_back(temp);\n prev = curr;\n }\n }\n\n // In case the prefix becomes shorter than searchWord, fill the remaining with empty vectors\n while (output.size() < searchWord.size()) {\n output.push_back({});\n }\n\n return output;\n }\n};\n", "memory": "92930" }
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 TrieNode {\npublic:\n char data;\n TrieNode *children[26];\n bool isTerminal;\n \n TrieNode(char ch) {\n data = ch;\n for(int i = 0; i < 26; i++) {\n children[i] = NULL;\n }\n isTerminal = false;\n }\n};\n\nclass Trie {\n TrieNode *root;\n \npublic:\n Trie() {\n root = new TrieNode('\\0');\n }\n\n void insertUtil(TrieNode* root, const string &word, int index) {\n if(index == word.length()) {\n root->isTerminal = true; \n return;\n }\n\n int charIndex = word[index] - 'a';\n TrieNode* child;\n \n if(root->children[charIndex] != NULL) {\n child = root->children[charIndex];\n } else {\n child = new TrieNode(word[index]);\n root->children[charIndex] = child;\n }\n\n insertUtil(child, word, index + 1);\n }\n\n void insert(const string &word) {\n insertUtil(root, word, 0);\n }\n\n void printSuggestions(TrieNode* curr, vector<string> &temp, string &prefix) {\n if(curr->isTerminal) {\n temp.push_back(prefix);\n }\n for(char ch = 'a'; ch <= 'z'; ch++) {\n TrieNode* next = curr->children[ch - 'a'];\n if(next != NULL){\n prefix.push_back(ch);\n printSuggestions(next, temp, prefix);\n prefix.pop_back();\n }\n }\n }\n\n vector<vector<string>> getSuggestions(const string &str) {\n TrieNode* prev = root;\n vector<vector<string>> output;\n string prefix = \"\";\n\n for(int i = 0; i < str.length(); i++) {\n char lastch = str[i];\n prefix.push_back(lastch);\n TrieNode *curr = prev->children[lastch - 'a'];\n\n if(curr == NULL) {\n // If no more matches in the Trie, just push empty vectors for remaining characters\n while (i < str.length()) {\n output.push_back({});\n i++;\n }\n break;\n }\n\n vector<string> temp;\n printSuggestions(curr, temp, prefix);\n if(temp.size() > 3) {\n temp.resize(3); // Keep only top 3 suggestions\n }\n output.push_back(temp);\n prev = curr;\n }\n\n return output;\n}\n\n\n ~Trie() {\n // Ideally, delete all dynamically allocated nodes\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie t; // Create Trie object on the stack\n // Insert all strings in trie\n for(const string &str : products) {\n t.insert(str);\n }\n return t.getSuggestions(searchWord);\n }\n};\n", "memory": "100710" }
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 TrieNode {\npublic:\n char data;\n TrieNode *children[26];\n bool isTerminal;\n \n TrieNode(char ch) {\n data = ch;\n for(int i = 0; i < 26; i++) {\n children[i] = NULL;\n }\n isTerminal = false;\n }\n};\n\nclass Trie {\n TrieNode *root;\n \npublic:\n Trie() {\n root = new TrieNode('\\0');\n }\n\n void insertUtil(TrieNode* root, const string &word, int index) {\n if(index == word.length()) {\n root->isTerminal = true; \n return;\n }\n\n int charIndex = word[index] - 'a';\n TrieNode* child;\n \n if(root->children[charIndex] != NULL) {\n child = root->children[charIndex];\n } else {\n child = new TrieNode(word[index]);\n root->children[charIndex] = child;\n }\n\n insertUtil(child, word, index + 1);\n }\n\n void insert(const string &word) {\n insertUtil(root, word, 0);\n }\n\n void printSuggestions(TrieNode* curr, vector<string> &temp, string &prefix) {\n if(curr->isTerminal) {\n temp.push_back(prefix);\n }\n for(char ch = 'a'; ch <= 'z'; ch++) {\n TrieNode* next = curr->children[ch - 'a'];\n if(next != NULL){\n prefix.push_back(ch);\n printSuggestions(next, temp, prefix);\n prefix.pop_back();\n }\n }\n }\n\n vector<vector<string>> getSuggestions(const string &str) {\n TrieNode* prev = root;\n vector<vector<string>> output;\n string prefix = \"\";\n\n for(int i = 0; i < str.length(); i++) {\n char lastch = str[i];\n prefix.push_back(lastch);\n // kya prev se attached lastch hain koi?\n TrieNode *curr = prev->children[lastch - 'a'];\n\n if(curr == NULL) {\n // If no more matches in the Trie, just push empty vectors for remaining characters\n while (i < str.length()) {\n output.push_back({});\n i++;\n }\n break;\n }\n\n vector<string> temp;\n printSuggestions(curr, temp, prefix);\n if(temp.size() > 3) {\n temp.resize(3); // Keep only top 3 suggestions\n }\n output.push_back(temp);\n prev = curr;\n }\n return output;\n }\n\n ~Trie() {\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Trie t; \n // Insert all strings in trie\n for(string &str : products) {\n t.insert(str);\n }\n return t.getSuggestions(searchWord);\n }\n};\n", "memory": "100710" }
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": "// Trie Node\nstruct Node {\n Node* next[26];\n priority_queue<string, vector<string>> maxHeap;\n\n Node() {\n for (int i = 0; i < 26; i++) {\n next[i] = nullptr;\n }\n }\n};\n\nclass Solution {\npublic:\n // Keep track of the number suggestions currently made\n int curSuggestionCnt;\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> res;\n Node* root = new Node();\n\n // Add all the product names into the Trie\n for (string product: products) {\n Node* curNode = root;\n for (char c: product) {\n if (curNode->next[c - 'a'] == nullptr) {\n curNode->next[c - 'a'] = new Node();\n }\n curNode = curNode->next[c - 'a'];\n\n // Ensure there are always at most 3 suggestions and lexicographically minimums \n if (curNode->maxHeap.size() < 3) {\n curNode->maxHeap.push(product);\n } else {\n if (curNode->maxHeap.top() > product) {\n curNode->maxHeap.pop();\n curNode->maxHeap.push(product);\n }\n }\n }\n }\n\n // Iterate through the search word character by character\n Node* curNode = root;\n for (char c: searchWord) {\n if (curNode == nullptr) {\n res.push_back({});\n } else {\n vector<string> curSuggestions;\n curNode = curNode->next[c - 'a'];\n if (curNode == nullptr) {\n res.push_back({});\n continue;\n } else {\n while (!curNode->maxHeap.empty()) {\n curSuggestions.push_back(curNode->maxHeap.top());\n curNode->maxHeap.pop();\n }\n reverse(curSuggestions.begin(), curSuggestions.end());\n res.push_back(curSuggestions);\n }\n }\n }\n\n return res;\n }\n};", "memory": "108490" }
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 unordered_map<string, priority_queue<string>> mp;\n vector<vector<string>> ans;\n\n for(auto prod : products){\n string t = \"\";\n for( auto x : prod){\n t += x;\n mp[t].push(prod);\n while( !mp[t].empty() && mp[t].size() > 3 ){\n mp[t].pop();\n }\n }\n }\n string t = \"\";\n for( auto x : searchWord ){\n t += x;\n vector<string> v;\n while( !mp[t].empty() ){\n v.push_back(mp[t].top());\n mp[t].pop();\n }\n reverse(v.begin(), v.end());\n ans.push_back(v);\n }\n return ans;\n }\n};", "memory": "108490" }
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 TrieNode {\n vector<shared_ptr<TrieNode>> children;\n const string* word = nullptr;\n TrieNode() : children(26) {}\n};\n\nclass Solution {\n public:\n vector<vector<string>> suggestedProducts(vector<string>& products,\n string searchWord) {\n vector<vector<string>> ans;\n\n for (const string& product : products)\n insert(product);\n\n shared_ptr<TrieNode> node = root;\n\n for (const char c : searchWord) {\n if (node == nullptr || node->children[c - 'a'] == nullptr) {\n node = nullptr;\n ans.push_back({});\n continue;\n }\n node = node->children[c - 'a'];\n ans.push_back(search(node));\n }\n\n return ans;\n }\n\n private:\n shared_ptr<TrieNode> root = make_shared<TrieNode>();\n\n void insert(const string& word) {\n shared_ptr<TrieNode> node = root;\n for (const char c : word) {\n const int i = c - 'a';\n if (node->children[i] == nullptr)\n node->children[i] = make_shared<TrieNode>();\n node = node->children[i];\n }\n node->word = &word;\n }\n\n vector<string> search(shared_ptr<TrieNode> node) {\n vector<string> res;\n dfs(node, res);\n return res;\n }\n\n void dfs(shared_ptr<TrieNode> node, vector<string>& ans) {\n if (ans.size() == 3)\n return;\n if (node == nullptr)\n return;\n if (node->word != nullptr)\n ans.push_back(*node->word);\n for (shared_ptr<TrieNode> child : node->children)\n dfs(child, ans);\n }\n};", "memory": "116270" }
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": "#include <utility>\n#include <algorithm>\n#include <deque>\n/*\n1. each Trie node could contain more info: not only next char, but also the number of strings of the next char (so that we can decide how many words we need to fetch)\nIf no such info, we would: while count is < 3, go to smallest next char, fetch word; Then go back up if no more words;\n2. Design of Trie Node:\nstruct Node\n{\n char ch;\n bool isEnd;\n vecotr<pair<Node*, word_count>> next;\n int word_count; // including current char\n};\n3. On an input:\n(a) we go to Trie and follow the nodes\n(b) track how many word_count we have at the last char\n(c) if word_count >= 3, decide how to distribute:\nc.1 if isEnd, then current word is selected; 3 --;\nc.2 use a helper to collect words up to 3. \nc.3 concat input + words and return\n4. overall logic:\n(a) build a Trie\n(b) search\n*/\nclass Solution {\npublic:\n struct Node {\n char ch;\n bool isEnd;\n vector<pair<Node*, int> > next; // <Node*, wordCount> of next chars\n int wordCount;\n\n Node(char ch, bool isEnd = false)\n : ch(ch), isEnd(isEnd), next(26, make_pair(nullptr, 0)), wordCount(0) // wordCount init to 0, will update on next char insertion\n {}\n };\n\n // for debug use, print Trie in BFS way\n void printTrie(Node* root) {\n if(!root) {\n cout << \"Trie is empty\\n\";\n return;\n }\n deque<pair<Node*, string> > bfs;\n bfs.push_front(make_pair(root, \"\"));\n size_t indent = 0;\n while(bfs.size() > 0) {\n // for each level\n cout << string(indent, ' ') << \"[ \";\n size_t count = bfs.size();\n for(size_t i = 0; i < count; ++i){ // for each bfs element\n auto& last_element = bfs.back();\n auto [node, word] = last_element;\n bfs.pop_back();\n //auto [node, word] = bfs.pop_back();\n cout << word << \", \";\n for(size_t j = 0; j < 26; ++j) {\n if(node->next[j].first) { // has next char\n string next_word = word + (char)('a' + j);\n bfs.push_front(make_pair(node->next[j].first, next_word));\n }\n }\n }\n cout << \"]\\n\";\n indent += 2;\n }\n cout << endl;\n }\n\n\n Node* buildTrie(vector<string>& products) {\n Node* root = new Node(' ', false); // init\n //root->wordCount = 0; // root is special; reset wordCount to 0 at init\n for(string product : products) {\n Node* insert_ptr = root;\n for(auto ch : product) { \n insert_ptr->wordCount ++;\n size_t pos = ch - 'a';\n insert_ptr->next[pos].second ++; // wordCount ++\n if(!insert_ptr->next[pos].first) { // next char Node do not exist\n insert_ptr->next[pos].first = new Node(ch, false); // creat Node\n }\n insert_ptr = insert_ptr->next[pos].first; // move ptr to next char\n }\n // handle last char\n insert_ptr->isEnd = true;\n insert_ptr->wordCount ++;\n }\n return root;\n }\n\n void updateHelper(Node* root, string& searchWord, vector<string>& suggestion, int return_count) {\n // base case\n if(return_count <= 0) {\n return; // do nothing\n }\n int count = return_count;\n if(root->isEnd) { // use current word\n suggestion.push_back(searchWord);\n count --;\n }\n size_t pos = 0;\n while(pos < 26 && count > 0) { // backtrack\n int next_count = min(root->next[pos].second, count); // how many words we can fetch from next char\n if(next_count > 0){\n searchWord += ('a' + pos); // append next char\n updateHelper(root->next[pos].first, searchWord, suggestion, count); // recursion\n searchWord.pop_back(); // reset searchWord\n count -= next_count;\n }\n pos ++; // next iter\n }\n return;\n }\n\n // return current Node* for next search\n Node* updateSuggestion(Node* root, string& searchWord, char ch, vector<string>& suggestion) {\n //cout << \"search \" << searchWord << \", ch \" << ch << \" at root \" << root << endl;\n\n // base case\n if(!root) {\n return root; // do nothing\n }\n size_t pos = ch - 'a';\n Node* ch_root = root->next[pos].first;\n if(!ch_root) { // can not find this char, break\n //cout << \" no ch_root ptr found!\" << endl;\n auto next_ch = root->next[pos];\n //cout << \" pos: \" << pos << \", next word count \" << next_ch.second << endl;\n return ch_root;\n }\n int return_count = min(3, ch_root->wordCount); // set how many return strings we can return; max is 3\n //cout << \" return_count \" << return_count << endl;\n // use a recursive helper function to find remaining words\n updateHelper(ch_root, searchWord, suggestion, return_count);\n //cout << \" return ch_root \" << ch_root << endl;\n return ch_root;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n Node* root = buildTrie(products);\n //printTrie(root);\n // iterate on each step of searchWord\n vector<vector<string> > vec_output;\n string str_search = \"\";\n Node* search_root = root;\n for(auto ch : searchWord) {\n str_search += ch;\n vector<string> suggestion;\n // search\n search_root = updateSuggestion(search_root, str_search, ch, suggestion); // update search root to current char\n vec_output.push_back(vector<string>(suggestion)); // use copy constructor to copy suggestion\n }\n return vec_output;\n }\n};", "memory": "116270" }
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 R{public:\n R* a[26];\n vector<string> b;\n R(){\n for(int r=0;r<26;r++)\n a[r]=NULL;\n b={};\n }\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& p, string s) {\nsort(p.begin(),p.end());\n R * root=new R();\nfor(int r=0;r<p.size();r++){R* ro=root;\n for(int e=0;e<p[r].size();e++){\nif(ro->a[p[r][e]-'a']==NULL){\n ro->a[p[r][e]-'a']=new R();\n}\nro=ro->a[p[r][e]-'a'];\nro->b.push_back(p[r]);\n }\n}\nvector<vector<string>> ans;\nfor(int r=0;r<s.size();r++){\nif(root->a[s[r]-'a']==NULL)\nbreak;\n root=root->a[s[r]-'a'];\n \n if(root->b.size()<=3)\nans.push_back(root->b);\nelse{\n \n vector<string> a(root->b.begin(),root->b.begin()+3);\nans.push_back(a);\n\n}\n}\nwhile(ans.size()!=s.size())\nans.push_back({});\nreturn ans;\n }\n};", "memory": "124050" }
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 R{public:\n R* a[26];\n vector<string> b;\n R(){\n for(int r=0;r<26;r++)\n a[r]=NULL;\n b={};\n }\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& p, string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n \n\nsort(p.begin(),p.end());\n R * root=new R();\nfor(int r=0;r<p.size();r++){R* ro=root;\n for(int e=0;e<p[r].size();e++){\nif(ro->a[p[r][e]-'a']==NULL){\n ro->a[p[r][e]-'a']=new R();\n}\nro=ro->a[p[r][e]-'a'];\nro->b.push_back(p[r]);\n }\n}\nvector<vector<string>> ans;\nfor(int r=0;r<s.size();r++){\nif(root->a[s[r]-'a']==NULL)\nbreak;\n root=root->a[s[r]-'a'];\n \n if(root->b.size()<=3)\nans.push_back(root->b);\nelse{\n \n vector<string> a(root->b.begin(),root->b.begin()+3);\nans.push_back(a);\n\n}\n}\nwhile(ans.size()!=s.size())\nans.push_back({});\nreturn ans;\n }\n};", "memory": "124050" }
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 struct Node {\n Node* children[26];\n set<string> products;\n\n void add(string& s) {\n products.insert(s);\n if (products.size() > 3) products.erase(-- products.end());\n }\n };\n Node* root = new Node();\n\n void insert(string& s) {\n Node* p = root;\n for (char c: s) {\n int x = c - 'a';\n if (!p->children[x]) p->children[x] = new Node();\n p = p->children[x];\n p->add(s);\n }\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> res;\n for (auto& s: products) {\n insert(s);\n }\n\n Node* p = root;\n for (char c: searchWord) {\n int x = c - 'a';\n vector<string> prod;\n if (p) p = p->children[x];\n if (p == NULL) {\n res.push_back(prod);\n continue;\n }\n for (auto& s: p->products) {\n prod.push_back(s);\n }\n res.push_back(prod);\n }\n\n return res;\n }\n};", "memory": "131830" }
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 struct Node {\n Node* children[26];\n set<string> products;\n\n void add(string& s) {\n products.insert(s);\n if (products.size() > 3) products.erase(-- products.end());\n }\n };\n Node* root = new Node();\n\n void insert(string& s) {\n Node* p = root;\n for (char c: s) {\n int x = c - 'a';\n if (!p->children[x]) p->children[x] = new Node();\n p = p->children[x];\n p->add(s);\n }\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n vector<vector<string>> res;\n for (auto& s: products) {\n insert(s);\n }\n\n Node* p = root;\n for (char c: searchWord) {\n int x = c - 'a';\n vector<string> prod;\n if (p) p = p->children[x];\n if (p == NULL) {\n res.push_back(prod);\n continue;\n }\n for (auto& s: p->products) {\n prod.push_back(s);\n }\n res.push_back(prod);\n }\n\n return res;\n }\n};", "memory": "131830" }
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": "#define ll long long\n\nstruct node {\n node *child[26];\n ll pref;\n multiset<string> strEnd;\n node() {\n pref = 0;\n for(ll i=0; i<26; i++) {\n child[i] = nullptr;\n }\n }\n};\n\nstruct trie {\n node *root;\n trie() {\n root = new node;\n }\n \n void insert(string &s) {\n node *curr = root;\n for(ll i=0; i<s.length(); i++) {\n char ch = s[i];\n if(curr->child[ch-'a'] == nullptr) {\n curr->child[ch-'a'] = new node;\n }\n curr = curr->child[ch-'a'];\n curr->pref++;\n curr->strEnd.insert(s);\n }\n }\n \n vector<vector<string>> query(string &s) {\n node *curr = root;\n vector<vector<string>> ans;\n for(ll i=0; i<s.length(); i++) {\n char ch = s[i];\n vector<string> temp;\n if(curr->child[ch-'a'] == nullptr) {\n while(i<s.length()) {\n ans.push_back(temp);\n i++;\n }\n break;\n }\n curr = curr->child[ch-'a'];\n if(curr->strEnd.size() > 0) {\n auto it = curr->strEnd.begin();\n for(ll i=0; i<3; i++) {\n if(it != curr->strEnd.end()){\n temp.push_back(*it);\n it++;\n }\n else {\n break;\n }\n }\n }\n ans.push_back(temp);\n }\n return ans;\n }\n};\n\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n trie t;\n ll n = products.size();\n for(ll i=0; i<n; i++) {\n t.insert(products[i]);\n }\n \n return t.query(searchWord);\n }\n};\n\n// \"mouse\"\n\n// \"mobile\"\n// \"mouse\"\n// \"moneypot\"\n// \"monitor\"\n// \"mousepad\"\n \n// m = [\"mobile\",\"moneypot\",\"monitor\"]\n// mo = [\"mobile\",\"moneypot\",\"monitor\"]\n// mou = [\"mouse\",\"mousepad\"]\n// mous = [\"mouse\",\"mousepad\"]\n// mouse = [\"mouse\",\"mousepad\"]", "memory": "139610" }
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 TrieNode\n{\npublic:\n unordered_map<char, TrieNode*> children;\n set<string> words;\n TrieNode() = default;\n};\nclass Solution {\npublic:\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n TrieNode* root = new TrieNode();\n for (string product: products)\n {\n addProduct(root, product);\n }\n vector<vector<string>> ans(searchWord.size());\n for (int i = 0; i < searchWord.size(); i++)\n {\n if (root->children.find(searchWord[i]) == root->children.end())\n {\n break;\n }\n root = root->children[searchWord[i]];\n for (string word: root->words)\n {\n ans[i].push_back(word);\n if (ans[i].size() == 3)\n {\n break;\n }\n }\n }\n return ans;\n }\n\n void addProduct(TrieNode* root, string product)\n {\n for (char ch : product)\n {\n if (root->children.find(ch) == root->children.end())\n {\n root->children[ch] = new TrieNode();\n }\n root = root->children[ch];\n root->words.insert(product);\n }\n }\n};\n", "memory": "139610" }
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 TrieNode{\n TrieNode* address[26];\n vector<string> indexWord;\n};\n\nclass Solution {\npublic:\n TrieNode* root = new TrieNode();\n void insert(string word)\n {\n TrieNode* node = root;\n for(auto i : word)\n {\n int idx = i-'a';\n if(node->address[idx]==NULL){\n node->address[i-'a'] = new TrieNode();\n }\n node = node->address[i-'a'];\n node->indexWord.push_back(word); \n }\n }\n\n vector<string> matchesWith(string prefix)\n {\n TrieNode* node = root;\n for(auto i : prefix){\n if(node->address[i-'a']){\n node = node->address[i-'a'];\n }\n else return {};\n }\n return node->indexWord;\n }\n\n vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {\n \n sort(products.begin(),products.end());\n for(auto i : products) insert(i);\n\n vector<vector<string>> result;\n string prefix = \"\";\n\n for(auto i:searchWord){\n prefix += i;\n vector<string> matchWords = matchesWith(prefix);\n vector<string> matchprefWords;\n int len = 3;\n for(auto i : matchWords)\n {\n matchprefWords.push_back(i);\n if(!(--len)) break;\n }\n result.push_back(matchprefWords); \n }\n\n return result;\n }\n};", "memory": "147390" }
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 map<string, vector<string>> m;\n for (string s:products){\n for (int i=0;i<s.size();i++){\n string cur=s.substr(0,i+1);\n m[cur].push_back(s);\n }\n }\n vector<vector<string>> ans;\n for (int i=0;i<searchWord.size();i++){\n string cur=searchWord.substr(0,i+1);\n vector<string> temp=m[cur];\n sort(temp.begin(),temp.end());\n if (temp.size()>3) {\n ans.push_back({temp[0],temp[1],temp[2]});\n }\n else ans.push_back(temp);\n }\n return ans;\n\n }\n};", "memory": "147390" }
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" }