id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n unordered_set<int> s;\n int n=nums.size();\n int ans=0;\n int mask=0;\n for(int i=31;i>=0;i--){\n mask|=(1<<i);\n for(int j=0;j<n;j++){\n s.insert(mask & nums[j]);\n }\n \n int possible=ans |(1<<i);\n for(auto j:s){\n if(s.find(j^possible)!=s.end()){\n ans=possible;\n break;\n }\n }\n s.clear();\n }\n return ans;\n }\n};", "memory": "220318" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int max = 0, mask = 0;\n unordered_set<int> t;\n // search from left to right, find out for each bit is there \n // two numbers that has different value\n for (int i = 31; i >= 0; i--){\n // mask contains the bits considered so far\n mask |= (1 << i);\n t.clear();\n // store prefix of all number with right i bits discarded\n for (int n: nums){\n t.insert(mask & n);\n }\n \n // now find out if there are two prefix with different i-th bit\n // if there is, the new max should be current max with one 1 bit at i-th position, which is candidate\n // and the two prefix, say A and B, satisfies:\n // A ^ B = candidate\n // so we also have A ^ candidate = B or B ^ candidate = A\n // thus we can use this method to find out if such A and B exists in the set \n int candidate = max | (1<<i);\n for (int prefix : t){\n if (t.find(prefix ^ candidate) != t.end()){\n max = candidate;\n break;\n }\n \n }\n }\n return max;\n }\n};", "memory": "225276" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "#include <array>\n#include <vector>\n\nusing std::array, std::vector;\n\nconst int MAX_BIT = 31;\n\nstruct Node {\n array<Node*, 2> children{};\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n for (int num : nums) {\n std::cout << std::bitset<MAX_BIT + 1>(num) << \"\\n\";\n }\n\n Node* trie = new Node{};\n for (int num : nums) {\n add_num(trie, num, 0);\n }\n\n int max_xor = 0;\n for (int num : nums) {\n int alternate = find_alternate(trie, num, 0);\n if ((num ^ alternate) > max_xor) {\n max_xor = num ^ alternate;\n }\n }\n\n return max_xor;\n }\n\nprivate:\n void add_num(Node* node, int num, int pos) {\n int bit = (num >> (MAX_BIT - pos)) & 1;\n if (!node->children[bit]) {\n node->children[bit] = new Node{};\n }\n if (pos < MAX_BIT) {\n add_num(node->children[bit], num, pos + 1);\n }\n }\n\n int find_alternate(Node* node, int num, int pos) {\n int shift = MAX_BIT - pos;\n int bit = (num >> shift) & 1;\n if (node->children[1 - bit]) {\n if (pos < MAX_BIT) {\n return ((1 - bit) << shift) | find_alternate(node->children[1 - bit], num, pos + 1);\n }\n return (1 - bit) << shift;\n } else if (node->children[bit]) {\n if (pos < MAX_BIT) {\n return (bit << shift) | find_alternate(node->children[bit], num, pos + 1);\n }\n return bit << shift;\n }\n return bit << shift;\n }\n\n void print_trie(Node* node, int depth) {\n for (int bit = 0; bit <= 1; bit ++) {\n if (node->children[bit]) {\n std::cout << string(depth * 2, ' ') << bit << \"\\n\";\n print_trie(node->children[bit], depth + 1);\n }\n }\n }\n};", "memory": "225276" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\n struct TrieNode{\n int mini;\n TrieNode* next[2];\n\n TrieNode(){\n mini=INT_MAX;\n next[0]=next[1]=NULL;\n }\n };\n\n class Trie{\n TrieNode *root;\n\n public:\n Trie(){\n root=new TrieNode();\n }\n\n void insert(int a){\n TrieNode *temp=root;\n for(int i=30;i>=0;i--){\n int bit=(((1<<i)&a)!=0);\n if(temp->next[bit]==NULL) temp->next[bit]=new TrieNode();\n temp=temp->next[bit];\n temp->mini=min(temp->mini,a);\n }\n }\n\n int find(int x){\n int ans=0;\n TrieNode *temp=root;\n for(int i=30;i>=0;i--){\n int xbit=(((1<<i)&x)!=0);\n int prefbit=(!xbit);\n if(temp->next[prefbit]!=NULL){\n ans+=(1<<i);\n temp=temp->next[prefbit];\n }\n else if(temp->next[!prefbit]!=NULL){\n temp=temp->next[!prefbit];\n }\n else{\n ans=0;\n break;\n }\n }\n return ans;\n }\n };\n\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int ans=0;\n Trie tr;\n for(auto i:nums) tr.insert(i);\n for(auto i:nums) ans=max(ans,tr.find(i));\n return ans;\n }\n\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n }\n};", "memory": "230233" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Node{\npublic:\n Node* links[2];\n bool flag = 0;\n bool is_contain(int ind){\n return links[ind]!=NULL;\n }\n};\nclass Trie{\n Node* root;\npublic:\n Trie(){\n root = new Node();\n }\n void insert(int val){\n Node* tmp = root;\n for(int i=31;i>=0;i--){\n int mask = ((val>>i)&1);\n if(!tmp->is_contain(mask)){\n tmp->links[mask] = new Node();\n }\n tmp = tmp->links[mask];\n }\n tmp->flag = 1;\n }\n int max_xor(int val){\n int ans = 0;\n Node* tmp = root;\n for(int i=31;i>=0;i--){\n int mask = ((val>>i)&1);\n if(tmp->is_contain(mask^1)){\n ans+=(1<<i);\n tmp = tmp->links[mask^1];\n }\n else{\n tmp = tmp->links[mask];\n }\n if(tmp==NULL) break;\n }\n return ans;\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n Trie trie;\n for(auto &it:nums){\n trie.insert(it);\n }\n int ans = 0;\n for(auto &it:nums){\n ans = max(ans,trie.max_xor(it));\n }\n return ans;\n }\n};", "memory": "235191" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\n struct TrieNode{\n int mini;\n TrieNode* next[2];\n\n TrieNode(){\n mini=INT_MAX;\n next[0]=next[1]=NULL;\n }\n };\n\n class Trie{\n TrieNode *root;\n\n public:\n Trie(){\n root=new TrieNode();\n }\n\n void insert(int a){\n TrieNode *temp=root;\n for(int i=30;i>=0;i--){\n int bit=(((1<<i)&a)!=0);\n if(temp->next[bit]==NULL) temp->next[bit]=new TrieNode();\n temp=temp->next[bit];\n temp->mini=min(temp->mini,a);\n }\n }\n\n int find(int x){\n int ans=0;\n TrieNode *temp=root;\n for(int i=30;i>=0;i--){\n int xbit=(((1<<i)&x)!=0);\n int prefbit=(!xbit);\n if(temp->next[prefbit]!=NULL){\n ans+=(1<<i);\n temp=temp->next[prefbit];\n }\n else if(temp->next[!prefbit]!=NULL){\n temp=temp->next[!prefbit];\n }\n else{\n ans=0;\n break;\n }\n }\n return ans;\n }\n };\n\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int ans=0;\n Trie tr;\n for(auto i:nums) tr.insert(i);\n for(auto i:nums) ans=max(ans,tr.find(i));\n return ans;\n }\n\n Solution(){\n ios_base::sync_with_stdio(false);\n }\n};", "memory": "240148" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Node{\npublic:\n Node* links[2];\n bool flag = 0;\n bool is_contain(int ind){\n return links[ind]!=NULL;\n }\n};\nclass Trie{\n Node* root;\npublic:\n Trie(){\n root = new Node();\n }\n void insert(int val){\n Node* tmp = root;\n for(int i=31;i>=0;i--){\n int mask = ((val>>i)&1);\n if(!tmp->is_contain(mask)){\n tmp->links[mask] = new Node();\n }\n tmp = tmp->links[mask];\n }\n tmp->flag = 1;\n }\n int max_xor(int val){\n int ans = 0;\n Node* tmp = root;\n for(int i=31;i>=0;i--){\n int mask = ((val>>i)&1);\n if(tmp->is_contain(mask^1)){\n ans+=(1<<i);\n tmp = tmp->links[mask^1];\n }\n else{\n tmp = tmp->links[mask];\n }\n if(tmp==NULL) break;\n }\n return ans;\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n Trie trie;\n for(auto &it:nums){\n trie.insert(it);\n }\n int ans = 0;\n for(auto &it:nums){\n ans = max(ans,trie.max_xor(it));\n }\n return ans;\n }\n};", "memory": "240148" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Trie{\n public:\n Trie* child[2];\n bool is_leaf;\n\n Trie(){\n this->is_leaf=false;\n this->child[0]=NULL;\n this->child[1]=NULL;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n=nums.size();\n\n Trie* root=new Trie();\n for(int i=0;i<n;i++){\n Trie* node=root;\n for(int j=30;j>=0;j--){\n int mask=(1<<j);\n int bit=((mask&nums[i])==mask);\n if(node->child[bit]==NULL){\n node->child[bit]=new Trie();\n }\n\n node=node->child[bit];\n }\n }\n\n int max_X=0;\n for(int i=0;i<n;i++){\n int maxi=0;\n Trie* node=root;\n for(int j=30;j>=0;j--){\n int mask=(1<<j);\n int bit=!((mask&nums[i])==mask);\n if(node->child[bit]!=NULL){\n maxi+=mask;\n node=node->child[bit];\n }\n else{\n node=node->child[!bit];\n }\n }\n\n max_X=max(max_X,maxi);\n }\n\n return max_X;\n }\n};", "memory": "245106" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Trie {\npublic:\n Trie* children[2];\n int number;\n bool is_num = false;\n void insert(int x) {\n auto cur = this;\n bitset<32> str(x);\n\n for(int i = 31; i >= 0; i--) {\n if ( !cur->children[!str[i]] ) cur->children[!str[i]] = new Trie();\n cur = cur->children[!str[i]];\n }\n cur->number = x;\n cur->is_num = true; \n }\n\n int find(int x) {\n auto cur = this;\n bitset<32> str(x);\n for(int i = 31; i >= 0; i--) {\n int digit = !str[i];\n if (!cur->children[digit]) return -1;\n cur = cur->children[digit];\n }\n\n return cur->number;\n }\n\n int get_max_xor(int x) {\n auto cur = this;\n bitset<32> str(x);\n for(int i = 31; i >= 0; i--) {\n if(!str[i]) {\n cur = cur->children[0] ? cur->children[0] : cur->children[1];\n } else {\n cur = cur->children[1] ? cur->children[1] : cur->children[0];\n }\n }\n return cur->number ^ x;\n }\n \n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int max_xor = 0;\n auto trie = new Trie();\n for (int i = 0; i < nums.size(); i++) {\n trie->insert(nums[i]);\n max_xor = max(max_xor, trie->get_max_xor(nums[i]));\n }\n return max_xor;\n }\n};", "memory": "245106" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#define ff first\n#define ss second\nstruct Node{\n Node* links[2]={NULL};\n bool flag=false;\n \n void put(int c, Node* nw){\n links[c]=nw;\n return;\n }\n \n Node* get(int c){\n return links[c];\n }\n \n bool isend(){\n return this->flag;\n }\n \n void setend(){\n this->flag=true;\n return;\n }\n};\nclass Trie{\n public:\n Node* root;\n Trie(){\n root=new Node();\n }\n\n void insert(int n){\n int mask=1<<30,ct=0;\n Node* cur=root;\n while(mask){\n int f=((n&mask)>0);\n if(cur->get(f)==NULL)\n cur->put(f, new Node);\n cur=cur->get(f);\n mask/=2;\n ct++;\n }\n cur->setend();\n }\n\n int find_max(){\n Node* cur=root;\n int ans=0,mask=1<<30;\n //cout<<mask<<\" \";\n queue<pair<Node*,Node*>> q;\n while(mask){\n if(cur->get(0)&&cur->get(1))\n ans|=mask,q.push({cur->get(0),cur->get(1)});\n mask/=2;\n if(q.size())\n break;\n if(cur->get(0))\n cur=cur->get(0);\n else\n cur=cur->get(1);\n }\n\n //cout<<q.size()<<\" \"<<ans<<\" \";\n\n while(mask){\n int f=0;\n\n stack<pair<int,pair<Node*,Node*>>> s;\n while(q.size()){\n auto val=q.front();\n auto x=val.ff;\n auto y=val.ss;\n\n if(x->get(0)&&y->get(1))\n f=1,s.push({1, {x->get(0),y->get(1)}});\n\n if(x->get(1)&&y->get(0))\n f=1,s.push({1, {x->get(1),y->get(0)}});\n\n if(f==0&&x->get(0)&&y->get(0))\n s.push({0, {x->get(0),y->get(0)}});\n\n if(f==0&&x->get(1)&&y->get(1))\n s.push({0, {x->get(1),y->get(1)}});\n\n q.pop();\n }\n\n while(s.size()){\n auto val=s.top();\n s.pop();\n if(val.ff==f)\n q.push(val.ss);\n }\n ans|=(f*mask);\n mask/=2;\n }\n\n\n return ans;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& a) {\n Trie tr;\n for(auto i: a)\n tr.insert(i);\n\n return tr.find_max();\n }\n};", "memory": "250063" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#define ff first\n#define ss second\nstruct Node{\n Node* links[2]={NULL};\n bool flag=false;\n \n void put(int c, Node* nw){\n links[c]=nw;\n return;\n }\n \n Node* get(int c){\n return links[c];\n }\n \n bool isend(){\n return this->flag;\n }\n \n void setend(){\n this->flag=true;\n return;\n }\n};\nclass Trie{\n public:\n Node* root;\n Trie(){\n root=new Node();\n }\n\n void insert(int n){\n int mask=1<<30,ct=0;\n Node* cur=root;\n while(mask){\n int f=((n&mask)>0);\n if(cur->get(f)==NULL)\n cur->put(f, new Node);\n cur=cur->get(f);\n mask/=2;\n ct++;\n }\n cur->setend();\n }\n\n int find_max(){\n Node* cur=root;\n int ans=0,mask=1<<30;\n //cout<<mask<<\" \";\n queue<pair<Node*,Node*>> q;\n while(mask){\n if(cur->get(0)&&cur->get(1))\n ans|=mask,q.push({cur->get(0),cur->get(1)});\n mask/=2;\n if(q.size())\n break;\n if(cur->get(0))\n cur=cur->get(0);\n else\n cur=cur->get(1);\n }\n\n //cout<<q.size()<<\" \"<<ans<<\" \";\n\n while(mask){\n int f=0;\n\n stack<pair<int,pair<Node*,Node*>>> s;\n while(q.size()){\n auto val=q.front();\n auto x=val.ff;\n auto y=val.ss;\n\n if(x->get(0)&&y->get(1))\n f=1,s.push({1, {x->get(0),y->get(1)}});\n\n if(x->get(1)&&y->get(0))\n f=1,s.push({1, {x->get(1),y->get(0)}});\n\n if(f==0&&x->get(0)&&y->get(0))\n s.push({0, {x->get(0),y->get(0)}});\n\n if(f==0&&x->get(1)&&y->get(1))\n s.push({0, {x->get(1),y->get(1)}});\n\n q.pop();\n }\n\n while(s.size()){\n auto val=s.top();\n s.pop();\n if(val.ff==f)\n q.push(val.ss);\n }\n ans|=(f*mask);\n mask/=2;\n }\n\n\n return ans;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& a) {\n Trie tr;\n for(auto i: a)\n tr.insert(i);\n\n return tr.find_max();\n }\n};", "memory": "255021" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct Node {\n Node* links[2] = {nullptr};\n \n bool containsKey(char ch) {\n return links[ch - '0'] != nullptr;\n }\n \n void put(char ch, Node* node) {\n links[ch - '0'] = node;\n }\n \n Node* get(char ch) {\n return links[ch - '0'];\n }\n};\n\nclass Trie {\nprivate:\n Node* root; \npublic:\n Trie() {\n root = new Node();\n }\n \n void insert(int n) {\n string binary = bitset<32>(n).to_string();\n Node* node = root;\n for (char ch : binary) {\n if (!node->containsKey(ch)) {\n node->put(ch, new Node());\n }\n node = node->get(ch);\n }\n }\n \n int getMaximum(int n) {\n string binary = bitset<32>(n).to_string();\n Node*node=root;\n unsigned int val=(1<<31);\n int maxi=0;\n for(char ch:binary){\n char oB = (ch == '0') ? '1' : '0'; // opposite Bit\n if (node->containsKey(oB)) {\n maxi+=val;\n node = node->get(oB);\n } else node = node->get(ch);\n val/=2;\n }\n return maxi;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie trie;\n for (int ele : nums) {\n trie.insert(ele);\n }\n \n int maxi = 0;\n for (int ele : nums) {\n maxi = max(maxi, trie.getMaximum(ele));\n }\n \n return maxi;\n }\n};", "memory": "259978" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node\n{\n public:\n Node * links[2]={nullptr, nullptr};\n\n bool contain(int bit)\n {\n return (links[bit]!=NULL);\n\n }\n Node * get(int bit)\n {\n return links[bit];\n }\n void put(Node * node,int bit)\n {\n links[bit]=node;\n }\n};\n\nclass Solution {\npublic:\n Node * root=new Node();\n int findMaximumXOR(vector<int>& nums) {\n for(int i=0;i<nums.size();i++)\n {\n bitset<32> b(nums[i]); \n string n = b.to_string(); \n Node * node=root;\n for(int j=0;j<32;j++)\n {\n if(!node->contain(n[j]-'0'))\n {\n node->put(new Node(),n[j]-'0');\n }\n node=node->get(n[j]-'0');\n }\n \n\n\n }\n long long int ans=0;\n for(int i=0;i<nums.size();i++)\n {\n long long int m=0;\n bitset<32> b(nums[i]); \n string n = b.to_string(); \n Node * node=root;\n for(int j=0;j<32;j++)\n {\n int bit=n[j]-'0';\n if(node->contain(!bit))\n {\n m |= (1 << (31 - j));\n node=node->get(!bit);\n }\n else\n {\n node=node->get(bit);\n }\n \n }\n ans=max(ans,m);\n \n\n\n }\n return ans;\n\n }\n};", "memory": "264936" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node\n{\n public:\n Node * links[2]={nullptr, nullptr};\n\n bool contain(int bit)\n {\n return (links[bit]!=NULL);\n\n }\n Node * get(int bit)\n {\n return links[bit];\n }\n void put(Node * node,int bit)\n {\n links[bit]=node;\n }\n};\n\nclass Solution {\npublic:\n Node * root=new Node();\n int findMaximumXOR(vector<int>& nums) {\n for(int i=0;i<nums.size();i++)\n {\n bitset<32> b(nums[i]); \n string n = b.to_string(); \n Node * node=root;\n for(int j=0;j<32;j++)\n {\n if(!node->contain(n[j]-'0'))\n {\n node->put(new Node(),n[j]-'0');\n }\n node=node->get(n[j]-'0');\n }\n \n\n\n }\n long long int ans=0;\n for(int i=0;i<nums.size();i++)\n {\n long long int m=0;\n bitset<32> b(nums[i]); \n string n = b.to_string(); \n Node * node=root;\n for(int j=0;j<32;j++)\n {\n int bit=n[j]-'0';\n if(node->contain(!bit))\n {\n m =m+pow(2,31-j);\n node=node->get(!bit);\n }\n else\n {\n node=node->get(bit);\n }\n \n }\n ans=max(ans,m);\n \n\n\n }\n return ans;\n\n }\n};", "memory": "264936" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TreeNode {\n int val;\n TreeNode* children[2];\n TreeNode(int a) {\n val = a;\n for(int i = 0; i < 2; i++) children[i] = nullptr;\n }\n };\n\n TreeNode* root;\n\n int findMaximumXOR(vector<int>& nums) {\n root = new TreeNode(0);\n int c = 0;\n for(int a: nums) c = max(c, a);\n if (c == 0) return 0;\n int n = log2(c);\n TreeNode* t;\n for(int a: nums) {\n t = root;\n for(int i = n; i >= 0 ;i--) {\n int c = a / pow(2, i);\n a -= c * pow(2, i);\n if (!t->children[c]) t->children[c] = new TreeNode(c);\n t = t->children[c];\n }\n }\n queue<pair<TreeNode*, TreeNode*>> q, q1;\n q.push({root, root});\n int ans = 0;\n for(int i = n; i >= 0; i--) {\n int c = q.size();\n q1 = queue<pair<TreeNode*, TreeNode*>>();\n for(int j = 0; j < c; j++) {\n TreeNode* t1 = q.front().first;\n TreeNode* t2 = q.front().second;\n q.pop();\n if (t1->children[0] && t2->children[1]) q.push({t1->children[0], t2->children[1]});\n if (t1->children[1] && t2->children[0]) q.push({t1->children[1], t2->children[0]});\n if (t1->children[0] && t2->children[0]) q1.push({t1->children[0], t2->children[0]});\n if (t1->children[1] && t2->children[1]) q1.push({t1->children[1], t2->children[1]});\n }\n if (!q.empty()) {\n ans += pow(2, i);\n }\n else q = q1;\n } \n return ans;\n }\n};", "memory": "269893" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode {\npublic:\n TrieNode* ch[2];\n};\n\nclass Solution {\n int get_num_bits(int num) {\n int c = 0;\n for(int i=0; i<32; i++) {\n int bit = 1<<i;\n if(bit & num) c = i;\n }\n return c+1;\n }\n string convert_to_string(int num, int size) {\n string s = string(size, '0');\n for(int i=size-1; i>=0; --i) {\n if(num) s[i] = '0' + (num%2);\n else s[i] = '0';\n num /= 2;\n }\n return s;\n }\n int calculate_ans(string a, string b) {\n int calc = 0;\n for(int idx=a.size()-1; idx>=0; --idx) {\n int bit = 1 << a.size() - idx - 1;\n if(a[idx] != b[idx]) {\n calc |= bit;\n }\n }\n return calc;\n }\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int max_ele = *max_element(nums.begin(), nums.end());\n int size = get_num_bits(max_ele);\n vector<string> numbers;\n for(int i=0; i<nums.size(); ++i) {\n numbers.push_back(convert_to_string(nums[i], size));\n }\n TrieNode* root = new TrieNode();\n \n // now construct the trie and append elements one by one\n for(int idx=0; idx<numbers.size(); ++idx) {\n TrieNode* ptr = root;\n for(char c: numbers[idx]) {\n if(ptr->ch[c-'0'] == NULL) {\n ptr->ch[c-'0'] = new TrieNode();\n }\n ptr = ptr->ch[c-'0'];\n }\n }\n int result = 0;\n for(int idx=0; idx<numbers.size(); ++idx) {\n TrieNode* ptr = root;\n string t = \"\";\n for(char c: numbers[idx]) {\n // try to find the max opposite, or else switch that only\n char target = c == '0'? '1':'0';\n if(ptr->ch[target-'0'] != NULL) {\n t += target;\n ptr = ptr->ch[target-'0'];\n } else {\n t += c;\n ptr = ptr->ch[c-'0'];\n }\n }\n result = max(result, calculate_ans(numbers[idx], t));\n }\n return result;\n }\n};\n", "memory": "284766" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Trie {\n struct trieNode {\n trieNode * children[2];\n };\n\n trieNode * getNode() {\n trieNode * newNode = new trieNode();\n for (int i = 0; i < 2; i++) {\n newNode->children[i] = NULL;\n }\n return newNode;\n }\n\n trieNode * root;\n\npublic:\n Trie() {\n root = getNode();\n }\n\n void insert(string s) {\n trieNode * crawler = root;\n for (int i = 0; i < s.size(); i++) {\n char ch = s[i];\n int idx = ch-'0';\n if (crawler->children[idx] == NULL) {\n crawler->children[idx] = getNode();\n }\n crawler = crawler->children[idx];\n }\n }\n\n int find_max(string s) {\n int output = 0;\n trieNode * crawler = root;\n for (int i = 0; i < s.size(); i++) {\n int bit = !(s[i]-'0');\n if (crawler->children[bit]) {\n output = output | (1 << (31-i));\n crawler = crawler->children[bit];\n } else if (crawler->children[!bit]) {\n crawler = crawler->children[!bit];\n }\n }\n // output >> 2;\n return output;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie t;\n for (int i: nums) {\n string s = bitset<32>(i).to_string();\n cout << s << endl;\n t.insert(s);\n }\n int maxi = 0;\n for (int i: nums) {\n maxi = max(maxi, t.find_max(bitset<32>(i).to_string()));\n }\n return maxi;\n }\n};", "memory": "289723" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct Node {\n Node* children[2] = {nullptr, nullptr};\n};\n\nclass Solution {\npublic:\n Node* root = new Node();\n\n string binaryForm(int n) {\n return bitset<32>(n).to_string(); \n }\n\n int binaryToDecimal(const string &binary) {\n return stoi(binary, nullptr, 2);\n }\n \n int findMaximumXOR(vector<int>& nums) {\n // Insert numbers into Trie\n for (int num : nums) {\n Node* droot = root;\n string binary = binaryForm(num);\n for (char c : binary) {\n int bit = c - '0';\n if (droot->children[bit] == nullptr) {\n droot->children[bit] = new Node();\n }\n droot = droot->children[bit];\n }\n }\n\n // Find maximum XOR\n int maxx = 0;\n for (int num : nums) {\n string o = binaryForm(num);\n string s = o;\n Node* droot = root;\n\n // Toggle bits in `s` for potential max XOR\n for (int j = 0; j < s.length(); j++) { \n s[j] = (s[j] == '1') ? '0' : '1';\n }\n\n for (int j = 0; j < s.length(); j++) {\n int x = s[j] - '0';\n if (droot->children[x] != nullptr) {\n droot = droot->children[x];\n } else {\n s[j] = (x == 1) ? '0' : '1'; \n droot = droot->children[s[j] - '0'];\n }\n }\n\n maxx = max(maxx, binaryToDecimal(o) ^ binaryToDecimal(s));\n }\n return maxx;\n }\n};", "memory": "294681" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode {\n \n public:\n TrieNode**children;\n\n TrieNode(){\n children = new TrieNode*[2];\n children[0] = NULL;\n children[1] = NULL;\n }\n};\nclass Trie{\n\n public:\n TrieNode *root;\n\n Trie() {\n root = new TrieNode();\n }\n\n void insert(int x){\n TrieNode *temp = root;\n for(int i=31;i>=0;i--){\n // int x=(num>>i)&1;\n if(temp->children[((x>>i)&1)]==NULL) {\n temp->children[((x>>i)&1)] = new TrieNode();\n }\n temp = temp->children[((x>>i)&1)];\n }\n }\n\n int find(int x){\n int ans = 0;\n TrieNode *temp = root;\n for(int i=31;i>=0;i--){\n int value = (x>>i)&1;\n \n if(temp->children[value^1]) {\n ans = ans^(1<<i);\n temp = temp->children[value^1];\n }else{\n temp = temp->children[value];\n } \n }\n return ans;\n }\n\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n \n Trie t;\n for(int i=0;i<nums.size();i++){\n t.insert(nums[i]);\n }\n int ans = 0;\n for(int i=0;i<nums.size();i++){\n ans = max(ans,t.find(nums[i]));\n }\n return ans;\n }\n};", "memory": "299638" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node{\n public:\n Node *links[2];\n\n bool containsKey(int bit){\n return (links[bit]!=NULL);\n }\n\n void put(int bit, Node* node){\n links[bit] = new Node();\n }\n\n Node* get(int bit){\n return links[bit];\n }\n};\n\nclass Trie{\n private:\n Node* root;\n\n public:\n Trie(){\n root = new Node();\n }\n\n void insert(int num){\n Node *node = root;\n\n for(int i=31; i>=0; i--){\n int bit = (num>>i) & 1;\n if(!node->containsKey(bit)){\n node->put(bit, new Node());\n }\n node = node->get(bit);\n }\n }\n\n int getMax(int num){\n Node *node = root;\n\n int maxNum=0;\n for(int i=31; i>=0; i--){\n int bit = (num>>i) & 1;\n if(node->containsKey(1 - bit)){\n maxNum |= (1<<i);\n node = node->get(1- bit);\n }\n else{\n node = node->get(bit);\n }\n }\n return maxNum;\n }\n\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie trie;\n\n for(auto &it : nums){\n trie.insert(it);\n }\n\n int maxi=0;\n\n for(auto &it : nums){\n maxi = max(maxi, trie.getMax(it));\n }\n return maxi;\n }\n};", "memory": "304596" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": " struct Node{\n Node* links[2];\n bool containskey(int bit){\n return (links[bit]!=NULL);\n }\n Node* get(int bit){\n return links[bit];\n }\n void put(int bit,Node* node){\n links[bit]=new Node();\n }\n \n };\n class Trie{\n Node* root;\n public:\n Trie(){\n root=new Node();\n }\n void insert(int num){\n Node* node=root;\n for(int i=31;i>=0;i--){\n int bit=(num>>i)&1;\n if(!node->containskey(bit)){\n node->put(bit,new Node());\n }\n node=node->get(bit);\n }\n }\n int findmax(int num){\n int maxi=0;\n Node* node=root;\n for(int i=31;i>=0;i--){\n int bit=(num>>i)&1;\n if(node->containskey(1-bit)){\n maxi=maxi|(1<<i);\n node=node->get(1-bit);\n }\n else{\n node=node->get(bit);}\n }\n return maxi;\n }\n \n };\nclass Solution {\npublic:\n \n int findMaximumXOR(vector<int>& nums) {\n Trie* trie=new Trie();\n int ans=0;\n for(int i=0;i<nums.size();i++){\n trie->insert(nums[i]);\n ans=max(ans,trie->findmax(nums[i]));\n }\n return ans;\n }\n};", "memory": "304596" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Vertex\n {\n int next[2];\n bool output = false;\n Vertex()\n {\n fill(begin(next),end(next),-1);\n }\n };\n vector <Vertex> trie;\n map <pair<int,int>,int> mp;\n void Trie() \n {\n trie.emplace_back();\n }\n void insert(string word) {\n int v=0;\n for(int i=0;i<word.size();i++)\n {\n int value=word[i]-'0';\n if(trie[v].next[value]==-1)\n {\n trie[v].next[value]=trie.size();\n trie.emplace_back();\n // cout<<v<<\" \"<<value<<\" \"<<trie[v].next[value]<<'\\n';\n }\n v=trie[v].next[value];\n }\n trie[v].output=true;\n }\n int ct=0;\n int find_max(int v1,int v2,int j) {\n int ans=0;\n ct++;\n int vs=0;\n // if(mp.find({v1,v2})!=mp.end())\n // {\n // return mp[{v1,v2}];\n // }\n if(trie[v1].next[0]!=-1&&trie[v2].next[1]!=-1)\n {\n int v3=trie[v1].next[0];\n int v4=trie[v2].next[1];\n ans=max(ans,(1<<j)+find_max(v3,v4,j-1));\n vs=1;\n }\n if(trie[v2].next[0]!=-1&&trie[v1].next[1]!=-1)\n {\n int v3=trie[v2].next[0];\n int v4=trie[v1].next[1];\n ans=max(ans,(1<<j)+find_max(v3,v4,j-1));\n vs=1;\n }\n if(!vs)\n {\n if(trie[v1].next[0]!=-1&&trie[v2].next[0]!=-1)\n {\n int v3=trie[v1].next[0];\n int v4=trie[v2].next[0];\n ans=max(ans,find_max(v3,v4,j-1));\n }\n if(trie[v1].next[1]!=-1&&trie[v2].next[1]!=-1)\n {\n int v3=trie[v1].next[1];\n int v4=trie[v2].next[1];\n ans=max(ans,find_max(v3,v4,j-1));\n }\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n Trie();\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(int j=31;j>=0;j--)\n {\n if(nums[i]&(1<<j))\n {\n temp+='1';\n }\n else\n {\n temp+='0';\n }\n }\n insert(temp);\n }\n int v= find_max(0,0,31);\n cout<<ct<<'\\n';\n return v;\n }\n};", "memory": "309553" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Vertex\n {\n int next[2];\n bool output = false;\n Vertex()\n {\n fill(begin(next),end(next),-1);\n }\n };\n vector <Vertex> trie;\n void Trie() \n {\n trie.emplace_back();\n }\n void insert(string word) {\n int v=0;\n for(int i=0;i<word.size();i++)\n {\n int value=word[i]-'0';\n if(trie[v].next[value]==-1)\n {\n trie[v].next[value]=trie.size();\n trie.emplace_back();\n // cout<<v<<\" \"<<value<<\" \"<<trie[v].next[value]<<'\\n';\n }\n v=trie[v].next[value];\n }\n trie[v].output=true;\n }\n int find_max(int v1,int v2,int j) {\n int ans=0;\n int vs=0;\n if(trie[v1].next[0]!=-1&&trie[v2].next[1]!=-1)\n {\n int v3=trie[v1].next[0];\n int v4=trie[v2].next[1];\n ans=max(ans,(1<<j)+find_max(v3,v4,j-1));\n vs=1;\n }\n if(trie[v2].next[0]!=-1&&trie[v1].next[1]!=-1)\n {\n int v3=trie[v2].next[0];\n int v4=trie[v1].next[1];\n ans=max(ans,(1<<j)+find_max(v3,v4,j-1));\n vs=1;\n }\n if(!vs)\n {\n if(trie[v1].next[0]!=-1&&trie[v2].next[0]!=-1)\n {\n int v3=trie[v1].next[0];\n int v4=trie[v2].next[0];\n ans=max(ans,find_max(v3,v4,j-1));\n }\n if(trie[v1].next[1]!=-1&&trie[v2].next[1]!=-1)\n {\n int v3=trie[v1].next[1];\n int v4=trie[v2].next[1];\n ans=max(ans,find_max(v3,v4,j-1));\n }\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n Trie();\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(int j=31;j>=0;j--)\n {\n if(nums[i]&(1<<j))\n {\n temp+='1';\n }\n else\n {\n temp+='0';\n }\n }\n insert(temp);\n }\n int v= find_max(0,0,31);\n return v;\n }\n};", "memory": "309553" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n unordered_set<uint32_t> pr[32];\n for(int x_: nums) {\n uint32_t x = x_;\n for(int l=0; l<=31; l++) {\n pr[l].insert((x>>(31-l)) << (31-l));\n }\n }\n uint32_t ans = 0;\n for(int x_: nums) {\n uint32_t x = x_;\n uint32_t pre = 0;\n for(int i=0; i<=31; i++) {\n uint32_t bi = (uint32_t)1 << (31-i);\n uint32_t prelarger = pre | ( (x&bi) ^ bi);\n if( pr[i].contains(prelarger) ) {\n pre = prelarger;\n }\n else {\n pre = pre | (x&bi);\n }\n }\n ans = max(ans,x^pre);\n }\n return ans;\n }\n};", "memory": "314511" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\n public:\n int findMaximumXOR(vector<int>& nums) {\n const int maxNum = ranges::max(nums);\n if (maxNum == 0)\n return 0;\n const int maxBit = static_cast<int>(log2(maxNum));\n int ans = 0;\n int prefixMask = 0; // Grows like: 10000 -> 11000 -> ... -> 11111.\n\n // If ans is 11100 when i = 2, it means that before we reach the last two\n // bits, 11100 is the maximum XOR we have, and we're going to explore if we\n // can get another two 1s and put them into `ans`.\n for (int i = maxBit; i >= 0; --i) {\n prefixMask |= 1 << i;\n unordered_set<int> prefixes;\n // We only care about the left parts,\n // If i = 2, nums = {1110, 1011, 0111}\n // -> prefixes = {1100, 1000, 0100}\n for (const int num : nums)\n prefixes.insert(num & prefixMask);\n // If i = 1 and before this iteration, the ans is 10100, it means that we\n // want to grow the ans to 10100 | 1 << 1 = 10110 and we're looking for\n // XOR of two prefixes = candidate.\n const int candidate = ans | 1 << i;\n for (const int prefix : prefixes)\n if (prefixes.contains(prefix ^ candidate)) {\n ans = candidate;\n break;\n }\n }\n\n return ans;\n }\n};", "memory": "314511" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n // for checking and setting jth bit in number n\n bool isset(int n, int j){return n&(1<<j);}\n void setbit(int& n, int j){n|=(1<<j);}\n\npublic:\n int findMaximumXOR(vector<int>& nums) {\n \n int n = nums.size();\n int xorTillNow = 0;\n vector<int> mask(n,0);\n\n for(int i=31;i>=0;i--)\n {\n // check if the ith bit can be set.\n //\n unordered_map<int,bool> ma;\n bool canSet = false;\n\n for(int j=0;j<n;j++)\n {\n mask[j] = mask[j]<<1;\n if(isset(nums[j],i))\n mask[j] = mask[j] | 1;\n ma[mask[j]]=1;\n }\n\n xorTillNow = (xorTillNow << 1);\n\n for(int j=0;j<n;j++)\n {\n int req = mask[j] xor (xorTillNow | 1);\n if(ma.count(req)) canSet = true;\n if(canSet) break;\n }\n\n \n\n if(canSet)\n {\n xorTillNow = (xorTillNow) | 1;\n }\n }\n\n return xorTillNow;\n }\n};", "memory": "319468" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class TrieNode {\n public:\n TrieNode* one;\n TrieNode* zero;\n TrieNode() {\n one = NULL;\n zero = NULL;\n }\n };\n\n TrieNode *root;\n void insert(string &s) {\n TrieNode* cur = root;\n for (auto x: s) {\n if (x == '0') {\n if (cur->zero == NULL) cur->zero = new TrieNode();\n cur = cur->zero;\n }\n else {\n if (cur->one == NULL) cur->one = new TrieNode();\n cur = cur->one;\n }\n }\n }\n\n int getVal(string &s) {\n TrieNode* cur = root;\n int val = 0, i = 30;\n for (auto x: s) {\n if (x == '0') {\n if (cur->one != NULL) {\n cur = cur->one;\n val += (1 << i);\n }\n else {\n cur = cur->zero;\n }\n }\n else {\n if (cur->zero != NULL) {\n cur = cur->zero;\n val += (1 << i);\n }\n else {\n cur = cur->one;\n }\n }\n i--;\n }\n return val;\n }\n\n int findMaximumXOR(vector<int>& a) {\n root = new TrieNode();\n unordered_map<int, string> bin;\n for (auto x: a) {\n string s = \"\";\n for (int i = 30; i >= 0; i--) {\n if (x & (1 << i)) s.push_back('1');\n else s.push_back('0');\n }\n bin[x] = s;\n insert(s);\n }\n\n int ans = 0;\n\n for (auto x: a) {\n ans = max(ans, getVal(bin[x]));\n }\n\n return ans;\n }\n};", "memory": "319468" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct Node{\n Node* child[2];\n int cnt;\n vector<int> wend;\n\n Node(){\n child[0] = NULL;\n child[1] = NULL;\n \n cnt = 0; \n }\n};\nclass Trie{\npublic:\n Node* root;\n\n Trie(){\n root = new Node;\n }\n\n void insert(int n, int ln){\n Node* curr = root;\n for(int i=(ln-1); i>=0; i--){\n curr->cnt++;\n int x = (n&(1<<i)) ? 1 : 0;\n\n if(curr->child[x] == NULL){\n curr->child[x] = new Node;\n }\n\n curr=curr->child[x];\n }\n curr->wend.push_back(n);\n }\n\n int getXorMax(int x, int ln){\n Node* curr = root;\n for(int i=(ln-1); i>=0; i--){\n curr->cnt++;\n int bit = (x&(1<<i)) ? 1 : 0;\n\n if(curr->child[0] == NULL){\n curr = curr->child[1];\n }\n else if(curr->child[1] == NULL){\n curr = curr->child[0];\n }\n else{\n if(!bit){\n curr = curr->child[1];\n }\n if(bit){\n curr = curr->child[0];\n }\n }\n }\n\n return curr->wend[0];\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n=nums.size();\n\n int ln = 31;\n Trie trie;\n trie.insert(nums[0], ln);\n\n int ans = 0;\n for(int i=1; i<n; i++){\n int x = nums[i];\n int y = trie.getXorMax(x, ln);\n\n ans = max(ans, x^y);\n trie.insert(nums[i], ln);\n }\n\n return ans;\n }\n};", "memory": "324426" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct TrieNode{\n TrieNode *children[2];\n bool flag;\n \n TrieNode () {\n children[0] = nullptr;\n children[1] = nullptr;\n flag = false;\n }\n};\n\nclass Solution {\npublic:\n void dfs(TrieNode *left, TrieNode *right, int currNum) {\n if (!left || !right) return;\n maxXOR = max(maxXOR, currNum);\n if (left->flag) return;\n int newNum = (currNum << 1);\n bool diffExists = false;\n if (left->children[0] && right->children[1]) {\n dfs(left->children[0], right->children[1], newNum+1);\n diffExists = true;\n }\n if (right != left && left->children[1] && right->children[0]) {\n dfs(left->children[1], right->children[0], newNum+1);\n diffExists = true;\n }\n \n if (!diffExists) {\n if (left->children[0] && right->children[0]) {\n dfs(left->children[0], right->children[0], newNum);\n }\n if (left->children[1] && right->children[1]) {\n dfs(left->children[1], right->children[1], newNum);\n }\n }\n }\n \n int findMaximumXOR(vector<int>& nums) {\n constructTrie(nums);\n dfs(root, root, 0);\n \n return maxXOR;\n }\n \nprivate:\n TrieNode *root;\n int maxXOR = 0;\n \n int index(char c) {\n return c-'0';\n }\n \n void insert(string &s) {\n TrieNode *curr = root;\n for (char c : s) {\n if (!curr->children[index(c)]) curr->children[index(c)] = new TrieNode();\n curr = curr->children[index(c)];\n }\n curr->flag = true;\n }\n \n void constructTrie(vector<int>& nums) {\n int n = nums.size();\n vector<string> numsBinary(n);\n for(int i = 0; i < n; i++) {\n numsBinary[i] = decToBinary(nums[i]);\n }\n addPadding(numsBinary);\n root = new TrieNode();\n for (int i = 0; i < n; i++) {\n insert(numsBinary[i]);\n }\n }\n \n string decToBinary(int x) {\n string res;\n while (x > 0) {\n res += (x & 1) ? '1' : '0';\n x = x >> 1;\n }\n reverse(res.begin(), res.end());\n return res;\n }\n \n void addPadding(vector<string> &numsBinary) {\n int maxLen = 0;\n for (string &s : numsBinary) {\n maxLen = max(maxLen, (int)s.length());\n }\n \n for (string &s : numsBinary) {\n while (s.length() < maxLen) {\n s = '0' + s;\n }\n }\n }\n};", "memory": "324426" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class TrieNode {\n public:\n TrieNode* one;\n TrieNode* zero;\n TrieNode() {\n one = NULL;\n zero = NULL;\n }\n };\n\n TrieNode *root;\n void insert(string &s) {\n TrieNode* cur = root;\n for (auto x: s) {\n if (x == '0') {\n if (cur->zero == NULL) cur->zero = new TrieNode();\n cur = cur->zero;\n }\n else {\n if (cur->one == NULL) cur->one = new TrieNode();\n cur = cur->one;\n }\n }\n }\n\n int getVal(string &s) {\n TrieNode* cur = root;\n int val = 0, i = 30;\n for (auto x: s) {\n if (x == '0') {\n if (cur->one != NULL) {\n cur = cur->one;\n val += (1 << i);\n }\n else {\n cur = cur->zero;\n }\n }\n else {\n if (cur->zero != NULL) {\n cur = cur->zero;\n val += (1 << i);\n }\n else {\n cur = cur->one;\n }\n }\n i--;\n }\n return val;\n }\n\n int findMaximumXOR(vector<int>& a) {\n root = new TrieNode();\n map<int, string> bin;\n for (auto x: a) {\n string s = \"\";\n for (int i = 30; i >= 0; i--) {\n if (x & (1 << i)) s.push_back('1');\n else s.push_back('0');\n }\n bin[x] = s;\n insert(s);\n }\n\n int ans = 0;\n\n for (auto x: a) {\n ans = max(ans, getVal(bin[x]));\n }\n\n return ans;\n }\n};", "memory": "329383" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct TreeNode {\n int val;\n TreeNode* children[2];\n TreeNode(int a) {\n val = a;\n for(int i = 0; i < 2; i++) children[i] = nullptr;\n }\n };\n\n TreeNode* root;\n\n int findMaximumXOR(vector<int>& nums) {\n root = new TreeNode(0);\n int c = 0;\n for(int a: nums) c = max(c, a);\n if (c == 0) return 0;\n int n = log2(c);\n TreeNode* t;\n for(int a: nums) {\n t = root;\n for(int i = n; i >= 0 ;i--) {\n int c = a / pow(2, i);\n a -= c * pow(2, i);\n if (!t->children[c]) t->children[c] = new TreeNode(c);\n t = t->children[c];\n }\n }\n queue<vector<TreeNode*>> q, q1;\n q.push({root, root});\n int ans = 0;\n for(int i = n; i >= 0; i--) {\n int c = q.size();\n q1 = queue<vector<TreeNode*>>();\n for(int j = 0; j < c; j++) {\n TreeNode* t1 = q.front()[0];\n TreeNode* t2 = q.front()[1];\n q.pop();\n if (t1->children[0] && t2->children[1]) q.push({t1->children[0], t2->children[1]});\n if (t1->children[1] && t2->children[0]) q.push({t1->children[1], t2->children[0]});\n if (t1->children[0] && t2->children[0]) q1.push({t1->children[0], t2->children[0]});\n if (t1->children[1] && t2->children[1]) q1.push({t1->children[1], t2->children[1]});\n }\n if (!q.empty()) {\n ans += pow(2, i);\n }\n else q = q1;\n } \n return ans;\n }\n};", "memory": "329383" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\nusing ints=int;\nusing doublee=double;\nusing vi=vector<ints>;\nusing pi=pair<ints,ints>;\nusing vvi=vector<vector<ints>>;\nusing vpi=vector<pi>;\n#define endl \"\\n\"\n#define all(arr) arr.begin(),arr.end()\n\nstruct node{\n ints prefixCount,wordCount;\n node* children[2];\n\n node(ints prefixCount) {\n this->prefixCount=prefixCount;\n this->wordCount=0;\n for(ints i=0;i<2;i++) \n children[i]=nullptr;\n }\n};\n\nclass Trie{\n public:\n ints ct;\n node *root;\n\n Trie() {\n root= new node(0);\n ct=0;\n }\n\n void insert(string &s) {\n ints m=s.size();\n this->ct++;\n node *ptr=this->root;\n for(ints i=0;i<m;i++) {\n ints ch=s[i]-'0';\n if(ptr->children[ch]==nullptr) {\n node* newNode=new node(1);\n ptr->children[ch]=newNode;\n }\n else{\n ptr->children[ch]->prefixCount++;\n }\n ptr=ptr->children[ch];\n }\n ptr->wordCount++;\n }\n};\n\nclass Solution {\npublic:\n ints sol(node* root1,node* root2,ints level) {\n if(root1==nullptr) return 0;\n ints temp=0,maxx=0;\n bool ff=0;\n if(root1->children[0]) {\n if(root2->children[1]) \n ff=1,temp=(1ll<<level) + sol(root1->children[0],root2->children[1],level-1);\n else\n temp=sol(root1->children[0],root2->children[0],level-1);\n maxx=max(maxx,temp);\n }\n if(root1->children[1]) {\n if(root2->children[0]) \n temp=(1ll<<level) + sol(root1->children[1],root2->children[0],level-1);\n else if(!ff)\n temp=sol(root1->children[1],root2->children[1],level-1);\n maxx=max(maxx,temp);\n }\n return maxx;\n }\n\n ints findMaximumXOR(vi &arr) {\n ints n=arr.size();\n Trie *trie=new Trie();\n for(ints i=0;i<n;i++) {\n string t;\n for(ints j=30;j>=0;j--) {\n if((1ll<<j)&arr[i]) t+='1';\n else t+='0';\n }\n trie->insert(t);\n }\n node *ptr=trie->root;\n ints ans=0,level=30;\n while(true) {\n if(ptr->children[0] && ptr->children[1]){\n ans=(1ll<<level);\n break;\n } \n else if(ptr->children[0]) ptr=ptr->children[0];\n else if(ptr->children[1]) ptr=ptr->children[1];\n else break;\n level--;\n }\n ans+= sol(ptr->children[0],ptr->children[1],level-1);\n return ans;\n }\n};", "memory": "334341" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int max = 0, mask = 0;\n for (int i = 31; i >= 0; i--)\n {\n mask |= (1 << i);\n set<int> s;\n for (int num : nums)\n {\n s.insert(num & mask);\n }\n\n int tmp = max | (1 << i);\n for (int prefix : s)\n {\n if (s.find(tmp ^ prefix) != s.end())\n {\n max = tmp;\n break;\n }\n }\n }\n\n return max;\n }\n};", "memory": "334341" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int max_xor(vector<int>&arr, int n) {\n int maxx = 0, mask = 0;\n\n set<int> se;\n\n for (int i = 30; i >= 0; i--) {\n mask |= (1 << i);\n\n for (int i = 0; i < n; ++i) {\n se.insert(arr[i] & mask);\n }\n\n int newMaxx = maxx | (1 << i);\n\n for (int prefix : se) {\n if (se.count(newMaxx ^ prefix)) {\n maxx = newMaxx;\n break;\n }\n }\n se.clear();\n }\n\n return maxx;\n }\n int findMaximumXOR(vector<int>& nums) {\n return max_xor(nums,nums.size());\n }\n};", "memory": "339298" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n=nums.size();\n\n set<int> st;\n int max_X=0,mask=0;\n for(int i=30;i>=0;i--){\n mask=(mask|(1<<i));\n for(int j=0;j<n;j++){\n st.insert(mask&nums[j]);\n }\n\n int new_mask=(max_X|(1<<i));\n for(int prefix : st){\n if(st.find(prefix^new_mask)!=st.end()){\n max_X=new_mask;\n break;\n }\n }\n\n st.clear();\n }\n\n return max_X;\n }\n};", "memory": "339298" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "string decimal_to_binary(int n){\n bitset<32> num = n;\n string s = num.to_string();\n return s;\n}\n\nstruct Node{\n Node * links[2];\n bool contains(char ch){\n return links[ch-'0']!=NULL;\n }\n Node *get(char ch){\n return links[ch-'0'];\n }\n void put(char ch,Node * node){\n links[ch-'0'] = node;\n }\n\n};\n\nclass Trie{\n private : Node * root;\n public:\n Trie(){\n root = new Node();\n }\n\n void insert(string s){\n Node * node = root;\n for(int i=0;i<s.size();i++){\n if(!node->contains(s[i])){\n node->put(s[i],new Node());\n }\n node = node->get(s[i]);\n }\n }\n\n int getmax(string s){\n Node * node = root;\n int ans = 0;\n for(int i=0;i<s.size();i++){\n if(node->contains((1-(s[i]-'0')) + '0')){\n ans += 1<<(32-i-1);\n node = node->get((1-(s[i]-'0')) + '0');\n }\n else{\n node = node->get(s[i]);\n }\n }\n return ans;\n }\n\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n\n Trie trie;\n for(auto &it : nums){\n string s=decimal_to_binary(it);\n trie.insert(s);\n }\n int ans = 0;\n for(auto &it : nums){\n string s=decimal_to_binary(it);\n ans = max(ans,trie.getmax(s));\n }\n return ans;\n\n \n }\n};", "memory": "344256" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct trie{\n trie *one;\n trie *zero;\n\n trie()\n {\n one = NULL;\n zero = NULL;\n } \n};\n\ntrie *root;\n\nstring change(int i)\n{\n string res(32,'0');\n int j =0;\n while(i)\n {\n if(i & 1)\n {\n res[j] = 1;\n }\n j++;\n i = i >> 1;\n }\n reverse(res.begin(),res.end());\n return res;\n}\nvoid insert(string str)\n{\n trie* curr = root;\n for(auto i: str)\n {\n if(i == '0')\n {\n if(curr->zero == NULL)\n {\n curr->zero = new trie();\n }\n curr = curr->zero;\n }else{\n if(curr->one == NULL)\n {\n curr->one = new trie();\n }\n curr = curr->one;\n }\n }\n}\n \n int find(string str)\n {\n trie *curr = root;\n int res = 0;\n int po = 31;\n for(auto i: str)\n {\n if(i == '0')\n {\n if(curr->one == NULL)\n {\n if(curr->zero == NULL)\n {\n return res;\n }\n curr = curr->zero;\n }else{\n res += pow(2,po);\n curr = curr->one;\n }\n }else{\n if(curr->zero == NULL)\n {\n if(curr->one == NULL)\n {\n return res;\n }\n curr = curr->one;\n }else{\n res += pow(2,po);\n curr = curr->zero;\n }\n }\n po--;\n }\n return res;\n }\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n root = new trie();\n for(int i: nums)\n {\n string str = change(i);\n insert(str);\n }\n int res = 0;\n for(int i: nums)\n {\n string str = change(i);\n res = max(res,find(str));\n }\n return res;\n }\n};", "memory": "344256" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct TrieNode {\n bool exists;\n TrieNode *nodes[2];\n\n TrieNode() {\n exists = false;\n nodes[1] = nodes[0] = nullptr;\n }\n};\n\nvoid insert(TrieNode *root, int num) {\n vector<int> bits(32, 0);\n \n for (int i = 0; i < 32; i++) {\n int m = 1<<i;\n if (num & m)\n bits[i] = 1;\n }\n \n TrieNode *curr = root;\n for (int i = 31; i >= 0; i--) {\n int b = bits[i];\n if (!curr->nodes[b])\n curr->nodes[b] = new TrieNode();\n curr = curr->nodes[b];\n };\n\n curr->exists = true;\n}\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n TrieNode *root = new TrieNode();\n\n for (int el: nums)\n insert(root, el);\n\n int ans = 0;\n for (int el: nums) {\n // max xor\n TrieNode *curr = root;\n int comp = 0;\n for (int b = 31; b >= 0; b--) {\n int m = 1<<b;\n int d = 1;\n if (el & m)\n d = 0;\n if (curr->nodes[d]) {\n curr = curr->nodes[d];\n comp += d * m;\n } else if (curr->nodes[1 - d]) {\n comp += (1 - d) * m;\n curr = curr->nodes[1 - d];\n } else\n break;\n }\n // cout<<el<<\" \"<<comp<<\"\\n\";\n ans = max(ans, comp ^ el);\n }\n\n return ans;\n }\n};", "memory": "349213" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Node\n {\n Node* next[2];\n\n Node() {\n next[0] = NULL;\n next[1] = NULL;\n }\n };\n\n struct Node* head;\n\n void insert(string x)\n {\n struct Node* cur = head;\n for(int i=0;i<x.size();i++)\n {\n if(cur->next[x[i]-'0'] == NULL){\n cur->next[x[i]-'0'] = new Node();\n }\n\n cur = cur->next[x[i]-'0'];\n }\n }\n\n string toBinary(int number) {\n if (number == 0) return \"0\";\n\n string binary;\n while (number > 0) {\n binary.push_back((number % 2) ? '1' : '0');\n number /= 2;\n }\n return binary;\n }\n\n int find(string x)\n {\n struct Node* cur = head;\n string ans;\n for(int i=0;i<x.size();i++)\n {\n int ch = x[i]-'0';\n if(cur->next[ch] != NULL)\n {\n //if(x == \"11010\") cout << 1 << endl;\n ans.push_back('1');\n cur = cur->next[ch];\n }\n else\n {\n //if(x == \"11010\") cout << 0 << endl;\n ans.push_back('0');\n cur = cur->next[1-ch];\n }\n }\n\n int lol = 0;\n int b = 1;\n\n // cout << x << \" \" << ans << endl;\n\n //cout << ans << endl;\n\n reverse(ans.begin(), ans.end());\n\n for(int i=0;i<ans.size();i++)\n {\n lol+=b*(ans[i]-'0');\n if(i==ans.size() - 1) break;\n b=b*2;\n }\n\n return lol;\n }\n\n int findMaximumXOR(vector<int>& x) {\n int n = x.size();\n\n head = new Node();\n head->next[0] = NULL;\n head->next[1] = NULL;\n\n vector<string> ans;\n\n int maxx = 0;\n\n for(int i=0;i<n;i++)\n {\n string xx = toBinary(x[i]);\n maxx = max(maxx, (int)xx.size());\n ans.push_back(xx);\n }\n\n for(int i=0;i<n;i++)\n {\n while(ans[i].size() < maxx) ans[i].push_back('0');\n reverse(ans[i].begin(), ans[i].end());\n\n //cout << ans[i] << endl;\n\n insert(ans[i]);\n }\n\n int anss1 = 0;\n\n //cout << endl;\n\n\n for(int i=0;i<n;i++)\n {\n string xx = ans[i];\n for(int j=0;j<xx.size();j++)\n {\n if(ans[i][j] == '1') xx[j] = '0';\n else xx[j] = '1';\n }\n\n //cout << xx << endl;\n \n anss1 = max(anss1, find(xx));\n }\n\n return anss1;\n }\n};", "memory": "354171" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "std::string fromIntToBitString(uint32_t num)\n{\n std::string bits;\n for(int i=0; i<32; i++){\n bits.push_back('0' + ((num >> i) & 1));\n }\n\n std::reverse(bits.begin(), bits.end());\n return bits;\n}\n\n\nstruct BitPrefixNode {\n BitPrefixNode* zero_left;\n BitPrefixNode* one_right;\n};\n\nvoid add(BitPrefixNode* trie, const std::string& bits)\n{\n for(char bit : bits){\n if(bit == '0') {\n if(trie->zero_left == nullptr) \n trie->zero_left = new BitPrefixNode();\n trie = trie->zero_left;\n }\n else {\n if(trie->one_right == nullptr)\n trie->one_right = new BitPrefixNode();\n trie = trie->one_right;\n }\n }\n\n}\n\n\nstd::string max_XOR_for_num(BitPrefixNode* trie, const std::string& num)\n{\n std::string max_xor;\n for(char bit : num) {\n if(bit == '0') {\n if(trie->one_right != nullptr){\n max_xor.push_back('1');\n trie = trie->one_right;\n } else {\n max_xor.push_back('0');\n trie = trie->zero_left;\n }\n } else { //1\n if(trie->zero_left != nullptr) {\n max_xor.push_back('1');\n trie = trie->zero_left; \n } else {\n max_xor.push_back('0');\n trie = trie->one_right;\n }\n }\n }\n\n return max_xor;\n}\n\n\n\nclass Solution {\npublic:\n int findMaximumXOR(std::vector<int>& nums) {\n BitPrefixNode* trie = new BitPrefixNode();\n std::string max_xor(32,'0');\n for(u_int32_t num : nums){\n std::string bits_num = fromIntToBitString(num);\n add(trie, bits_num);\n \n std::string max_xor_candidate = max_XOR_for_num(trie, bits_num);\n if(max_xor_candidate > max_xor)\n max_xor = max_xor_candidate;\n }\n\n \n return std::stoi(max_xor, nullptr, 2);\n }\n};", "memory": "354171" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node{\n public:\n Node* links[2];\n Node(){\n links[0]=NULL;\n links[1]=NULL;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Node* root=new Node();\n for(auto x:nums){\n Node* curr=root;\n string s=\"\";\n for(int i=31;i>=0;i--){\n if(x&(1<<i))s+='1';\n else s+='0';\n }\n for(int i=0;i<s.size();i++){\n if(curr->links[s[i]-'0']==NULL){\n curr->links[s[i]-'0']=new Node();\n }\n curr=curr->links[s[i]-'0'];\n }\n }\n int ans=0;\n for(auto x:nums){\n Node* curr=root;\n string s=\"\";\n for(int i=31;i>=0;i--){\n if(x&(1<<i))s+='1';\n else s+='0';\n }\n int val=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='1'){\n if(curr->links[0]!=NULL){\n curr=curr->links[0];\n val|=(1<<(31-i));\n }else{\n curr=curr->links[1];\n }\n }else{\n if(curr->links[1]!=NULL){\n curr=curr->links[1];\n val|=(1<<(31-i));\n }else{\n curr=curr->links[0];\n }\n }\n }\n ans=max(ans,val);\n }\n return ans;\n }\n};", "memory": "359128" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nstruct Node {\n Node* links[2] = {NULL};\n bool flag = false;\n int cw = 0;\n int cp = 0;\n};\nclass Trie {\n\npublic:\n Node* root;\n Trie() { root = new Node; }\n\n void insert(string word) {\n Node* node = root;\n for (char ch : word) {\n if (node->links[ch - '0'] == NULL) {\n node->links[ch - '0'] = new Node;\n }\n node = node->links[ch - '0'];\n node->cp++;\n }\n node->flag = true;\n node->cw++;\n }\n bool search(string word) {\n Node* node = root;\n for (char ch : word) {\n if (node->links[ch - '0'] == NULL) {\n return false;\n }\n node = node->links[ch - '0'];\n }\n if (node->flag == true)\n return true;\n else\n return false;\n }\n bool startsWith(string prefix) {\n Node* node = root;\n for (char ch : prefix) {\n if (node->links[ch - '0'] == NULL) {\n return false;\n }\n node = node->links[ch - '0'];\n }\n return true;\n }\n\n int countWordsEqualTo(string word) {\n // Write your code here.\n Node* node = root;\n for (char ch : word) {\n if (node->links[ch - '0'] == NULL) {\n return 0;\n }\n node = node->links[ch - '0'];\n }\n if (node->flag == true)\n return node->cw;\n else\n return 0;\n }\n int countWordsStartingWith(string word) {\n // Write your code here.\n Node* node = root;\n for (char ch : word) {\n if (node->links[ch - '0'] == NULL) {\n return 0;\n }\n node = node->links[ch - '0'];\n }\n return node->cp;\n }\n\n void erase(string word) {\n // Write your code here.\n Node* node = root;\n for (char ch : word) {\n if (node->links[ch - '0'] == NULL) {\n cout << \"Word not found\";\n return;\n }\n node = node->links[ch - '0'];\n node->cp--;\n }\n node->cw--;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie t;\n int n = nums.size();\n for (int i = 0; i < n; i++) {\n bitset<32> binary(nums[i]);\n t.insert(binary.to_string());\n }\n int ans = INT_MIN;\n for (int i = 0; i < n; i++) {\n string maxi(32, '0');\n bitset<32> binary(nums[i]);\n string str = binary.to_string();\n Node* node = t.root;\n for (int j = 0; j < 32; j++) {\n if (str[j] == '1') {\n if (node->links[0]) {\n maxi[j] = '1';\n node = node->links[0];\n } else {\n maxi[j] = '0';\n node = node->links[1];\n }\n } else // str[i]=='0'\n {\n if (node->links[1]) {\n maxi[j] = '1';\n node = node->links[1];\n } else {\n maxi[j] = '0';\n node = node->links[0];\n }\n }\n }\n bitset<32> bitset(maxi);\n int deci = bitset.to_ulong();\n ans = max(ans, deci);\n }\n return ans;\n }\n};", "memory": "359128" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node {\n vector<Node*> link;\npublic:\n Node() {\n link = vector<Node*>(2, nullptr);\n }\n\n bool containKey(int bit) {\n return link[bit] != nullptr;\n }\n\n void putKey(int bit, Node* node) {\n link[bit] = node;\n }\n\n Node* getKey(int bit) {\n return link[bit];\n }\n};\n\nclass Trie {\n Node* root;\npublic:\n Trie() {\n root = new Node();\n }\n\n void insert(int num) {\n Node* node = root;\n for (int i = 31; i >= 0; i--) {\n int bit = (num >> i) & 1;\n if (!node->containKey(bit)) {\n node->putKey(bit, new Node());\n }\n node = node->getKey(bit);\n }\n }\n\n int findMaxXOR(int x) {\n Node* node = root;\n int maxi = 0;\n for (int i = 31; i >= 0; i--) {\n int bit = (x >> i) & 1;\n if (node->containKey(1 - bit)) {\n maxi = (maxi << 1) | 1;\n node = node->getKey(1 - bit);\n } else {\n maxi = (maxi << 1);\n node = node->getKey(bit);\n }\n }\n return maxi;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n ios::sync_with_stdio(false);\ncin.tie(nullptr);\ncout.tie(nullptr);\n Trie trie;\n for (auto elem : nums) {\n trie.insert(elem);\n }\n\n int maxi = 0;\n for (auto elem : nums) {\n int temp = trie.findMaxXOR(elem);\n maxi = max(maxi, temp);\n }\n\n return maxi;\n }\n};\n", "memory": "364086" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Trie{\n vector<Trie*>val;\n public:\n Trie(){\n val.resize(2,NULL);\n }\n void insert(int num, Trie *root){\n Trie*temp = root;\n for(int i=0;i<31;++i){\n bool rem;\n if((1<<(30-i)) & num)rem = 1;\n else rem = 0;\n if(temp->val[rem])temp = temp->val[rem];\n else{\n temp->val[rem] = new Trie();\n temp = temp->val[rem];\n }\n }\n }\n int solve(Trie*root, int num){\n Trie*temp = root;\n int ans = 0;\n for(int i=0;i<31;++i){\n bool rem;\n if((1<<(30-i)) & num)rem = 0;\n else rem = 1;\n // cout<<rem<<\" \";\n \n if(temp->val[rem]){\n ans = ans ^ (rem<<(30-i));\n temp = temp->val[rem];\n }\n else{\n ans = ans ^ ((!rem)<<(30-i));\n temp = temp->val[!rem];\n }\n }\n // cout<<endl;\n return ans;\n }\n};\n\nclass Solution {\npublic:\nSolution(){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n }\n int findMaximumXOR(vector<int>& nums) {\n Trie*ans = new Trie();\n int ret = 0;\n for(int i=0;i<nums.size();++i){\n ans->insert(nums[i], ans);\n ret = max(ret, nums[i] ^ (ans->solve(ans, nums[i])));\n }\n return ret;\n }\n};", "memory": "364086" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node {\npublic:\n vector<Node*> v;\n Node() : v(2, nullptr) {}\n};\n\nclass Trie {\n Node* root;\npublic:\n Trie() {\n root = new Node();\n }\n\n void insert(int num) {\n Node* curr = root;\n for ( int i = 31; i>=0; i--) {\n int temp = 1 << i;\n int pos = num & temp;\n pos = pos >> i;\n if (curr->v[pos] == nullptr) {\n curr->v[pos] = new Node();\n }\n curr = curr->v[pos];\n }\n }\n\n int max_xor(int num) {\n int res = 0;\n Node* curr = root;\n for (int i = 31; i>=0; i--) {\n int temp = 1 << i;\n int bit = num & temp;\n bit = bit >> i;\n int desired = 1 - bit;\n if (curr->v[desired]) {\n res += (1 << i);\n curr = curr->v[desired];\n } else {\n curr = curr->v[bit];\n }\n }\n\n return res;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie t;\n for (auto &i: nums) {\n t.insert(i);\n }\n\n int res = 0;\n for (auto &i: nums) {\n res = max(res, t.max_xor(i));\n }\n \n return res;\n }\n};", "memory": "369043" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": " struct Node{\n Node* link[2];\n\n bool refExist(char ch){\n return link[ch-'0']!=nullptr;\n }\n\n void putNode(char ch){\n link[ch-'0'] = new Node();\n }\n\n Node* getNode(char ch){\n return link[ch-'0'];\n }\n };\n\n\n class Trie{\n Node* root;\n\n public:\n Trie(){\n root = new Node();\n }\n\n string intToBinString(int num){\n bitset<32> bs(num);\n string bin = bs.to_string();\n return bin;\n }\n int binStringtoInt(string bin){\n // return stoi(bin, nullptr, 2);\n // OR \n bitset<32> bs(bin);\n int num = bs.to_ulong();\n return num; \n\n }\n\n char findCompliment(char ch){\n return (!(ch-'0')) + '0';\n //OR\n // return ((ch-'0')^1) + '0';\n }\n\n void insertNum(int num){\n string bin = intToBinString(num);\n Node* cur=root;\n for(auto &chNum: bin){\n if(!cur->refExist(chNum))\n cur->putNode(chNum);\n \n cur = cur->getNode(chNum);\n }\n }\n\n //return other num present in the trie such that num^otherNum will be max\n int getOtherNum(int num){\n string bin=intToBinString(num);\n Node *cur=root; \n string otherNum=\"\";\n for(auto &chNum: bin){\n char compli = findCompliment(chNum);\n if(cur->refExist(compli)){\n otherNum.append(1, compli);\n cur = cur->getNode(compli);\n }\n else{\n otherNum.append(1, chNum);\n cur = cur->getNode(chNum);\n }\n }\n\n int ans = binStringtoInt(otherNum);\n return ans;\n }\n };\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie *t= new Trie();\n //insert all nums into trie in the form of binary string\n for(auto &num: nums) \n t->insertNum(num);\n\n int maxXor=0;\n for(auto &num: nums){\n int num2 = t->getOtherNum(num);\n maxXor = max(maxXor, num^num2);\n }\n return maxXor;\n }\n};", "memory": "388873" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct Node{\n Node *link[2];\n\n bool containKey(char ch){\n return link[ch-'0']!=NULL;\n }\n\n void put(char ch,Node *node){\n link[ch-'0']=node;\n }\n\n Node *get(char ch){\n return link[ch-'0'];\n }\n\n};\n\nclass Trie{\n\n public:\n\n Node *root;\n\n Trie(){\n root=new Node();\n }\n\n void insert(string &word){\n Node *node=root;\n for(int i=0;i<word.size();i++){\n if(!node->containKey(word[i])){\n node->put(word[i],new Node());\n }\n node=node->get(word[i]);\n }\n }\n\n int maxm(string &word){\n Node *node=root;\n int res=0;\n for(int i=0;i<32;i++){\n if(node->containKey('0'+'0'+1-word[i])){\n res=res|(1<<(31-i));\n node=node->get('0'+'0'+1-word[i]);\n }\n else{\n node=node->get(word[i]);\n }\n }\n return res;\n }\n};\n\nstring f(int x){\n string s=\"\";\n while(x>0){\n if(x%2==0){\n s.push_back('0');\n\n }\n else s.push_back('1');\n x/=2;\n }\n\n while(s.size()<32){\n s.push_back('0');\n }\n\n reverse(s.begin(),s.end());\n\n return s;\n}\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& v) {\n Trie trie;\n vector<string>x;\n for(auto i:v){\n string z=f(i);\n x.push_back(z);\n trie.insert(z);\n }\n int res=0;\n for(auto i:x){\n res=max(res,trie.maxm(i));\n }\n return res;\n }\n};", "memory": "393831" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Node{\n public:\n Node* links[2];\n};\nclass Trie{\n public:\n Node* root;\n Trie(){\n root = new Node();\n }\n void insert(string word){\n int n = word.length();\n Node * curr = root;\n for(int i=0;i<n;i++){\n if(curr->links[word[i]-'0']==NULL){\n curr->links[word[i]-'0'] = new Node();\n }\n curr = curr->links[word[i]-'0'];\n }\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int n = nums.size();\n Trie t;\n for(int i=0;i<n;i++){\n int k = nums[i];\n string s;\n while(k){\n s+=(to_string(k%2));\n k= k>>1;\n }\n while(s.length()<32){\n s+='0';\n }\n reverse(s.begin(),s.end());\n t.insert(s);\n }\n int mx =0;\n for(int i=0;i<n;i++){\n int k = nums[i];\n string s;\n while(k){\n s+=(to_string(k%2));\n k/=2;\n }\n while(s.length()<32){\n s+='0';\n }\n reverse(s.begin(),s.end());\n int val=0;\n Node *curr = t.root;\n //cout<<s<<endl;\n for(int j=0;j<32;j++){\n int bit = s[j] - '0';\n if (curr->links[1 - bit]) {\n val = (val << 1) | 1;\n curr = curr->links[1 - bit];\n } else {\n val = val << 1;\n curr = curr->links[bit];\n }\n }\n mx = max(mx,val);\n }\n return mx;\n }\n};", "memory": "393831" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class TrieNode {\n public:\n TrieNode* child[2];\n TrieNode() {\n child[0] = nullptr;\n child[1] = nullptr;\n }\n };\n\n class Trie {\n public:\n TrieNode* root;\n Trie() {\n root = new TrieNode();\n }\n\n void insert(vector<int>& arr) {\n TrieNode* node = root;\n for (int i = 0; i < arr.size(); i++) {\n int bit = arr[i];\n if (!node->child[bit]) {\n node->child[bit] = new TrieNode();\n }\n node = node->child[bit];\n }\n }\n\n void search(vector<int>& arr, int& ans) {\n TrieNode* node = root;\n for (int i = 0; i < arr.size(); i++) {\n int bit = arr[i];\n if (node->child[!bit]) { // Opposite bit exists\n ans = (ans << 1) | 1;\n node = node->child[!bit];\n } else { // Follow the same bit\n ans = (ans << 1);\n node = node->child[bit];\n }\n }\n }\n };\n\n vector<int> dectobin(int num) {\n vector<int> ans(31);\n for (int i = 30; i >= 0; i--) {\n ans[30 - i] = (num >> i) & 1;\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n Trie* t = new Trie();\n\n // Insert all numbers into the Trie in binary form\n for (int i = 0; i < nums.size(); i++) {\n vector<int> temp = dectobin(nums[i]);\n t->insert(temp);\n }\n\n int maxXor = 0;\n\n // Search for the maximum XOR\n for (int i = 0; i < nums.size(); i++) {\n vector<int> temp = dectobin(nums[i]);\n int currentXor = 0;\n t->search(temp, currentXor);\n maxXor = max(maxXor, currentXor);\n }\n\n return maxXor;\n }\n};\n", "memory": "398788" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode{\n public:\n TrieNode* child[2];\n TrieNode() {\n child[0] = nullptr;\n child[1] = nullptr;\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n TrieNode *root = new TrieNode;\n TrieNode* curr = root;\n for(int i = 0; i<nums.size(); i++) {\n insert(nums[i], curr);\n }\n int ans = 0;\n curr = root;\n for(int i = 0; i<nums.size(); i++) {\n findMax(nums[i], curr, ans);\n }\n return ans;\n }\nprivate:\n void insert(int x, TrieNode* root) {\n vector<int> bn(32, 0);\n for(int i = 0; i<32; i++) {\n bn[i] = x%2;\n x/=2;\n }\n reverse(bn.begin(), bn.end());\n for(int i = 0; i<32; i++) {\n if(!root->child[bn[i]]) {\n root->child[bn[i]] = new TrieNode();\n }\n root = root->child[bn[i]];\n }\n }\n void findMax(int x, TrieNode* root, int& ans) {\n int res = 0;\n vector<int> bn(32, 0);\n for(int i = 0; i<32; i++) {\n bn[i] = x%2;\n x/=2;\n }\n reverse(bn.begin(), bn.end());\n for(int i = 0; i<32; i++) {\n if(!root->child[!bn[i]]) {\n root = root->child[bn[i]];\n }\n else {\n res += (1<<(31-i));\n root = root->child[!bn[i]];\n }\n }\n ans = max(res, ans);\n }\n};", "memory": "403746" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct TrieNode {\n TrieNode* child[2];\n bool endFlag;\n char data;\n\n TrieNode(char ch){\n data = ch;\n for(int i=0; i<2; i++){\n child[i] = NULL;\n }\n endFlag = false;\n }\n};\n\nclass Solution {\nprivate:\n TrieNode* root = new TrieNode('0');\npublic:\n void insert(string word) {\n int index = word[0] - '0';\n int i = 0;\n TrieNode* node = root;\n while(i < word.length()){\n int index = word[i] - '0';\n if(node -> child[index] == NULL){\n TrieNode* newNode = new TrieNode(word[i]);\n node -> child[index] = newNode;\n }\n node = node -> child[index];\n if(i == word.length()-1) node -> endFlag = true;\n i++;\n }\n }\n\n int search(string word) {\n int index = word[0] - '0';\n int toFind = index;\n int i = 0;\n int count = 0;\n TrieNode* node = root;\n while(i < word.length()){\n index = word[i] - '0';\n toFind = abs(index-1);\n if(node -> child[toFind] != NULL){\n count = (count << 1) | 1;\n node = node -> child[toFind];\n // }else if(node -> child[index] != NULL){\n // count = (count << 1) | 0;\n // node = node -> child[index];\n }else{\n count = (count << 1) | 0;\n node = node -> child[index];\n }\n i++;\n }\n return count;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n string word = \"\";\n for(auto &n : nums){\n bitset<32> bits(n);\n word = bits.to_string();\n insert(word);\n }\n int maxDepth = 0;\n for(auto &n : nums){\n bitset<32> bits(n);\n word = bits.to_string();\n maxDepth = max(maxDepth, search(word));\n }\n return maxDepth;\n }\n};", "memory": "403746" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode{\n public:\n bool isTerminal;\n char ch;\n TrieNode *children[2];\n\n TrieNode(char ch){\n this->ch=ch;\n isTerminal = false;\n for(int i=0;i<2;i++){\n children[i]=NULL;\n }\n }\n};\n\nclass Solution {\npublic:\n\n string getbinaryString(int n){\n string s = bitset<32>(n).to_string();\n return s;\n }\n int findMaximumXOR(vector<int>& nums) {\n vector<string>bsa;\n for(int i=0;i<nums.size();i++){\n bsa.push_back(getbinaryString(nums[i]));\n }\n\n TrieNode *root = new TrieNode('\\0');\n\n for(auto bs:bsa){\n TrieNode *currNode = root;\n for(auto c:bs){\n if(currNode->children[c-'0']==NULL){\n currNode->children[c-'0'] = new TrieNode(c);\n }\n currNode = currNode->children[c-'0'];\n }\n currNode->isTerminal=true;\n }\n\n int maxXor=0;\n for(auto bs:bsa){\n int res=0;\n int mul = 31;\n TrieNode * currNode = root;\n for(auto c:bs){\n auto k = (c=='0')? '1':'0';\n if(currNode->children[k-'0']!=NULL){\n currNode = currNode->children[k-'0'];\n res = res+pow(2,mul);\n }\n else{\n currNode = currNode->children[!(k-'0')];\n }\n mul--;\n }\n maxXor=max(maxXor,res);\n }\n return maxXor;\n }\n};", "memory": "408703" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\nstruct TrieNode{\n TrieNode* childNode[2];\n int nodeData;\n TrieNode(){\n nodeData=0;\n for(int i=0; i<2; i++){\n childNode[i]=NULL;\n }\n }\n};\n\nvoid insert(TrieNode* root, string s, int no){\n\n TrieNode* currentNode=root;\n for(int i=0; i<(int)s.size(); i++){\n int idx=(s[i]=='0' ? 0 : 1);\n if(currentNode->childNode[idx]==NULL){\n TrieNode* newNode=new TrieNode();\n currentNode->childNode[idx]=newNode;\n }\n currentNode=currentNode->childNode[idx];\n }\n\n currentNode->nodeData=no;\n}\n\nint findvalue(TrieNode* root, string s){\n\n TrieNode* currentNode=root;\n for(int i=0; i<(int)s.size(); i++){\n int idx=(s[i]=='0' ? 1 : 0);\n if(currentNode->childNode[idx]){\n currentNode=currentNode->childNode[idx];\n }\n else{\n currentNode=currentNode->childNode[!idx];\n }\n }\n\n return currentNode->nodeData;\n}\n\nstring f(int x){\n string s;\n while(x>0){\n if(x%2==1){\n s.push_back('1');\n }\n else{\n s.push_back('0');\n }\n x/=2;\n }\n reverse(s.begin(),s.end());\n return s;\n}\npublic:\n int findMaximumXOR(vector<int>&v) {\n TrieNode* root=new TrieNode();\n int sz=0;\n for(int i=0; i<(int)v.size(); i++){\n string s=f(v[i]);\n sz=max(sz,(int)s.size());\n }\n\n vector<pair<string,int>>a;\n for(int i=0; i<(int)v.size(); i++){\n string k;\n string s=f(v[i]);\n int x=(sz-(int)s.size());\n for(int i=0; i<x; i++){\n k.push_back('0');\n }\n for(int i=0; i<(int)s.size(); i++){\n k.push_back(s[i]);\n }\n a.push_back({k,v[i]});\n }\n\n for(int i=0; i<(int)v.size(); i++){\n insert(root, a[i].first, a[i].second);\n }\n\n int mx=0;\n for(int i=0; i<(int)v.size(); i++){\n int p=findvalue(root, a[i].first);\n mx=max(mx,v[i]^p);\n }\n return mx;\n }\n};", "memory": "408703" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode{\npublic:\n struct Trie{\n Trie* next[2] = {NULL};\n bool isEnd = false;\n };\n\n Trie* root;\n\n TrieNode(){\n root = new Trie();\n }\n\n void insert(string word){\n Trie* temp = root;\n\n for(auto &c : word){\n int i = c - '0';\n \n if(!temp->next[i]){\n temp->next[i] = new Trie();\n }\n\n temp = temp->next[i];\n }\n\n temp->isEnd = true;\n }\n\n int calcXor(string word){\n string curr=\"\";\n\n Trie* temp = root;\n\n for(auto &c : word){\n int i = c-'0';\n\n // if(temp->next[1-i]) curr.push_back('1');\n // else curr.push_back('0');\n //temp = temp->next[i];\n\n if (temp->next[1 - i]) { // Try to move to the opposite bit\n curr.push_back('1');\n temp = temp->next[1 - i];\n } else { // Move to the current bit\n curr.push_back('0');\n temp = temp->next[i];\n }\n }\n\n return stoi(curr, nullptr, 2);\n }\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n TrieNode* root = new TrieNode();\n\n for(auto &it : nums){\n bitset<32> binary(it);\n root->insert(binary.to_string());\n }\n\n int res=0;\n\n for(auto &it : nums){\n bitset<32> binary(it);\n int curr = root->calcXor(binary.to_string());\n\n res = max(res,curr);\n }\n\n return res;\n }\n};", "memory": "413661" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class TrieNode {\npublic:\n int val;\n TrieNode* child[2];\n bool isTerminal;\n TrieNode(int val) {\n this->val = val;\n child[0] = child[1] = NULL;\n isTerminal = false;\n }\n};\nclass Trie {\npublic:\n TrieNode* root;\n Trie() { root = new TrieNode(-1); }\n void insert(string s) {\n TrieNode* t = root;\n for (int i = 0; i < s.length(); i++) {\n if (t->child[s[i] - '0'] != NULL) {\n t = t->child[s[i] - '0'];\n } else {\n t->child[s[i] - '0'] = new TrieNode(s[i] - '0');\n t = t->child[s[i] - '0'];\n }\n }\n t->isTerminal = true;\n }\n int getmax(string s) {\n TrieNode* t = root;\n int ans = 0, curr = 31;\n for (int i = 0; i < s.length(); i++) {\n int f = 0;\n if (s[i] == '0') {\n f = 1;\n }\n if (t->child[f] != NULL) {\n t = t->child[f];\n ans = (ans | (1 << curr));\n } else if (t->child[1 - f] != NULL) {\n t = t->child[1 - f];\n } else {\n break;\n }\n curr--;\n }\n return ans;\n }\n};\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie* t = new Trie();\n int ans =0 ;\n for(int j = 0;j<nums.size();j++){\n string s = \"\";\n for(int i = 31;i>=0;i--){\n if(((1LL<<i)&nums[j])){\n s += '1';\n }else{\n s += '0';\n }\n }\n t->insert(s);\n ans = max(ans,t->getmax(s));\n }\n return ans;\n }\n};", "memory": "413661" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "string num_to_string(int num){\n string s;\n while(num){\n int bit=(num&1);\n if(bit==0) s.push_back('0');\n else s.push_back('1');\n num>>=1;\n }\n while(s.size()<31) s.push_back('0');\n reverse(s.begin(),s.end());\n return s;\n}\nstruct trie_node{\n trie_node* child[2];\n bool word_end;\n trie_node(){\n word_end=false;\n for(int i=0;i<2;i++)\n child[i]=nullptr;\n }\n};\nvoid insert_key(trie_node* root, int num){\n trie_node* curr=root;\n string s=num_to_string(num);\n for(char c:s){\n if(curr->child[c-'0']==nullptr){\n trie_node* new_node=new trie_node();\n curr->child[c-'0']=new_node;\n }\n curr=curr->child[c-'0'];\n }\n curr->word_end=true;\n}\nint max_xor(trie_node* root, int num){\n trie_node* curr=root;\n string s=num_to_string(num);\n for(int i=0;i<s.size();i++){\n if(s[i]=='1') s[i]='0';\n else s[i]='1';\n }\n int best=0,curr_num=0,i=30;\n for(char c:s){\n int bit=c-'0';\n if(curr->child[bit]==nullptr) bit=1-bit;\n if(curr->child[bit]==nullptr) break;\n if(bit==1) curr_num|=(1<<i);\n i--; curr=curr->child[bit];\n if(curr->word_end) best=max(best,curr_num^num);\n }\n return best;\n}\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n trie_node* root=new trie_node();\n for(auto c:nums) insert_key(root,c);\n int ans=0;\n for(int i:nums){\n ans=max(ans,max_xor(root,i));\n }\n return ans;\n }\n};", "memory": "418618" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class Trie{\n public:\n int value;\n vector<Trie*> children;\n bool isTerminal;\n\n Trie(int v, bool it = false):children(2, nullptr){\n value = v; \n isTerminal = it; \n }\n\n void insertInTrie(bitset<32>& br, int i = 31){\n if(i < 0){\n isTerminal = true;\n return;\n }\n\n if(children[br[i]] != nullptr){\n (*children[br[i]]).insertInTrie(br, i-1);\n }\n else{\n Trie* newTrie = new Trie(br[i]);\n children[br[i]] = newTrie;\n (*children[br[i]]).insertInTrie(br, i-1);\n }\n }\n\n void getMaxFromTrie(bitset<32>& br, int& b, int bri = 31){\n if(bri < 0){\n return;\n }\n\n int oppBit = !(br[bri]);\n if(children[oppBit] != nullptr){\n // br2[bri] = oppBit;\n b += (1 << bri);\n (*children[oppBit]).getMaxFromTrie(br, b, bri-1);\n //todo\n }\n else{\n // br2[bri] = !oppBit;\n (*children[!oppBit]).getMaxFromTrie(br, b, bri-1);\n }\n }\n };\n\n int findMaximumXOR(vector<int>& nums) {\n Trie* t = new Trie(-1);\n int N = nums.size();\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n t->insertInTrie(br);\n }\n\n int maxi = INT_MIN;\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n bitset<32> br2;\n int b = 0;\n t->getMaxFromTrie(br, b);\n maxi = max(maxi, b);\n }\n\n return maxi;\n }\n};", "memory": "423576" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class Trie{\n public:\n int value;\n vector<Trie*> children;\n bool isTerminal;\n\n Trie(int v, bool it = false):children(2, nullptr){\n value = v; \n isTerminal = it; \n }\n\n void insertInTrie(bitset<32>& br){\n // if(i < 0){\n // isTerminal = true;\n // return;\n // }\n\n // if(children[br[i]] != nullptr){\n // (*children[br[i]]).insertInTrie(br, i-1);\n // }\n // else{\n // Trie* newTrie = new Trie(br[i]);\n // children[br[i]] = newTrie;\n // (*children[br[i]]).insertInTrie(br, i-1);\n // }\n\n Trie* root = this;\n for(int i = 31; i >= 0; i--){\n int bit = br[i];\n if(root->children[bit] != nullptr){\n root = root->children[bit];\n }\n else{\n root->children[bit] = new Trie(bit);\n root = root->children[bit];\n }\n }\n }\n\n void getMaxFromTrie(bitset<32>& br, int& b){\n // if(bri < 0){\n // return;\n // }\n\n \n Trie* root = this;\n for(int i = 31; i >= 0; i--){\n int oppBit = !(br[i]);\n if(root->children[oppBit] != nullptr){\n b += (1 << i);\n root = root->children[oppBit];\n }\n else{\n root = root->children[!oppBit];\n }\n }\n // if(children[oppBit] != nullptr){\n // // br2[bri] = oppBit;\n // b += (1 << bri);\n // (*children[oppBit]).getMaxFromTrie(br, b, bri-1);\n // //todo\n // }\n // else{\n // // br2[bri] = !oppBit;\n // (*children[!oppBit]).getMaxFromTrie(br, b, bri-1);\n // }\n }\n };\n\n // bitset<32> getBr(int num){\n // bitset<32> br(32, 0);\n // for(int j = 0; j < 32; j++){\n // int bit = num&1;\n // br[31 - j] = bit;\n // num = num>>1;\n // }\n // return br;\n // }\n\n // int getNum(bitset<32> br){\n // int num = 0;\n // for(int j = 0; j < 32; j++){\n // int exp = pow(2.0, 1.0*(31-j)) + 0.1;\n // num += (br[j] * exp);\n // }\n // return num;\n // }\n\n\n int findMaximumXOR(vector<int>& nums) {\n Trie* t = new Trie(-1);\n int N = nums.size();\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n t->insertInTrie(br);\n }\n\n int maxi = INT_MIN;\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n bitset<32> br2;\n int b = 0;\n t->getMaxFromTrie(br, b);\n maxi = max(maxi, b);\n }\n\n return maxi;\n }\n};", "memory": "423576" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n class Trie{\n public:\n int value;\n vector<Trie*> children;\n bool isTerminal;\n\n Trie(int v, bool it = false):children(2, nullptr){\n value = v; \n isTerminal = it; \n }\n\n void insertInTrie(bitset<32>& br, int i = 31){\n if(i < 0){\n isTerminal = true;\n return;\n }\n\n if(children[br[i]] != nullptr){\n (*children[br[i]]).insertInTrie(br, i-1);\n }\n else{\n Trie* newTrie = new Trie(br[i]);\n children[br[i]] = newTrie;\n (*children[br[i]]).insertInTrie(br, i-1);\n }\n }\n\n void getMaxFromTrie(bitset<32>& br, bitset<32>& br2, int bri = 31){\n if(bri < 0){\n return;\n }\n\n int oppBit = !(br[bri]);\n if(children[oppBit] != nullptr){\n br2[bri] = oppBit;\n (*children[oppBit]).getMaxFromTrie(br, br2, bri-1);\n //todo\n }\n else{\n br2[bri] = !oppBit;\n (*children[!oppBit]).getMaxFromTrie(br, br2, bri-1);\n }\n }\n };\n\n // bitset<32> getBr(int num){\n // bitset<32> br(32, 0);\n // for(int j = 0; j < 32; j++){\n // int bit = num&1;\n // br[31 - j] = bit;\n // num = num>>1;\n // }\n // return br;\n // }\n\n // int getNum(bitset<32> br){\n // int num = 0;\n // for(int j = 0; j < 32; j++){\n // int exp = pow(2.0, 1.0*(31-j)) + 0.1;\n // num += (br[j] * exp);\n // }\n // return num;\n // }\n\n\n int findMaximumXOR(vector<int>& nums) {\n Trie* t = new Trie(-1);\n int N = nums.size();\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n t->insertInTrie(br);\n }\n\n int maxi = INT_MIN;\n for(int i = 0; i < N; i++){\n int num = nums[i];\n bitset<32> br(num);\n bitset<32> br2;\n t->getMaxFromTrie(br, br2);\n int b = br2.to_ullong();\n maxi = max(maxi, num^b);\n }\n\n return maxi;\n }\n};", "memory": "428533" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "const int LM = 31; // for bit Trie\nclass TrieNode {\n public:\n int data;\n bool isEnd;\n int endCnt;\n int preCnt;\n int k;\n vector<TrieNode*> child;\n \n TrieNode(int val, int K){\n data = val;\n k = K;\n isEnd = 0;\n endCnt = preCnt = 0;\n child.resize(k,0);\n }\n};\nclass Trie {\n public:\n\n TrieNode* root = 0;\n int k;\n\n Trie(int K){\n k = K;\n root = new TrieNode(-1,k);\n }\n\n void insert(string &s){\n TrieNode *curr = root;\n for(auto it : s){\n curr->preCnt++;\n if(curr->child[it-'a'] == 0){\n TrieNode *temp = new TrieNode(it-'a',k);\n curr->child[it-'a'] = temp;\n }\n curr = curr->child[it-'a'];\n }\n curr->isEnd = 1;\n curr->endCnt++;\n curr->preCnt++;\n }\n\n void insert(int num){\n TrieNode *curr = root;\n for(int i=LM; i>=0; i--){\n curr->preCnt++;\n if((num>>i)&1){\n if(curr->child[1]==0){\n TrieNode *temp = new TrieNode(1,k);\n curr->child[1] = temp;\n }\n curr = curr->child[1];\n }\n else{\n if(curr->child[0]==0){\n TrieNode *temp = new TrieNode(0,k);\n curr->child[0] = temp;\n }\n curr = curr->child[0];\n }\n }\n curr->isEnd = 1;\n curr->endCnt++;\n curr->preCnt++;\n }\n\n bool find(string &s){\n TrieNode* head = root;\n for(int i=0; i<s.size(); i++){\n if(head->child[s[i]-'a']==0){\n return 0;\n }\n head = head->child[s[i]-'a'];\n }\n return head->isEnd;\n }\n\n bool find(int num){\n TrieNode* head = root;\n for(int i=LM; i>=0; i--){\n if(head->child[(num>>i)&1]==0){\n return 0;\n }\n head = head->child[(num>>i)&1];\n }\n return head->isEnd;\n }\n\n void erase(string &s){\n bool check = find(s);\n if(!check){\n return;\n }\n\n TrieNode* head = root;\n for(int i=0; i<s.size(); i++){\n head->preCnt--;\n head = head->child[s[i]-'a'];\n }\n head->preCnt--;\n head->endCnt--;\n if((head->endCnt)==0){\n head->isEnd = 0;\n }\n\n }\n\n void erase(int num){\n bool check = find(num);\n if(!check){\n return;\n }\n\n TrieNode* head = root;\n for(int i=LM; i>=0; i--){\n head->preCnt--;\n head = head->child[(num>>i)&1];\n }\n head->preCnt--;\n head->endCnt--;\n if((head->endCnt)==0){\n head->isEnd = 0;\n }\n\n }\n\n int countWords(string &s){\n TrieNode* head = root;\n for(int i=0; i<s.size(); i++){\n if(head->child[s[i]-'a']==0){\n return 0;\n }\n head = head->child[s[i]-'a'];\n }\n return head->endCnt;\n }\n\n int countWordsWithPrefix(string &s){\n TrieNode* head = root;\n for(int i=0; i<s.size(); i++){\n if(head->child[s[i]-'a']==0){\n return 0;\n }\n head = head->child[s[i]-'a'];\n }\n return head->preCnt;\n }\n\n int solve(int num){\n int ans = 0;\n TrieNode* head = root;\n for(int i=LM; i>=0; i--){\n if((num>>i)&1){\n // off\n if(head->child[0]){\n ans |= (1<<i);\n head = head->child[0];\n }\n else if(head->child[1]){\n head = head->child[1];\n }\n }\n else{\n // on\n if(head->child[1]){\n ans |= (1<<i);\n head = head->child[1];\n }\n else if(head->child[0]){\n head = head->child[0];\n }\n }\n }\n\n return ans;\n }\n\n\n};\n\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n Trie ds(2);\n int ans = 0;\n for(int i=0; i<nums.size(); i++){\n ds.insert(nums[i]);\n int temp = ds.solve(nums[i]);\n ans = max(ans,temp);\n }\n return ans;\n }\n};", "memory": "428533" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Trie {\npublic:\n Trie* child[2];\n Trie() {\n for(int i = 0; i < 2; i++) {\n child[i] = NULL;\n }\n }\n};\n\nclass Solution {\npublic:\n Trie* root = new Trie();\n\n void insert(string s) {\n Trie* temp = root;\n for (auto ch : s) {\n int ind = ch - '0';\n if (temp->child[ind] == NULL) {\n temp->child[ind] = new Trie();\n }\n temp = temp->child[ind];\n }\n }\n\n string bitcon(int x) {\n string ans = \"\";\n for (int i = 31; i >= 0; i--) {\n int bit = (x >> i) & 1;\n ans += to_string(bit);\n }\n return ans;\n }\n\n int findMaximumXOR(vector<int>& nums) {\n vector<string> bits;\n for (auto num : nums) {\n string st = bitcon(num);\n insert(st);\n bits.push_back(st);\n }\n\n int ansmax = 0;\n for (auto bit : bits) {\n int res = 0;\n Trie* temp = root;\n for (auto b : bit) {\n int ind = b - '0';\n int flip = 1 - ind;\n if (temp->child[flip] != NULL) {\n res = (res << 1) | 1;\n temp = temp->child[flip];\n } else {\n res = res << 1;\n temp = temp->child[ind];\n }\n }\n ansmax = max(ansmax, res);\n }\n\n return ansmax;\n }\n};\n", "memory": "433491" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "struct Node{\n Node *link[2];\n};\n\nclass Solution {\npublic:\n \n\n Node* root;\n void insert(vector<int>& num){\n Node *node = root;\n for(int i=0;i<32;i++){\n if(node->link[num[i]]==NULL){\n node->link[num[i]]=new Node();\n }\n node=node->link[num[i]];\n }\n }\n vector<vector<int>> bitsarray;\n void bits(int num){\n vector<int> bit(32.0);\n int j=31;\n while(num!=0){\n bit[j--]=num%2;\n num/=2;\n }\n bitsarray.push_back(bit);\n }\n\n int maxXor(vector<int>& num){\n Node* node=root;\n int ans=0;\n for(int i=0;i<32;i++){\n if((node->link[1-num[i]])!=NULL){\n ans|=(1<<(31-i));\n node=node->link[1-num[i]];\n }\n else{\n node=node->link[num[i]];\n }\n }\n\n return ans;\n }\n int findMaximumXOR(vector<int>& nums) {\n root = new Node();\n\n for(int i=0;i<nums.size();i++){\n bits(nums[i]);\n }\n\n for(int i=0;i<nums.size();i++){\n insert(bitsarray[i]);\n }\n\n int ans=0;\n for(int i=0;i<nums.size();i++){\n ans=max(ans,maxXor(bitsarray[i]));\n }\n\n return ans;\n\n }\n};", "memory": "433491" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n ll m = nums.size();\n vector<ll> f;\n f.push_back(0);\n f.push_back(0);\n ll n = 0;\n vector<vector<ll> > trie;\n trie.push_back(f);\n for(ll i=0; i<m; i++){\n ll cur = 0;\n ll x = nums[i];\n for(ll j=31; j>=0; j--){\n if(x&(1<<j)){\n if(trie[cur][1]!=0){\n cur = trie[cur][1];\n }else{\n n++;\n trie[cur][1] = n;\n cur = n;\n trie.push_back(f);\n }\n }else{\n if(trie[cur][0]!=0){\n cur = trie[cur][0];\n }else{\n n++;\n trie[cur][0] = n;\n cur = n;\n trie.push_back(f);\n }\n\n }\n }\n }\n // for(ll i=0; i<=n;i++){\n // cout<<trie[i][0]<<\" \"<<trie[i][1]<<\"\\n\";\n // }\n ll ans = 0;\n for(ll i=0; i<m; i++){\n ll cur = 0;\n ll y = 0;\n ll x = nums[i];\n for(ll j=31; j>=0; j--){\n if(x&(1<<j)){\n if(trie[cur][0]!=0){\n y += (1<<j);\n cur = trie[cur][0];\n }else{\n cur = trie[cur][1];\n }\n }else{\n if(trie[cur][1]!=0){\n y += (1<<j);\n cur = trie[cur][1];\n }else{\n cur = trie[cur][0];\n }\n }\n }\n // cout<<x<<\" \"<<y<<\"\\n\";\n ll e = x ^ y;\n ans = fmax(ans,y);\n }\n return ans;\n }\n};", "memory": "438448" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n vector<vector<int>> nxt;\n nxt.push_back(vector<int>(2));\n int N = 0;\n for (auto num: nums) {\n int node = 0;\n for(int i = 30; i >= 0; i--) {\n int digit = ((1ll<<i) & num) != 0;\n if (nxt[node][digit] == 0) {\n nxt.push_back(vector<int>(2));\n nxt[node][digit] = ++N;\n } \n node = nxt[node][digit];\n }\n }\n\n int ans = 0;\n for (int num: nums) {\n int node = 0, curr = 0;\n for (int i = 30; i >= 0; i--) {\n int digit = ((1ll<<i) & num) != 0;\n if (nxt[node][!digit] != 0) {\n curr |= (1<<i);\n node = nxt[node][!digit];\n }\n else node = nxt[node][digit];\n }\n ans = max(ans, curr);\n }\n\n return ans;\n }\n};", "memory": "453321" }
421
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,10,5,25,2,8] <strong>Output:</strong> 28 <strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70] <strong>Output:</strong> 127 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaximumXOR(vector<int>& nums) {\n int ans=0;\n int c=1;\n vector<vector<int>> node(1, vector<int> (2,-1));\n for(auto k: nums){\n int x=0;\n for(int i=31;i>=0;i--){\n if((k>>i)&1){\n if(node[x][1]==-1){\n node.push_back(vector<int> (2,-1));\n node[x][1]=c++;\n }\n x=node[x][1];\n }\n else{\n if(node[x][0]==-1){\n node.push_back(vector<int> (2,-1));\n node[x][0]=c++;\n }\n x=node[x][0];\n }\n }\n }\n for(auto k: nums){\n int val=0;\n int x=0;\n for(int i=31;i>=0;i--){\n if((k>>i)&1){\n if(node[x][0]==-1){\n x=node[x][1];\n }\n else{\n val|=(1<<i);\n x=node[x][0];\n }\n }\n else{\n if(node[x][1]==-1){\n x=node[x][0];\n }\n else{\n val|=(1<<i);\n x=node[x][1];\n }\n }\n }\n ans=max(ans,val);\n }\n return ans;\n }\n};", "memory": "453321" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution \n{\nprivate:\n struct WordDigitPair\n {\n std::string_view word;\n int digit = 0;\n };\n\n // Sorted in order of unique letters and length.\n // This allows determine the count for each digit efficiently.\n // E.g. we can just count how many letters that make up zero\n // are in the array.\n const std::vector<WordDigitPair> s_wordDigitPairs = \n {\n {\"zero\", 0},\n {\"two\", 2},\n {\"six\", 6},\n {\"eight\", 8},\n {\"seven\", 7},\n {\"three\", 3},\n {\"four\", 4},\n {\"five\", 5},\n {\"nine\", 9},\n {\"one\", 1},\n };\n\npublic:\n string originalDigits(string& input)\n {\n std::vector<int> mapCharCount;\n mapCharCount.resize(std::numeric_limits<char>::max());\n for(char c: input)\n {\n mapCharCount[c] += 1;\n }\n\n // Get the digits from the string\n std::vector<int> digitsCount;\n digitsCount.resize(s_wordDigitPairs.size());\n for(const auto& wordDigitPair: s_wordDigitPairs)\n {\n int minCharsCount = std::numeric_limits<int>::max();\n for(char c: wordDigitPair.word)\n {\n minCharsCount = std::min(mapCharCount[c], minCharsCount);\n }\n for(char c: wordDigitPair.word)\n {\n mapCharCount[c] -= minCharsCount;\n }\n digitsCount[wordDigitPair.digit] = minCharsCount;\n }\n\n // Re-use the memory allocated for the input\n std::string& output = input;\n output.clear();\n for(size_t digit = 0; digit < digitsCount.size(); ++digit)\n {\n int digitCount = digitsCount[digit];\n char digitChar = '0' + digit;\n output.append(digitCount, digitChar);\n } \n return std::move(output);\n }\n};", "memory": "9500" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution \n{\nprivate:\n struct WordDigitPair\n {\n std::string_view word;\n int digit = 0;\n };\n\n // Sorted in order of unique letters and length\n const std::vector<WordDigitPair> s_wordDigitPairs = \n {\n {\"zero\", 0},\n {\"two\", 2},\n {\"six\", 6},\n {\"eight\", 8},\n {\"seven\", 7},\n {\"three\", 3},\n {\"four\", 4},\n {\"five\", 5},\n {\"nine\", 9},\n {\"one\", 1},\n };\n\npublic:\n string originalDigits(string& input)\n {\n std::vector<int> mapCharCount;\n mapCharCount.resize(std::numeric_limits<char>::max());\n for(char c: input)\n {\n mapCharCount[c] += 1;\n }\n\n // Get the digits from the string\n std::vector<int> digitsCount;\n digitsCount.resize(s_wordDigitPairs.size());\n for(const auto& wordDigitPair: s_wordDigitPairs)\n {\n int minCharsCount = std::numeric_limits<int>::max();\n for(char c: wordDigitPair.word)\n {\n minCharsCount = std::min(mapCharCount[c], minCharsCount);\n }\n for(char c: wordDigitPair.word)\n {\n mapCharCount[c] -= minCharsCount;\n }\n digitsCount[wordDigitPair.digit] = minCharsCount;\n }\n\n \n // Re-use the memory allocated for the input\n std::string& output = input;\n output.clear();\n for(size_t digit = 0; digit < digitsCount.size(); ++digit)\n {\n int digitCount = digitsCount[digit];\n char digitChar = '0' + digit;\n output.append(digitCount, digitChar);\n } \n return std::move(output);\n }\n};", "memory": "9600" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\n public:\n const string originalDigits(const string& s) {\n u_short count[10] {0};\n const uint sLen = s.size();\n for (uint i = 0; i < sLen; ++i) {\n switch (s[i]) {\n case 'z': ++count[0]; break;\n case 'w': ++count[2]; break;\n case 'u': ++count[4]; break;\n case 'x': ++count[6]; break;\n case 'g': ++count[8]; break;\n case 'h': ++count[3]; break;\n case 's': ++count[7]; break;\n case 'f': ++count[5]; break;\n case 'i': ++count[9]; break;\n case 'o': ++count[1]; break;\n }\n }\n count[3] -= count[8];\n count[7] -= count[6];\n count[5] -= count[4];\n count[9] -= count[5] + count[6] + count[8];\n count[1] -= count[0] + count[2] + count[4];\n string digits = \"\";\n // for (u_short i = 0; i < 10; ++i) digits += string(count[i], 48 + i);\n for (u_short i = 0; i < 10; ++i)\n while (count[i] > 0) {\n --count[i];\n digits.push_back(48 + i);\n }\n return digits;\n }\n};", "memory": "9700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\n public:\n const string originalDigits(const string& s) {\n int count[10] {0};\n const int sLen = s.size();\n for (int i = 0; i < sLen; ++i) {\n switch (s[i]) {\n case 'z': ++count[0]; break;\n case 'w': ++count[2]; break;\n case 'u': ++count[4]; break;\n case 'x': ++count[6]; break;\n case 'g': ++count[8]; break;\n case 'h': ++count[3]; break;\n case 's': ++count[7]; break;\n case 'f': ++count[5]; break;\n case 'i': ++count[9]; break;\n case 'o': ++count[1]; break;\n }\n }\n count[3] -= count[8];\n count[7] -= count[6];\n count[5] -= count[4];\n count[9] -= count[5] + count[6] + count[8];\n count[1] -= count[0] + count[2] + count[4];\n string digits;\n for (int i = 0; i < 10; ++i)\n while (--count[i] >= 0) digits.push_back(48 + i);\n return digits;\n }\n};", "memory": "9800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string number[10] = {\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\"};\n std::vector<std::pair<char, int>> mappingTable ={{'x', 6}, {'g', 8}, {'w', 2}, {'z', 0}, {'s', 7}, {'v', 5}, {'f', 4}, {'o', 1}, {'h', 3}, {'i', 9}};\n string originalDigits(string s) {\n int total = 0;\n int charMap[123] = {0};\n int numberCntBuf[10] = {0};\n\n for(char c:s) {\n charMap[c]++;\n }\n\n for(auto x:mappingTable) {\n int n = charMap[x.first];\n total += n;\n numberCntBuf[x.second] = n;\n\n for(char word:number[x.second]) {\n charMap[word] -= n;\n }\n }\n\n string ret(total, '*');\n for(int i=0,j=0;i<10;i++) {\n while(numberCntBuf[i]--) {\n ret[j++] = i + '0';\n }\n }\n\n return ret;\n }\n};", "memory": "9900" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "static constexpr pair<char, int> digChars[10] = {{'z', 0}, {'w', 2}, {'x', 6}, {'s', 7}, {'g', 8}, {'h', 3}, {'v', 5}, {'f', 4}, {'o', 1}, {'e', 9}};\n\nstatic string digWord[10] = {\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\"};\n\nclass Solution {\npublic:\n string originalDigits(string s) {\n // support variables\n\t\tint digits[10], tot = 0, alpha[123] = {};\n // populating alpha\n for (char c: s) alpha[c]++;\n for (auto d: digChars) {\n // getting the number of matches\n int n = alpha[d.first];\n // updating digits with the number of matches\n digits[d.second] = n;\n // updating tot by the number of needed characters\n tot += n;\n // clearing up alpha accordingly\n for (char c: digWord[d.second]) alpha[c] -= n;\n }\n // creating and populating res\n string res(tot, '*');\n for (int i = 0, j = 0; i < 10; i++) {\n while(digits[i]--) {\n res[j++] = i + '0';\n }\n }\n return res;\n }\n};", "memory": "10000" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\n public:\n string originalDigits(string s) {\n string ans;\n vector<int> count(10);\n\n for (const char c : s) {\n if (c == 'z')\n ++count[0];\n if (c == 'o')\n ++count[1];\n if (c == 'w')\n ++count[2];\n if (c == 'h')\n ++count[3];\n if (c == 'u')\n ++count[4];\n if (c == 'f')\n ++count[5];\n if (c == 'x')\n ++count[6];\n if (c == 's')\n ++count[7];\n if (c == 'g')\n ++count[8];\n if (c == 'i')\n ++count[9];\n }\n\n count[1] -= count[0] + count[2] + count[4];\n count[3] -= count[8];\n count[5] -= count[4];\n count[7] -= count[6];\n count[9] -= count[5] + count[6] + count[8];\n\n for (int i = 0; i < 10; ++i)\n for (int j = 0; j < count[i]; ++j)\n ans += i + '0';\n\n return ans;\n }\n};", "memory": "10100" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n std::vector<std::pair<char, int>> compare= {{'x', 6}, {'z', 0}, {'g', 8}, {'w', 2}, {'s', 7}, {'h', 3}, {'v', 5}, {'f', 4}, {'o', 1}, {'e', 9} };\n string number[10] = {\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\"};\n string originalDigits(string s) {\n int tot = 0;\n int charBufCnt[123] = {0};\n int digit[10] = {0};\n\n for(char c:s) {\n charBufCnt[c]++;\n }\n\n for(auto tmp:compare) {\n int n = charBufCnt[tmp.first];\n tot += n;\n digit[tmp.second] = n;\n\n for(char x:number[tmp.second]) {\n charBufCnt[x] -= n;\n }\n }\n\n string ret(tot, '*');\n for(int i=0, j=0;i<10;i++) {\n while(digit[i]--) {\n ret[j++] = i + '0';\n }\n }\n\n return ret;\n }\n};", "memory": "10200" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<int> counter(26);\n for (char c : s) ++counter[c - 'a'];\n vector<int> cnt(10);\n cnt[0] = counter['z' - 'a'];\n cnt[2] = counter['w' - 'a'];\n cnt[4] = counter['u' - 'a'];\n cnt[6] = counter['x' - 'a'];\n cnt[8] = counter['g' - 'a'];\n\n cnt[3] = counter['h' - 'a'] - cnt[8];\n cnt[5] = counter['f' - 'a'] - cnt[4];\n cnt[7] = counter['s' - 'a'] - cnt[6];\n\n cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];\n cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];\n\n string ans;\n for (int i = 0; i < 10; ++i)\n for (int j = 0; j < cnt[i]; ++j)\n ans += char(i + '0');\n return ans;\n }\n};", "memory": "10300" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\n public:\n string originalDigits(string s) {\n string ans;\n vector<int> count(10);\n\n for (const char c : s) {\n if (c == 'z')\n ++count[0];\n if (c == 'o')\n ++count[1];\n if (c == 'w')\n ++count[2];\n if (c == 'h')\n ++count[3];\n if (c == 'u')\n ++count[4];\n if (c == 'f')\n ++count[5];\n if (c == 'x')\n ++count[6];\n if (c == 's')\n ++count[7];\n if (c == 'g')\n ++count[8];\n if (c == 'i')\n ++count[9];\n }\n\n count[1] -= count[0] + count[2] + count[4];\n count[3] -= count[8];\n count[5] -= count[4];\n count[7] -= count[6];\n count[9] -= count[5] + count[6] + count[8];\n\n for (int i = 0; i < 10; ++i)\n for (int j = 0; j < count[i]; ++j)\n ans += i + '0';\n\n return ans;\n }\n};", "memory": "10400" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n vector<int> freq(26, 0);\n string ans = \"\";\n for(int i = 0;i< s.size();i++){\n freq[s[i] - 'a']++;\n }\n for(int i = 0 ;i<freq[25];i++){\n ans += '0';\n freq['o'-'a']--;\n }\n for(int i = 0 ;i<freq['w'- 'a'];i++){\n ans += '2';\n freq['o'-'a']--;\n }\n for(int i = 0 ;i<freq['g'-'a'];i++){\n ans += '8';\n freq['i'-'a']--;\n freq['h' - 'a']--;\n }\n for(int i = 0 ;i<freq['h'- 'a'];i++) ans += '3';\n for(int i = 0 ;i<freq['u'-'a'];i++){\n ans += '4';\n freq['o'-'a']--;}\n for(int i = 0 ;i<freq['x' -'a'];i++){ \n ans += '6';\n freq['s'- 'a']--;\n freq['i'-'a']--;\n }\n for(int i = 0 ;i< freq['s'-'a'];i++){\n ans += '7';\n freq['v'- 'a']--;\n }\n for(int i = 0 ;i<freq['v' - 'a'];i++){\n ans += '5';\n freq['i'-'a']--;\n }\n \n \n for(int i = 0 ;i< freq['o'-'a'];i++) ans += '1';\n \n for(int i = 0 ;i< freq['i'-'a'];i++) ans += '9';\n\n sort(ans.begin(),ans.end());\n return ans;\n\n \n\n \n \n }\n};", "memory": "10500" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n // 1. Declare state variables\n int length = s.length();\n std::vector<int> charCount(256, 0);\n \n // 2. Update count of characters\n for (int i = 0; i < length; i++) {\n charCount[s[i]]++;\n }\n \n // 3. Update the string\n std::string ans {};\n int val { 0 };\n \n // 3.0.\n val = charCount['z'];\n if (val) {\n ans.append(val, '0');\n charCount['z'] -= val;\n charCount['e'] -= val;\n charCount['r'] -= val;\n charCount['o'] -= val;\n }\n \n // 3.2.\n val = charCount['w'];\n if (val) {\n ans.append(val, '2');\n charCount['t'] -= val;\n charCount['w'] -= val;\n charCount['o'] -= val;\n }\n\n // 3.4.\n val = charCount['u'];\n if (val) {\n ans.append(val, '4');\n charCount['f'] -= val;\n charCount['o'] -= val;\n charCount['u'] -= val;\n charCount['r'] -= val;\n }\n\n // 3.6.\n val = charCount['x'];\n if (val) {\n ans.append(val, '6');\n charCount['s'] -= val;\n charCount['i'] -= val;\n charCount['x'] -= val;\n }\n\n // 3.8.\n val = charCount['g'];\n if (val) {\n ans.append(val, '8');\n charCount['e'] -= val;\n charCount['i'] -= val;\n charCount['g'] -= val;\n charCount['h'] -= val;\n charCount['t'] -= val;\n }\n \n // 3.3.\n val = charCount['h'];\n if (val) {\n ans.append(val, '3');\n charCount['t'] -= val;\n charCount['h'] -= val;\n charCount['r'] -= val;\n charCount['e'] -= val * 2;\n }\n\n // 3.1.\n val = std::min(std::min(charCount['o'], charCount['n']), charCount['e']);\n if (val) {\n ans.append(val, '1');\n charCount['o'] -= val;\n charCount['n'] -= val;\n charCount['e'] -= val;\n }\n\n // 3.5.\n val = std::min(std::min(std::min(charCount['f'], charCount['i']), charCount['v']), charCount['e']);\n if (val) {\n ans.append(val, '5');\n charCount['f'] -= val;\n charCount['i'] -= val;\n charCount['v'] -= val;\n charCount['e'] -= val;\n }\n\n // 3.7.\n val = std::min(std::min(std::min(charCount['s'], charCount['v']), charCount['n']), charCount['e'] / 2);\n if (val) {\n ans.append(val, '7');\n charCount['s'] -= val;\n charCount['v'] -= val;\n charCount['n'] -= val;\n charCount['e'] -= val * 2;\n }\n\n // 3.9.\n val = std::min(std::min(charCount['i'], charCount['e']), charCount['n'] / 2);\n if (val) {\n ans.append(val, '9');\n charCount['i'] -= val;\n charCount['e'] -= val;\n charCount['n'] -= val * 2;\n }\n \n std::sort(ans.begin(), ans.end());\n\n return ans;\n }\n};", "memory": "10600" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n int nums[10];\n vector<int> freq(26);\n for(int i=0;i<s.size();i++){\n freq[s[i]-'a']+=1;\n }\n nums[0]=freq['z'-'a'];\n nums[2]=freq['w'-'a'];\n nums[4]=freq['u'-'a'];\n nums[5]=freq['f'-'a']-nums[4];\n nums[6]=freq['x'-'a'];\n nums[8]=freq['g'-'a'];\n nums[1]=freq['o'-'a']-nums[0]-nums[2]-nums[4];\n nums[3]=freq['h'-'a']-nums[8];\n nums[7]=freq['s'-'a']-nums[6];\n nums[9]=freq['i'-'a']-nums[6]-nums[8]-nums[5];\n string res;\n for(int i=0;i<10;i++){\n if(nums[i]!=0){\n res+=string(nums[i],'0'+i);\n }\n }\n return res;\n }\n};", "memory": "10700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
0
{ "code": "class Solution \n{\nprivate:\n struct WordDigitPair\n {\n std::string_view word;\n int digit = 0;\n };\n\n // Sorted in order of unique letters and length\n const std::vector<WordDigitPair> s_wordDigitPairs = \n {\n {\"zero\", 0},\n {\"two\", 2},\n {\"six\", 6},\n {\"eight\", 8},\n {\"seven\", 7},\n {\"three\", 3},\n {\"four\", 4},\n {\"five\", 5},\n {\"nine\", 9},\n {\"one\", 1},\n };\n\npublic:\n string originalDigits(string& input)\n {\n std::map<char, int> mapCharCount;\n for(char c: input)\n {\n mapCharCount[c] += 1;\n }\n\n // Get the digits from the string\n std::vector<int> digitsCount;\n digitsCount.resize(s_wordDigitPairs.size());\n for(const auto& wordDigitPair: s_wordDigitPairs)\n {\n int minCharsCount = std::numeric_limits<int>::max();\n for(char c: wordDigitPair.word)\n {\n minCharsCount = std::min(mapCharCount[c], minCharsCount);\n }\n for(char c: wordDigitPair.word)\n {\n mapCharCount[c] -= minCharsCount;\n }\n digitsCount[wordDigitPair.digit] = minCharsCount;\n }\n\n \n // Re-use the memory allocated for the input\n std::string& output = input;\n output.clear();\n for(size_t digit = 0; digit < digitsCount.size(); ++digit)\n {\n int digitCount = digitsCount[digit];\n char digitChar = '0' + digit;\n output.append(digitCount, digitChar);\n } \n return std::move(output);\n }\n};", "memory": "10800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n int count[26] = {}, resultCount[10] = {};\n for (char c : s)\n count[c - 'a']++;\n vector<tuple<char, char, string>> digits{\n {'0', 'z', \"zero\"}, {'2', 'w', \"two\"}, {'4', 'u', \"four\"},\n {'6', 'x', \"six\"}, {'8', 'g', \"eight\"}, {'1', 'o', \"one\"},\n {'3', 'r', \"three\"}, {'5', 'f', \"five\"}, {'7', 'v', \"seven\"},\n {'9', 'e', \"nine\"}};\n for (auto& digit : digits) {\n int digitCount = count[get<1>(digit) - 'a'];\n resultCount[get<0>(digit) - '0'] += digitCount;\n for (char c : get<2>(digit))\n count[c - 'a'] -= digitCount;\n }\n string result;\n for (int i = 0; i < 10; i++)\n result += string(resultCount[i], i + '0');\n return result;\n }\n};", "memory": "10900" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string helper(int num, int freq){\n string res = \"\", tmp = to_string(num);\n for(int i=1;i<=freq;i++) res += tmp;\n return res;\n }\n string originalDigits(string s) {\n vector<int> hash(256, 0), num(10, 0);\n for(auto c : s) hash[(int)c]++;\n num[0] = hash[(int)'z']; // 0\n hash[(int)'o'] -= num[0];\n\n num[2] = hash[(int)'w']; // 2\n hash[(int)'o'] -= num[2];\n \n num[4] = hash[(int)'u']; // 4\n hash[(int)'f'] -= num[4];\n hash[(int)'o'] -= num[4];\n\n num[6] = hash[(int)'x']; // 6\n\n num[8] = hash[(int)'g']; // 8\n hash[(int)'h'] -= num[8];\n\n num[1] = hash[(int)'o']; // 1\n hash[(int)'n'] -= num[1];\n\n num[3] = hash[(int)'h']; // 3\n\n num[5] = hash[(int)'f']; // 5\n hash[(int)'v'] -= num[5];\n\n num[7] = hash[(int)'v']; // 7\n hash[(int)'n'] -= num[7];\n\n num[9] = hash[(int)'n'] / 2; // 9\n\n string res = \"\";\n for(int i=0;i<num.size();i++){\n if(num[i] > 0) res += helper(i, num[i]);\n }\n return res;\n }\n};", "memory": "11000" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<string> digitWords{\n \"zero\",\n \"one\",\n \"two\",\n \"three\",\n \"four\",\n \"five\",\n \"six\",\n \"seven\",\n \"eight\",\n \"nine\",\n \"ten\"\n };\n\n vector<int> charCounts(26, 0);\n\n for (char c : s)\n charCounts[c-'a']++;\n\n vector<int> digitCounts(10, 0);\n\n vector<pair<int, char>> identifyingChar{\n {0,'z'},\n {6,'x'},\n {2,'w'},\n {4,'u'},\n {8,'g'},\n {3,'r'},\n {7,'s'},\n {5,'v'},\n {9,'i'},\n {1,'o'},\n };\n\n for (auto& pair : identifyingChar) {\n digitCounts[pair.first] = charCounts[pair.second-'a'];\n for (char c : digitWords[pair.first])\n charCounts[c-'a'] -= digitCounts[pair.first];\n }\n\n string s2 = \"\";\n for (int i = 0; i < digitCounts.size(); i++) {\n s2 += string(digitCounts[i], '0'+i);\n }\n return s2;\n }\n};", "memory": "11100" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<int> freq(26, 0);\n for(char ch: s)\n freq[ch - 'a']++;\n \n vector<string> names = {\n \"zero\",\n \"one\",\n \"two\",\n \"three\",\n \"four\",\n \"five\",\n \"six\",\n \"seven\",\n \"eight\",\n \"nine\",\n };\n\n vector<vector<pair<char, int>>> levels = {\n {{'z', 0}, {'w', 2}, {'u', 4}, {'x', 6}, {'g', 8}},\n {{'h', 3}, {'f', 5}, {'s', 7}},\n {{'o', 1}, {'i', 9}}\n };\n\n vector<int> digitFreq(10, 0);\n for(auto& level: levels)\n for(auto [ch, dig]: level)\n {\n int f = freq[ch - 'a'];\n for(char c: names[dig])\n freq[c - 'a'] -= f;\n \n if(dig == 3 || dig == 7)\n freq['e' - 'a'] += f;\n else if(dig == 9)\n freq['n' - 'a'] += f;\n \n digitFreq[dig] = f;\n }\n\n string ans = \"\";\n for(int i=0; i<=9; i++)\n {\n string str(digitFreq[i], '0' + i);\n ans += str;\n }\n\n return ans;\n }\n};\n/*\nzero - z\none - 024o\ntwo - w\nthree - 8h\nfour - u\nfive - 4f\nsix - x\nseven - 6s\neight - g\nnine - 568n\n*/", "memory": "11200" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n unordered_map<int, string> digit_to_word = {\n {0, \"zero\"}, // z\n {2, \"two\"}, // w\n {4, \"four\"}, // u\n {6, \"six\"}, // x\n {8, \"eight\"},// g\n {3, \"three\"},// h (!g)\n {5, \"five\"}, // f(!u)\n {7, \"seven\"},// s(!x)\n {9, \"nine\"}, // i\n {1, \"one\"} // n\n };\n\n const vector<pair<int, char>> unique_letter = {\n {0, 'z'}, {2, 'w'}, {4, 'u'}, {6, 'x'},\n {8, 'g'}, {3, 'h'}, {5, 'f'}, {7, 's'},\n {9, 'i'}, {1, 'n'}\n };\n\n unordered_map<char, int> counter;\n for (auto &c : s) {\n ++counter[c];\n }\n\n vector<int> ans(10);\n int size = 0;\n for (const auto &[dig, let] : unique_letter) {\n if (counter.contains(let)) {\n ans[dig] += counter[let];\n size += ans[dig];\n for (auto &c : digit_to_word[dig]) {\n counter[c] -= ans[dig];\n }\n }\n }\n\n string result(size, ' ');\n\n int ind = 0;\n for (int i = 0; i < 10; ++i) {\n for (int j = 0; j < ans[i]; ++j) {\n result[ind++] = char(i + '0');\n }\n }\n\n return result;\n }\n};", "memory": "11300" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "#include <string>\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n string originalDigits(string s) {\n // Hash map to store the frequency of each letter in the input string\n unordered_map<char, int> count;\n for (char c : s) {\n count[c]++;\n }\n\n // Hash map to store the count of each digit (0-9)\n unordered_map<int, int> digitCount;\n\n // Find digits based on unique characters\n digitCount[0] = count['z']; // 'z' is unique to \"zero\"\n digitCount[2] = count['w']; // 'w' is unique to \"two\"\n digitCount[4] = count['u']; // 'u' is unique to \"four\"\n digitCount[6] = count['x']; // 'x' is unique to \"six\"\n digitCount[8] = count['g']; // 'g' is unique to \"eight\"\n\n // Find remaining digits\n digitCount[3] = count['h'] - digitCount[8]; // 'h' is in \"three\" and \"eight\"\n digitCount[5] = count['f'] - digitCount[4]; // 'f' is in \"five\" and \"four\"\n digitCount[7] = count['s'] - digitCount[6]; // 's' is in \"seven\" and \"six\"\n digitCount[1] = count['o'] - digitCount[0] - digitCount[2] - digitCount[4]; // 'o' is in \"one\", \"zero\", \"two\", and \"four\"\n digitCount[9] = count['i'] - digitCount[5] - digitCount[6] - digitCount[8]; // 'i' is in \"nine\", \"five\", \"six\", and \"eight\"\n\n // Construct the result based on the digit counts\n string result;\n for (int i = 0; i < 10; ++i) {\n for(int j = 0; j < digitCount[i]; ++j){\n result += (char)('0' + i);\n }\n }\n\n return result;\n }\n};\n", "memory": "11700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "#include <string>\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n string originalDigits(string s) {\n // Hash map to store the frequency of each letter in the input string\n unordered_map<char, int> count;\n for (char c : s) {\n count[c]++;\n }\n\n // Hash map to store the count of each digit (0-9)\n unordered_map<int, int> digitCount;\n\n // Find digits based on unique characters\n digitCount[0] = count['z']; // 'z' is unique to \"zero\"\n digitCount[2] = count['w']; // 'w' is unique to \"two\"\n digitCount[4] = count['u']; // 'u' is unique to \"four\"\n digitCount[6] = count['x']; // 'x' is unique to \"six\"\n digitCount[8] = count['g']; // 'g' is unique to \"eight\"\n\n // Find remaining digits\n digitCount[3] = count['h'] - digitCount[8]; // 'h' is in \"three\" and \"eight\"\n digitCount[5] = count['f'] - digitCount[4]; // 'f' is in \"five\" and \"four\"\n digitCount[7] = count['s'] - digitCount[6]; // 's' is in \"seven\" and \"six\"\n digitCount[1] = count['o'] - digitCount[0] - digitCount[2] - digitCount[4]; // 'o' is in \"one\", \"zero\", \"two\", and \"four\"\n digitCount[9] = count['i'] - digitCount[5] - digitCount[6] - digitCount[8]; // 'i' is in \"nine\", \"five\", \"six\", and \"eight\"\n\n // Construct the result based on the digit counts\n string result;\n for (int i = 0; i < 10; ++i) {\n for(int j = 0; j < digitCount[i]; ++j){\n result += (char)('0' + i);\n }\n }\n\n return result;\n }\n};\n", "memory": "11700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n /*\n string getDigits(string d, char digit, unordered_map<char, int>& m) {\n string res;\n while(1) {\n for(auto& c: d) {\n if(m[c] == 0) {\n return res;\n }\n }\n for(auto& c: d) {\n m[c]--;\n }\n res.push_back(digit);\n }\n return res;\n }\n \n string originalDigits(string s) {\n unordered_map<char, int> m;\n int cnt = 0;\n for(auto& c: s) {\n m[c]++;\n cnt++;\n }\n string res;\n res = res + getDigits(\"zero\", '0', m);\n res = res + getDigits(\"one\", '1', m);\n res = res + getDigits(\"two\", '2', m);\n res = res + getDigits(\"three\", '3', m);\n res = res + getDigits(\"four\", '4', m);\n res = res + getDigits(\"five\", '5', m);\n res = res + getDigits(\"six\", '6', m);\n res = res + getDigits(\"seven\", '7', m);\n res = res + getDigits(\"eight\", '8', m);\n res = res + getDigits(\"nine\", '9', m);\n return res;\n }\n */\n vector<string> v = {\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\"};\n string ans;\n bool helper(unordered_map<char, int>& m, int cnt, string& res, int idx) {\n if(ans.size() != 0) {\n return true;\n }\n if(idx == v.size()) {\n if(cnt == 0) {\n ans = res;\n return true;\n }\n return false;\n }\n bool r;\n r = helper(m, cnt, res, idx+1);\n if(r) {\n return r;\n }\n \n int found = 1;\n cout << res << endl;\n for(auto c: v[idx]) {\n if(m[c] == 0) {\n found = 0;\n break;\n }\n } \n \n if(found == 1) {\n for(auto c: v[idx]) {\n m[c]--;\n cnt--;\n }\n res = res + to_string(idx);\n }\n r = helper(m, cnt, res, idx);\n if(r) {\n return r;\n } else {\n if(found == 1) {\n res.pop_back();\n for(auto c: v[idx]) {\n m[c]++;\n }\n }\n return false;\n }\n }\n string originalDigits(string s) {\n unordered_map<char, int> m;\n for(char c : s) m[c]++;\n\n string ans;\n vector<pair<pair<char, char>, string>> v = {{{'z', '0'}, \"zero\"}, {{'w', '2'}, \"two\"}, {{'u', '4'}, \"four\"}, \n {{'x', '6'}, \"six\"}, {{'g', '8'}, \"eight\"}, {{'o', '1'}, \"one\"}, \n {{'t', '3'}, \"three\"}, {{'f', '5'}, \"five\"}, {{'s', '7'}, \"seven\"}, \n {{'i', '9'}, \"nine\"}};\n for(auto p : v) {\n int n = m[p.first.first];\n for(int i=0; i<n; i++) ans += p.first.second;\n for(char c : p.second) m[c] -= n;\n }\n sort(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "11800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char,int>m;\n vector<int>digits(10,0);\n\n for(char c:s){\n m[c]++;\n }\n\n digits[0]=m['z'];\n digits[2]=m['w'];\n digits[4]=m['u'];\n digits[6]=m['x'];\n digits[8]=m['g'];\n\n digits[3]=m['h']-digits[8];\n digits[5]=m['f']-digits[4];\n digits[7]=m['v']-digits[5];\n digits[1]=m['o']-digits[2]-digits[4]-digits[0];\n digits[9]=m['i']-digits[5]-digits[8]-digits[6];\n string result;\n for(int i=0;i<10;i++){\n result.append(digits[i],'0'+i);\n }\n return result;\n }\n};", "memory": "11900" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n unordered_map<char,int> count;\n for(char c : s)\n count[c]++;\n\n vector<int> digit(10,0);\n digit[0] = count['z'];\n digit[2] = count['w'];\n digit[4] = count['u'];\n digit[6] = count['x'];\n digit[8] = count['g'];\n\n digit[1] = count['o'] - digit[0] - digit[2] - digit[4];\n digit[3] = count['h'] - digit[8];\n digit[5] = count['f'] - digit[4];\n digit[7] = count['s'] - digit[6]; \n digit[9] = count['i'] - digit[5] - digit[6] - digit[8];\n string ans = \"\";\n for(int i=0; i<10; i++){\n ans += string(digit[i], '0' + i);\n }\n return ans;\n }\n};", "memory": "12000" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n unordered_map<char, int> count;\n string result;\n for (char c : s)\n count[c]++;\n vector<tuple<char, char, string>> digits{\n {'0', 'z', \"zero\"}, {'2', 'w', \"two\"}, {'4', 'u', \"four\"},\n {'6', 'x', \"six\"}, {'8', 'g', \"eight\"}, {'1', 'o', \"one\"},\n {'3', 'r', \"three\"}, {'5', 'f', \"five\"}, {'7', 'v', \"seven\"},\n {'9', 'e', \"nine\"}};\n for (auto& digit : digits) {\n int digitCount = count[get<1>(digit)];\n result += string(digitCount, get<0>(digit));\n for (char c : get<2>(digit))\n count[c] -= digitCount;\n }\n sort(result.begin(),result.end());\n return result;\n }\n};", "memory": "12100" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char,int>m;\n string ans;\n for(auto it:s) m[it]++;\n \n vector<int> digitCount(10, 0);\n \n \n digitCount[0] = m['z']; \n digitCount[2] = m['w']; \n digitCount[4] = m['u']; \n digitCount[6] = m['x']; \n digitCount[8] = m['g']; \n\n \ndigitCount[1] = m['o'] - digitCount[0] - digitCount[2] - digitCount[4]; \n digitCount[3] = m['h'] - digitCount[8]; \n digitCount[5] = m['f'] - digitCount[4]; \n digitCount[7] = m['s'] - digitCount[6]; \n digitCount[9] = m['i'] - digitCount[5] - digitCount[6] - digitCount[8]; \n \n for (int i = 0; i < 10; ++i) {\n ans += string(digitCount[i], '0' + i);\n }\n \n return ans;\n }\n};", "memory": "12200" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n//0: zero\n//1: one\n//2: two\n//3: three\n//4: four\n//5: five\n//6: six\n//7: seven\n//8: eight\n//9: nine\n string originalDigits(string s) {\n unordered_map<char, int> um;\n for(char c: s) um[c]++;\n vector<string> strs {\n \"zero\",//z\n \"one\",//n\n \"two\",//t\n \"three\",//h\n \"four\",//r\n \"five\",//f\n \"six\",//x\n \"seven\",//v\n \"eight\",//g\n \"nine\",//i\n };\n vector<int> count(10);\n if(0 < um['z']) {\n count[0] = um['z'];\n for(char c: strs[0]) um[c] -= count[0];\n }\n if(0 < um['x']) {\n count[6] = um['x'];\n for(char c: strs[6]) um[c] -= count[6];\n }\n if(0 < um['g']) {\n count[8] = um['g'];\n for(char c: strs[8]) um[c] -= count[8];\n }\n if(0 < um['h']) {\n count[3] = um['h'];\n for(char c: strs[3]) um[c] -= count[3];\n }\n if(0 < um['r']) {\n count[4] = um['r'];\n for(char c: strs[4]) um[c] -= count[4];\n }\n if(0 < um['f']) {\n count[5] = um['f'];\n for(char c: strs[5]) um[c] -= count[5];\n }\n if(0 < um['v']) {\n count[7] = um['v'];\n for(char c: strs[7]) um[c] -= count[7];\n }\n if(0 < um['i']) {\n count[9] = um['i'];\n for(char c: strs[9]) um[c] -= count[9];\n }\n if(0 < um['n']) {\n count[1] = um['n'];\n for(char c: strs[1]) um[c] -= count[1];\n }\n if(0 < um['t']) {\n count[2] = um['t'];\n for(char c: strs[2]) um[c] -= count[2];\n }\n\n string ans;\n for(int i = 0; i < 10; ++i) {\n if(0 < count[i]) {\n ans += string(count[i], '0' + i);\n }\n }\n return ans;\n }\n};", "memory": "12300" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "#include <stdio.h>\n#include <string.h>\nclass Solution {\npublic:\n string originalDigits(string s) {\n int Outputlist[10] = {0};\n string string_output;\n\n for (int i = 0; i< s.size(); i++)\n {\n if (s[i] == 'z') \n {\n Outputlist[0]++;\n }\n else if (s[i] == 'w') \n {\n Outputlist[2]++;\n }\n else if (s[i] == 'u') \n {\n Outputlist[4]++;\n }\n else if (s[i] == 'x') \n {\n Outputlist[6]++;\n }\n else if (s[i] == 'g') \n {\n Outputlist[8]++;\n }\n else if (s[i] == 's') \n {\n Outputlist[7]++;\n }\n else if (s[i] == 'v') \n {\n Outputlist[5]++;\n }\n else if (s[i] == 'r') \n {\n Outputlist[3]++;\n }\n else if (s[i] == 'o') \n {\n Outputlist[1]++;\n }\n else if (s[i] == 'i') \n {\n Outputlist[9]++;\n }\n }\n Outputlist[7] -= Outputlist[6];\n Outputlist[5] -= Outputlist[7];\n Outputlist[3] -= Outputlist[0]+Outputlist[4];\n Outputlist[1] -= Outputlist[0] + Outputlist[2] + Outputlist[4];\n Outputlist[9] -= Outputlist[5] + Outputlist[6] + Outputlist[8];\n for (int j = 0; j < 10; j++)\n {\n //printf (\"%d\",Outputlist[j]);\n for (int k = 0; k < Outputlist[j]; k++)\n {\n string_output.append(to_string(j));\n }\n \n }\n \n return string_output;\n }\n};", "memory": "12500" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n int n=s.size();\n vector<int> v(26,0);\n\n for(int i=0;i<n;i++){\n v[s[i]-'a']++;\n }\n\n vector<int> ans(10,0);\n\n ans[0]=v['z'-'a'];\n ans[2]=v['w'-'a'];\n ans[4]=v['u'-'a'];\n ans[6]=v['x'-'a'];\n ans[8]=v['g'-'a'];\n ans[1]=v['o'-'a']-ans[0]-ans[2]-ans[4];\n ans[3]=v['t'-'a']-ans[2]-ans[8];\n ans[5]=v['f'-'a']-ans[4];\n ans[7]=v['s'-'a']-ans[6];\n ans[9]=v['i'-'a']-ans[5]-ans[6]-ans[8];\n\n string str=\"\";\n\n for(int i=0;i<10;i++){\n if(ans[i]!=0){\n while(ans[i]!=0){\n str.append(to_string(i));\n ans[i]--;\n }\n }\n }\n\n return str;\n }\n};", "memory": "12600" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n int n=s.size();\n vector<int> v(26,0);\n\n for(int i=0;i<n;i++){\n v[s[i]-'a']++;\n }\n\n vector<int> ans(10,0);\n\n ans[0]=v['z'-'a'];\n ans[2]=v['w'-'a'];\n ans[4]=v['u'-'a'];\n ans[6]=v['x'-'a'];\n ans[8]=v['g'-'a'];\n ans[1]=v['o'-'a']-ans[0]-ans[2]-ans[4];\n ans[3]=v['t'-'a']-ans[2]-ans[8];\n ans[5]=v['f'-'a']-ans[4];\n ans[7]=v['s'-'a']-ans[6];\n ans[9]=v['i'-'a']-ans[5]-ans[6]-ans[8];\n\n string str=\"\";\n\n for(int i=0;i<10;i++){\n if(ans[i]!=0){\n while(ans[i]!=0){\n str.append(to_string(i));\n ans[i]--;\n }\n }\n }\n\n return str;\n }\n};", "memory": "12700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<int> count(26);\n\n for( char ch: s){\n count[ch-'a']++;\n }\n\n vector<int> nums(10);\n //unique cases\n nums[0]=count['z'-'a'];\n nums[2]=count['w'-'a'];\n nums[4]=count['u'-'a'];\n nums[6]=count['x'-'a'];\n nums[8]=count['g'-'a'];\n //derived cases\n nums[1]=count['o'-'a']-nums[0]-nums[2]-nums[4];\n nums[3]=count['h'-'a']-nums[8];\n nums[5]=count['f'-'a']-nums[4];\n nums[7]=count['s'-'a']-nums[6];\n nums[9]=count['i'-'a']-nums[6]-nums[8]-nums[5];\n\n string ans=\"\";\n for(int i=0;i<10;i++){\n while(nums[i]--){\n ans+=to_string(i);\n }\n }\n return ans;\n }\n};", "memory": "12800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<string> words = {\"zero\",\"two\",\"four\",\"six\",\"eight\",\"one\",\"three\",\"five\",\"seven\",\"nine\"};\n vector<int> nums = {0,2,4,6,8,1,3,5,7,9};\n vector<int> distinct = {'z','w','u','x','g','o','r','f','v','i'};\n vector<int> count(26,0);\n string ans=\"\";\n for(int i=0;i<s.length();i++){\n count[s[i]-'a']++;\n }\n for(int i=0;i<10;i++){\n int cnt = count[distinct[i]-'a'];\n for(int j=0;j<words[i].size();j++){\n count[words[i][j]-'a']-=cnt;\n }\n while(cnt--){\n ans+=(to_string(nums[i]));\n }\n }\n sort(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "12800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n unordered_map<char, int> chars;\n vector<int> result(10, 0);\n string resultString;\n for (auto ch : s) {\n chars[ch]++;\n }\n while (chars['z'] > 0) {\n chars['z']--;\n chars['r']--;\n chars['o']--;\n result[0]++;\n }\n while (chars['w'] > 0) {\n chars['w']--;\n chars['o']--;\n result[2]++;\n }\n while (chars['u'] > 0) {\n chars['f']--;\n chars['o']--;\n chars['u']--;\n chars['r']--;\n result[4]++;\n }\n while (chars['o'] > 0) {\n chars['o']--;\n chars['n']--;\n result[1]++;\n }\n while (chars['r'] > 0) {\n chars['r']--;\n result[3]++;\n }\n while (chars['f'] > 0) {\n chars['f']--;\n result[5]++;\n }\n while (chars['x'] > 0) {\n chars['s']--;\n chars['x']--;\n result[6]++;\n }\n while (chars['s'] > 0) {\n chars['s']--;\n chars['n']--;\n result[7]++;\n }\n while (chars['g'] > 0) {\n chars['g']--;\n result[8]++;\n }\n while (chars['n'] > 0) {\n chars['n']-=2;\n result[9]++;\n }\n for (int i = 0; i < result.size(); i++) {\n while (result[i] > 0) {\n resultString+=to_string(i);\n result[i]--;\n }\n }\n return resultString;\n }\n};", "memory": "12900" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution \n{\nprivate:\n struct WordDigitPair\n {\n std::string_view word;\n int digit = 0;\n };\n\n // Sorted in order of unique letters and length\n const std::vector<WordDigitPair> s_wordDigitPairs = \n {\n {\"zero\", 0},\n {\"two\", 2},\n {\"six\", 6},\n {\"eight\", 8},\n {\"seven\", 7},\n {\"three\", 3},\n {\"four\", 4},\n {\"five\", 5},\n {\"nine\", 9},\n {\"one\", 1},\n };\n\npublic:\n string originalDigits(string& input)\n {\n std::map<char, int> mapCharCount;\n for(char c: input)\n {\n mapCharCount[c] += 1;\n }\n\n // Get the digits from the string\n std::vector<int> digits;\n for(const auto& wordDigitPair: s_wordDigitPairs)\n {\n int minCharsCount = std::numeric_limits<int>::max();\n for(char c: wordDigitPair.word)\n {\n minCharsCount = std::min(mapCharCount[c], minCharsCount);\n }\n for(char c: wordDigitPair.word)\n {\n mapCharCount[c] -= minCharsCount;\n }\n for(int i = 0; i < minCharsCount; ++i)\n {\n digits.push_back(wordDigitPair.digit);\n }\n }\n\n std::sort(digits.begin(), digits.end());\n\n std::string& output = input;\n\n // Re-use the memory allocated for the input\n output.clear();\n for(int digit: digits)\n {\n char digitChar = '0' + digit;\n output += digitChar;\n } \n return output;\n }\n};", "memory": "13000" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <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[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution \n{\nprivate:\n struct WordDigitPair\n {\n std::string_view word;\n int digit = 0;\n };\n\n // Sorted in order of unique letters and length\n const std::vector<WordDigitPair> s_wordDigitPairs = \n {\n {\"zero\", 0},\n {\"two\", 2},\n {\"six\", 6},\n {\"eight\", 8},\n {\"seven\", 7},\n {\"three\", 3},\n {\"four\", 4},\n {\"five\", 5},\n {\"nine\", 9},\n {\"one\", 1},\n };\n\npublic:\n string originalDigits(string& input)\n {\n std::map<char, int> mapCharCount;\n for(char c: input)\n {\n mapCharCount[c] += 1;\n }\n\n // Get the digits from the string\n std::vector<int> digits;\n for(const auto& wordDigitPair: s_wordDigitPairs)\n {\n int minCharsCount = std::numeric_limits<int>::max();\n for(char c: wordDigitPair.word)\n {\n minCharsCount = std::min(mapCharCount[c], minCharsCount);\n }\n for(char c: wordDigitPair.word)\n {\n mapCharCount[c] -= minCharsCount;\n }\n for(int i = 0; i < minCharsCount; ++i)\n {\n digits.push_back(wordDigitPair.digit);\n }\n }\n\n std::sort(digits.begin(), digits.end());\n\n std::string output;\n for(int digit: digits)\n {\n char digitChar = '0' + digit;\n output += digitChar;\n } \n return output;\n }\n};", "memory": "13100" }