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 us...
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 ...
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 us...
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 ...
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 us...
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...
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 us...
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 ...
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 us...
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'...
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 us...
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 ...
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 us...
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 ...
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 us...
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 ...
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 us...
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 ...
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 us...
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 ...
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 us...
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 ...
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 us...
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 }\...
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 us...
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);...
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 us...
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() &&...
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 us...
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 ...
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 us...
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 ...
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 us...
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]]--...
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 us...
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};", "...
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 us...
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 let...
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 us...
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]--;...
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 us...
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 retur...
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 us...
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};", "...
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 us...
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 ...
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 us...
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 hashm...
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...
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 ...
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...
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 s...
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...
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...
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...
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 ...
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...
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...
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...
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 swa...
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...
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...
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...
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...
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...
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()%...
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...
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]);...
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...
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 ...
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...
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> shuffl...
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...
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_b...
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...
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_...
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...
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 ...
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...
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...
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...
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(aut...
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...
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 ...
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...
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 vec...
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="...
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...
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="...
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...
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="...
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 ...
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="...
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_MA...
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="...
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.f...
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="...
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(...
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="...
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 ...
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="...
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 s...
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="...
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 ...
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="...
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 e...
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="...
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 e...
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="...
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 ...
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="...
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.p...
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="...
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 ...
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="...
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 ...
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="...
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\...
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="...
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 ...
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="...
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 retu...
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="...
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 ...
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="...
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 ...
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="...
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 ...
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="...
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 fo...
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="...
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...
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="...
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 ...
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="...
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 ...
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="...
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 ...
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="...
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 stri...
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="...
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 ...
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="...
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 ...
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...
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...
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...
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(...
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...
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 rightInde...
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...
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 ...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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 ...
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...
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 inser...
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...
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 S...
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...
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...
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...
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 ...
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...
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 ...
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...
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 ...
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...
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 i...
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...
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...
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...
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<v...
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...
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, fe...
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...
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 ...
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...
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(nullp...
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...
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 ...
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...
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 ...
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...
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 }\...
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...
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 (str...
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...
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->...
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...
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].pu...
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...
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 ...