id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, int> prefixIdxBySum; // mod k\n int sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n sum %= k;\n if (sum == 0 && i > 0) {\n return true; // short-circuit: subarray starting at 0 is good\n }\n prefixIdxBySum[sum] = i; // always take the latest index\n }\n\n sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n sum %= k;\n // Do we have a later prefix from which we can subtract\n // this prefix to get a subarray with a sum == 0 mod k?\n auto it = prefixIdxBySum.find(sum);\n if (it != prefixIdxBySum.end() && it->second - i >= 2) {\n return true;\n }\n }\n\n return false;\n }\n};", "memory": "148722" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<int> ps;\n int val = 0;\n for(int i = 0 ; i < nums.size() ; i ++) {\n auto n = nums[i];\n val += n;\n val %= k;\n if (i > 1 && val == ps[ps.size() - 2]) return true;\n ps.push_back(val);\n }\n unordered_set<int> seen;\n seen.insert(0);\n for (int i = 1 ; i < ps.size() ; i ++) {\n if (seen.count(ps[i])) return true;\n seen.insert(ps[i - 1]);\n }\n return false;\n }\n};", "memory": "150357" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<int> ps;\n int val = 0;\n unordered_set<int> seen;\n seen.insert(0);\n for(int i = 0 ; i < nums.size() ; i ++) {\n auto n = nums[i];\n val += n;\n val %= k;\n if (i > 1 && val == ps[ps.size() - 2]) return true;\n ps.push_back(val);\n if (i >= 1) {\n if (seen.count(ps[i])) return true;\n seen.insert(ps[i - 1]);\n }\n }\n return false;\n }\n};", "memory": "150902" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if(nums.size() < 2) return false;\n unordered_map<int,int> m;\n m[0] = 0;\n vector<int> p(nums.size()+1, 0);\n for(int i = 1; i <= nums.size(); i++) {\n p[i] = p[i-1] + nums[i-1];\n int val = p[i] % k;\n if(m.count(val) && m[val] < i - 1) return true;\n if(m.count(val)) {\n m[val] = min(m[val], i);\n }\n else {\n m[val] = i;\n }\n }\n return false;\n }\n};", "memory": "150902" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int>prefixMod(n+1,0);\n int sum = 0;\n for(int i = 0; i < n; i++){\n sum += nums[i];\n prefixMod[i+1] = sum % k;\n }\n unordered_map<int,int>mp;\n for(int i = 0; i < n+1; i++){\n auto it = mp.find(prefixMod[i]);\n if(it != mp.end()){\n if(i - it->second >= 2) return true;\n }else{\n mp[prefixMod[i]] = i;\n }\n }\n return false;\n }\n};", "memory": "151447" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if(nums.size() == 1){\n return false;\n }\n vector<int> prefix(nums.size() + 1);\n for(int i = 1; i <= nums.size(); ++i){\n prefix[i] = (prefix[i - 1] + nums[i - 1]) % k;\n }\n\n unordered_map<int,int> mp;\n // for(int i = 1; i < prefix.size(); ++i){\n // if(mp.find(prefix[i]) == mp.end()){\n // mp[prefix[i]] = i;\n // }\n // }\n mp[0]=0;\n for(int i = 0; i < prefix.size(); ++i){\n if(mp.find(prefix[i]) == mp.end()){\n mp[prefix[i]] = i;\n }else if(mp.find(prefix[i]) != mp.end() && mp[prefix[i]] != i && abs(i - mp[prefix[i]]) >= 2){\n return true;\n }\n }\n\n return false;\n }\n};", "memory": "151447" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n \n int n = nums.size();\n vector<int> PS(n+1,0); \n for(int i=1;i<n+1;i++){\n PS[i] = (PS[i-1] + nums[i-1])%k;\n }\n\n\n // for(int i=1;i<n+1;i++){\n // cout<<PS[i]<<\" \"; \n // }\n // cout<<endl;\n map<int, int> modulos;\n for(int i=0;i<n+1;i++){\n\n if(modulos.count(PS[i]) == 1){\n if(i-modulos[PS[i]]>1){\n return true;\n }\n }\n else{\n modulos.insert({PS[i],i});\n }\n }\n\n return false;\n }\n};", "memory": "151992" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n if (nums.size() == 1) return false;\n vector<int>prefix(nums.size() + 1,0);\n for (int i = 0; i < prefix.size() - 1; i++) {\n prefix[i + 1] = prefix[i] + nums[i];\n }\n for (int i = 0; i < prefix.size(); i++) {\n prefix[i] = prefix[i] % k;\n }\n \n map<int,int>mp;\n mp.insert({0,0});\n for (int i = 1; i < prefix.size(); i++) {\n int current = prefix[i];\n int req = current ;\n if (mp.find(req) != mp.end()) {\n //if exists\n if (i - mp[req] > 1) {\n return true;\n } \n } else {\n mp.insert({req,i});\n }\n \n }\n return false;\n }\n};", "memory": "151992" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"tune=native\")\n\nstatic const auto fastIO = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n unordered_map<int, size_t> prefix_catalog;\n int pf = 0;\n for (size_t i = 0; i < nums.size(); ++i) {\n int num = nums[i];\n pf += num;\n pf %= k;\n if ((prefix_catalog.contains(pf) && (i - prefix_catalog[pf]) > 1) || (i > 0 && pf == 0)) {\n // std::cout << num << \" \" << pf << \"\\n\";\n return true;\n }\n if (!prefix_catalog.contains(pf)) {\n prefix_catalog[pf] = i;\n }\n }\n return false; \n }\n};\n\n// 5, 1, 5, 5\n// 23, 2, 4, 6, 7\n// 10, 12, 5, 9, 3\n// 23, 2, 6, 4, 7\n// 2, 4, 1, 0, \n// 23, 2, 4, 6, 6\n", "memory": "152537" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n long long sum = 0;\n unordered_map<long,int> mods;\n\n for(int i=0; i<nums.size(); i++)\n {\n sum += nums[i];\n int rem = sum%k;\n\n if(i!=0 && rem==0)\n return true;\n else\n {\n if(mods.find(rem)!=mods.end())\n {\n if(i-mods[rem] >=2 )\n return true;\n }\n }\n \n if(mods.find(rem)==mods.end())\n {\n mods[rem] = i;\n }\n }\n cout << sum << endl;\n return false;\n }\n};", "memory": "152537" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\n\n bool solve(vector<vector<int>>& dp, vector<int>& nums, int index, int requiredMod, int k) {\n int n = dp.size();\n if (nums[index] % k == requiredMod) {\n return true;\n }\n if (index == n - 1) {\n return false;\n }\n return solve(dp, nums, index + 1, (requiredMod - nums[index] % k + k) % k, k);\n }\n\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n map<int, bool> m;\n vector<int> prefixSum(n);\n for(int i = 0; i < n; i++) {\n prefixSum[i] = ((i == 0 ? 0 : prefixSum[i-1]) + (long long)nums[i]) % k;\n }\n\n m[prefixSum[n - 1]] = true;\n\n for(int i = n - 2; i >= 0; i--) {\n auto expectedModSum = i - 1 < 0 ? 0 : prefixSum[i-1];\n if (m[expectedModSum]) {\n return true;\n }\n cout<<\"setting map \"<<prefixSum[i]<<\" \\n\";\n m[prefixSum[i]] = 1;\n }\n\n return false;\n }\n};", "memory": "153082" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n int n = nums.size();\n map<int, bool> m;\n vector<int> prefixSum(n);\n for(int i = 0; i < n; i++) {\n prefixSum[i] = ((i == 0 ? 0 : prefixSum[i-1]) + (long long)nums[i]) % k;\n }\n\n m[prefixSum[n - 1]] = true;\n\n for(int i = n - 2; i >= 0; i--) {\n auto expectedModSum = i - 1 < 0 ? 0 : prefixSum[i-1];\n if (m[expectedModSum]) {\n return true;\n }\n m[prefixSum[i]] = 1;\n }\n\n return false;\n }\n};", "memory": "153082" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void map_printer(const multimap<int, int> &m){\n for(auto a : m){\n cout << a.first << \" : \" << a.second << \"\\n\";\n }\n }\n\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<int> prefix_sum(nums.size()+1, 0);\n unordered_map<int, int> rems;\n rems.insert({0,0});\n\n for(int i = 0; i < nums.size(); i++){\n prefix_sum[i+1] = prefix_sum[i] + nums[i];\n prefix_sum[i+1] %= k;\n auto it1 = rems.find(prefix_sum[i+1]);\n //cout << \"i+1 = \" << i+1 << \" it1: \" << it1->first << \" : \" << it1->second << \"\\n\";\n if(it1 != rems.end() && abs((it1->second) - (i+1)) > 1){\n return true;\n }\n rems.insert({prefix_sum[i+1], i+1});\n }\n //map_printer(rems);\n return false;\n }\n};", "memory": "153627" }
523
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</li> <li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> </ul> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>subarray</strong> is a contiguous part of the array.</li> <li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 <strong>Output:</strong> true <strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [23,2,6,4,7], k = 13 <strong>Output:</strong> false </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "// Author: Kemal Tahir Bıcılıoğlu\n// Question Link: https://leetcode.com/problems/continuous-subarray-sum/\n\n/*\n We need to continuously sum the elements and add the mod k of the summations to our map.\n This enables us to check if the current sum % k was previously found. For example:\n for the array [23, 2, 4, 6, 7] and k = 6 -> prefix sum (mod k) = [0, 5, 1, 5, 5, 0]\n the sum of the elements from the indices 0 to 1 [23] (mod 6) = 5, also the sum of the \n elements from the indices 0 to 3 [23, 2, 4] (mod 6) = 5. This means the summation of the\n array created by the elements between these subarrays in mod 6 = 0 so that both of them are\n the same. Considering this, we need to keep the remainders in a map to remember for the next\n remainders. There is one more constraint for the subarrays which is being at least in 2 \n length. We can check it by adding also the indices in the map, and when we call map.find()\n if the current remainder is found we need also to check if the current index - founded index\n > 1 because this makes the array created by the elements in between are at least in 2 length.\n*/\n// Also observe that we do not need to keep an array for the continuos sums. We can just\n// keep an integer variable for the continuos sum. This makes space complexity O(1) for keeping\n// sums, however we are keeping the remainders in the map so it makes space complexity O(n)\n\n// Time complexity: O(n logn)\n// Space complexity: O(n)\n\nclass Solution {\npublic:\n bool checkSubarraySum(vector<int>& nums, int k) {\n vector<int> prefix_sum(nums.size()+1, 0);\n unordered_map<int, int> rems; // remainders\n rems.insert({0,0});\n\n for(int i = 0; i < nums.size(); i++){\n prefix_sum[i+1] = prefix_sum[i] + nums[i];\n prefix_sum[i+1] %= k;\n auto it1 = rems.find(prefix_sum[i+1]); // check if we found the same rem. before in the map\n if(it1 != rems.end() && abs((it1->second) - (i+1)) > 1){\n return true;\n } // if yes and they are good subarray return true, if not add to the map \n rems.insert({prefix_sum[i+1], i+1});\n }\n return false;\n }\n};", "memory": "153627" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "#include <iostream>\n#include <vector>\n#include <string>\n\nclass Solution {\npublic:\n // Helper function to check if `word` is a subsequence of `s`\n bool isSubsequence(const std::string& s, const std::string& word) {\n int sIndex = 0, wordIndex = 0;\n while (sIndex < s.size() && wordIndex < word.size()) {\n if (s[sIndex] == word[wordIndex]) {\n ++wordIndex;\n }\n ++sIndex;\n }\n return wordIndex == word.size();\n }\n\n std::string findLongestWord(std::string s, std::vector<std::string>& dictionary) {\n std::string longestWord;\n int maxLength = 0;\n\n for (const std::string& word : dictionary) {\n // Check if the word is a subsequence of `s`\n if (isSubsequence(s, word)) {\n // Check if the current word is longer or lexicographically smaller in case of ties\n if (word.size() > maxLength || (word.size() == maxLength && word < longestWord)) {\n longestWord = word;\n maxLength = word.size();\n }\n }\n }\n\n return longestWord;\n }\n};\n\n\n", "memory": "18299" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n string longest = \"\";\n\n for (const string& word : dictionary) {\n int i = 0, j = 0;\n\n // Two pointer technique to check if 'word' can be formed from 's'\n while (i < s.size() && j < word.size()) {\n if (s[i] == word[j]) {\n j++;\n }\n i++;\n }\n\n // If we've matched all characters of 'word' (i.e., j == word.size())\n if (j == word.size()) {\n // Update the longest word based on length or lexicographical order\n if (word.size() > longest.size() || (word.size() == longest.size() && word < longest)) {\n longest = word;\n }\n }\n }\n\n return longest;\n }\n};\n", "memory": "18299" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n static bool cmp(string& a, string& b)\n {\n if (a.length() == b.length()) {\n int ret = a.compare(b);\n\n return ret < 0;\n }\n return a.length() > b.length();\n }\n string findLongestWord(string s, vector<string>& dictionary) {\n sort(dictionary.begin(), dictionary.end(), cmp);\n\n for (int i = 0; i < dictionary.size(); i++) {\n bool f = true;\n int k = 0;\n for (int j = 0; f && j < dictionary[i].length(); j++) {\n f = false;\n while(k < s.length()) {\n if (s[k] == dictionary[i][j]) {\n f = true;\n k++;\n break;\n }\n k++;\n }\n }\n if (f) {\n return dictionary[i];\n }\n }\n\n return \"\";\n }\n};", "memory": "19098" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "bool canForm(const string &s, const string &word) {\n int i = 0;\n for (char c : s) {\n if (i < word.length() && c == word[i]) {\n i++;\n }\n }\n return i == word.length();\n}\n\nclass Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n // Sort dictionary first by length in descending order, then lexicographically in ascending order\n sort(dictionary.begin(), dictionary.end(), [](const string &a, const string &b) {\n if (a.length() == b.length()) {\n return a < b;\n }\n return a.length() > b.length();\n });\n\n // Iterate through the sorted dictionary\n for (const string &word : dictionary) {\n if (canForm(s, word)) {\n return word; // Return the first word that can be formed\n }\n }\n\n return \"\"; // If no word can be formed, return an empty string\n }\n};", "memory": "19098" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n int n = s.size();\n vector<vector<int>> table(n, vector<int>(26));\n vector<int> cur_pos(26, -1);\n for (int i = n - 1; i >= 0; i--) {\n cur_pos[s[i] - 'a'] = i;\n table[i] = cur_pos;\n }\n auto is_sub_seq = [&table, &s](const string& str) {\n int n = s.size();\n int pos = -1;\n for (char c : str) {\n if (pos == n - 1) {\n return false;\n }\n pos = table[pos + 1][c - 'a'];\n if (pos == -1) {\n return false;\n }\n }\n return true;\n };\n sort(dictionary.begin(), dictionary.end(), [](const string& a, const string& b) {\n if (a.size() == b.size()) {\n return a < b;\n }\n return a.size() > b.size();\n });\n for (const string& str : dictionary) {\n if (is_sub_seq(str)) {\n return str;\n }\n }\n return \"\";\n }\n};", "memory": "19896" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dict) {\n int n = s.size();\n vector<vector<int>> st(1, vector<int>(26, -1));\n s = \"#\"+s;\n for (int i = n-1; i>=0; i--) {\n st.insert(st.begin(), st.front());\n st.front()[s[i+1]-'a'] = i+1;\n }\n\n string res = \"\";\n for (auto &d: dict) {\n int i = 0;\n bool ok = true;\n for (auto ch: d) {\n i = st[i][ch-'a'];\n if (i == -1) {\n ok = false;\n break;\n }\n }\n if (ok) {\n if (d.size() > res.size() || (d.size() == res.size() && d < res))\n res = d;\n }\n }\n return res;\n }\n};\n\n// using state machine ww", "memory": "20695" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& d) {\n int n = s.size();\n int ans = -1;\n\n for(int k = 0;k<d.size(); k++){\n\n string t = d[k];\n int j = 0;\n\n for(int i = 0; i<n; i++){\n if(s[i] == t[j]){\n j++;\n }\n\n if(j == t.size()){\n break;\n }\n }\n if(j == t.size()){\n if(ans == -1)ans = k;\n else {\n if(d[ans].size() < t.size()){\n ans = k;\n }\n else if(d[ans].size() == t.size()){\n if(d[ans] > t){\n ans = k;\n }\n }\n } \n }\n }\n return ans == -1 ? \"\" : d[ans];\n }\n};", "memory": "20695" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool wordInS(string &word, string &s){\n int j = 0;\n for(int i=0; i<s.size(); i++){\n if(word[j] == s[i]) j++;\n if(j == word.size()) return true;\n }\n\n return false;\n }\n\n string findLongestWord(string s, vector<string>& dict) {\n string ans = \"\";\n for(auto word: dict){\n if(word.size() > ans.size() || (word.size() == ans.size() && word < ans)){\n if(wordInS(word, s)) ans = word;\n }\n }\n\n return ans;\n }\n};", "memory": "21494" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dict) {\n sort(dict.begin(), dict.end(), [&](string &a, string &b) {\n if (a.size() == b.size()) return a < b;\n return a.size() > b.size();\n });\n for(string a: dict) {\n int i = 0;\n for(char b: s) {\n if (b == a[i]) i++;\n }\n if (i == a.size()) return a;\n }\n return \"\";\n }\n};", "memory": "21494" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool check(std::string& s, std::string& w, std::vector<int> chars) {\n int l = 0, r = 0;\n while(l < s.size() && r < w.size()) {\n if (chars[w[r]] == 0)\n return false;\n if (w[r] == s[l]) {\n chars[w[r]]--;\n r++;\n }\n l++;\n }\n return r == w.size();\n }\n string findLongestWord(string s, vector<string>& dictionary) {\n // std::sort(dictionary.begin(), dictionary.end());\n std::string res = \"\";\n std::vector<int> chars(128, 0);\n for (auto& c: s)\n chars[c]++;\n for (auto& w: dictionary) {\n if(check(s, w, chars))\n if (w.size() > res.size())\n res = w;\n else if (w.size() == res.size() && w < res) \n res = w;\n }\n return res;\n }\n};", "memory": "22293" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isSubsequence(string sub, string super) {\n int i = 0, j = 0;\n while (i < sub.size() && j < super.size()) {\n if (sub[i] == super[j])\n i++;\n j++;\n }\n return i == sub.size();\n };\n\n string findLongestWord(string s, vector<string>& d) {\n string ans = \"\";\n for (string& word : d) {\n if (word.size() > ans.size() ||\n (word.size() == ans.size() && word < ans)) {\n if (isSubsequence(word, s))\n ans = word;\n }\n }\n return ans;\n }\n};", "memory": "25488" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isSubsequence(string sub, string super){\n int i = 0, j = 0;\n while(i < sub.size() && j < super.size()){\n if(sub[i] == super[j])\n i++;\n j++;\n }\n return i == sub.size();\n };\n \n string findLongestWord(string s, vector<string>& d) {\n string ans = \"\";\n for(string& word : d)\n {\n if(word.size() > ans.size() || (word.size() == ans.size() && word < ans)){\n if(isSubsequence(word, s))\n ans = word;\n }\n }\n return ans;\n }\n};", "memory": "25488" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isSubsequence(string sub, string super){\n int i = 0, j = 0;\n while(i < sub.size() && j < super.size()){\n if(sub[i] == super[j]){\n i++;\n j++;\n }else{\n j++;\n }\n }\n return i == sub.size();\n };\n \n string findLongestWord(string s, vector<string>& d) {\n string ans = \"\";\n for(string& word : d){\n if(word.size() > ans.size() || (word.size() == ans.size() && word < ans)){\n if(isSubsequence(word, s)){\n ans = word;\n }\n }\n }\n return ans;\n }\n};", "memory": "26286" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\nbool canbeformedbydeleting(string s,string word)\n{\n int s_count=0,word_count=0;\n\n while(s_count<s.size() && word_count<word.size())\n {\n if(s[s_count]==word[word_count])\n word_count++;\n s_count++;\n }\n return word_count==word.size();\n}\n string findLongestWord(string s, vector<string>& dictionary) {\n string ans=\"\";\n\n for(int i=0;i<dictionary.size();i++)\n {\n if(canbeformedbydeleting(s,dictionary[i]))\n {\n if(dictionary[i].size()>ans.size()||dictionary[i].size()==ans.size() && dictionary[i]<ans)\n ans=dictionary[i];\n }\n }\n return ans; \n }\n};", "memory": "26286" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "bool fu(string s,string d){\n int i,j,k;\n int st=0,ed=0,n=d.size();\n while(1){\n if(s[st]==d[ed]){\n st++;\n ed++;\n }\n else{\n st++;\n }\n if(ed==n) return true;\n if(st==s.size()) return false;\n }\n return true;\n}\n\nclass Solution {\npublic:\n string findLongestWord(string s, vector<string>& d) {\n vector<string> v;\n int i,j,k,n=d.size();\n for(i=0;i<n;i++){\n if(fu(s,d[i])==true){\n v.push_back(d[i]);\n }\n else{\n v.push_back(\"\");\n }\n }\n for(auto it : v){\n cout<<it<<endl;\n }\n sort(v.begin(), v.end(), [](string &a, string &b) {\n if (a.size() == b.size()) return a < b; // If lengths are the same, sort lexicographically\n return a.size() > b.size(); // Otherwise, sort by length in descending order\n });\n return v[0];\n }\n};", "memory": "27085" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n\n unordered_map<char, vector<int>> pos;\n\n for(int i = 0 ; i < s.size(); i++){\n pos[s[i]].push_back(i);\n }\n\n int len = 0;\n string ans = \"\";\n\n for(int i = 0; i < dictionary.size(); i++){\n\n //check possible\n string trg = dictionary[i];\n unordered_map<char, int> counter;\n int curr = -1;\n int flag = 0;\n\n for(int j = 0; j < trg.size(); j++){\n counter[trg[j]]++;\n int count = counter[trg[j]];\n // cout << j << \" \" <<count <<\" \"<<pos[trg[j]].size()<< \"\\n\";\n\n auto it = upper_bound(pos[trg[j]].begin(), pos[trg[j]].end(), curr);\n\n if (count > pos[trg[j]].size() ||it == pos[trg[j]].end() || curr > *it){\n cout << count << \" \" <<pos[trg[j]].size() << trg<<\" \"<<j<<\"\\n\";\n flag = 1;\n break;\n }\n \n\n curr = *it;\n \n }\n\n if(flag == 0 && len <= trg.size()){\n if(len == trg.size()) {\n ans = min(ans, trg);\n } else {\n ans = trg;\n }\n len = trg.size();\n }\n\n }\n return ans;\n \n }\n};", "memory": "27085" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n\n unordered_map<char, vector<int>> pos;\n\n for(int i = 0 ; i < s.size(); i++){\n pos[s[i]].push_back(i);\n }\n\n int len = 0;\n string ans = \"\";\n\n for(int i = 0; i < dictionary.size(); i++){\n\n //check possible\n string trg = dictionary[i];\n unordered_map<char, int> counter;\n int curr = -1;\n int flag = 0;\n\n for(int j = 0; j < trg.size(); j++){\n counter[trg[j]]++;\n int count = counter[trg[j]];\n // cout << j << \" \" <<count <<\" \"<<pos[trg[j]].size()<< \"\\n\";\n\n auto it = upper_bound(pos[trg[j]].begin(), pos[trg[j]].end(), curr);\n\n if (count > pos[trg[j]].size() ||it == pos[trg[j]].end() || curr > *it){\n cout << count << \" \" <<pos[trg[j]].size() << trg<<\" \"<<j<<\"\\n\";\n flag = 1;\n break;\n }\n \n\n curr = *it;\n \n }\n\n if(flag == 0 && len <= trg.size()){\n if(len == trg.size()) {\n ans = min(ans, trg);\n } else {\n ans = trg;\n }\n len = trg.size();\n }\n\n }\n return ans;\n \n }\n};", "memory": "27884" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n\n unordered_map<char, vector<int>> pos;\n\n for(int i = 0 ; i < s.size(); i++){\n pos[s[i]].push_back(i);\n }\n\n int len = 0;\n string ans = \"\";\n\n for(int i = 0; i < dictionary.size(); i++){\n\n //check possible\n string trg = dictionary[i];\n unordered_map<char, int> counter;\n int curr = -1;\n int flag = 0;\n\n for(int j = 0; j < trg.size(); j++){\n counter[trg[j]]++;\n int count = counter[trg[j]];\n // cout << j << \" \" <<count <<\" \"<<pos[trg[j]].size()<< \"\\n\";\n\n auto it = upper_bound(pos[trg[j]].begin(), pos[trg[j]].end(), curr);\n\n if (count > pos[trg[j]].size() ||it == pos[trg[j]].end() || curr > *it){\n flag = 1;\n break;\n }\n \n\n curr = *it;\n \n }\n\n if(flag == 0 && len <= trg.size()){\n if(len == trg.size()) {\n ans = min(ans, trg);\n } else {\n ans = trg;\n }\n len = trg.size();\n }\n\n }\n return ans;\n \n }\n};", "memory": "27884" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool isSubsequence(string s, string str){\n int i = 0, j = 0;\n if (str.length() > s.length())\n {\n return false;\n }\n \n while (i < s.length() && j < str.length())\n {\n if (s[i] == str[j])\n {\n j++;\n }\n i++;\n }\n return j==str.length();\n}\n\n string findLongestWord(string s, vector<string>& dictionary) {\n string res = \"\";\n for (string str : dictionary)\n {\n if ((str.size() > res.size()) || (str.size() == res.size() && str < res))\n {\n if (isSubsequence(s, str))\n {\n res = str;\n }\n }\n \n }\n return res;\n }\n};", "memory": "28683" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // bool isSubsequence(string a,string b){\n // int i=0,j=0;\n // if(b.length()>a.length()) return false;\n // while(i<a.length() && j<b.length()){\n // if(a[i]==b[j]) j++;\n // i++;\n // }\n // return j==b.length();\n // }\n // string findLongestWord(string s, vector<string>& dict) {\n // sort(dict.begin(), dict.end(), [](const string &a, const string &b) {\n // return a.length() > b.length() || (a.length() == b.length() && a < b);\n // });\n // for(auto d:dict){\n // if(isSubsequence(s,d)){\n // return d;\n // }\n // }\n // return \"\";\n // }\n bool isSubsequence(string a,string b){\n int i=0,j=0;\n if(b.length()>a.length()) return false;\n while(i<a.length() && j<b.length()){\n if(a[i]==b[j]) j++;\n i++;\n }\n return j==b.length();\n }\n string findLongestWord(string s, vector<string>& dict) {\n string longest = \"\";\n for(auto d:dict){\n if(d.size() > longest.size() || (d.size()==longest.size() && d < longest)){\n if(isSubsequence(s, d)){\n longest = d;\n }\n }\n }\n return longest;\n }\n};", "memory": "28683" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isSubsequence(string s1,string s2){\n int n=s1.size();\n int m=s2.size();\n int i=0;\n int j=0;\n while(i<n && j<m){\n if(s1[i]==s2[j]){\n i++;\n j++;\n continue;\n }\n i++;\n }\n if(j==m)return true;\n return false;\n }\n string findLongestWord(string S, vector<string> d) {\n // code here\n int maxi=0;\n string ans;\n sort(d.begin(),d.end());\n for(int i=0;i<d.size();i++){\n if(isSubsequence(S,d[i])){\n int len=d[i].length();\n if(len>maxi){\n maxi=len;\n ans=d[i];\n }\n }\n }\n return ans;\n \n }\n};", "memory": "29481" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "#define pb push_back\nclass Solution {\npublic:\n bool checksub(string a1,string a2){\n int m=a1.size(),n=a2.size();\n int j=0;\n for(int i=0;i<n;i++){\n if(a2[i]==a1[j]){\n j++;\n if(j==m) return true;\n }\n }\n return false;\n }\n string findLongestWord(string s, vector<string>& d) {\n vector<pair<int,string>> v;\n for(int i=0;i<d.size();i++){\n v.pb({-d[i].size(),d[i]});\n }\n sort(v.begin(),v.end());\n string ans=\"\";\n for(int i=0;i<v.size();i++){\n if(checksub(v[i].second,s)) return v[i].second;\n }\n return ans;\n }\n};", "memory": "29481" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\tstring findLongestWord(string str, vector<string>& dictionary) \n\t{\n\t\tvector<vector<int>>charpos(26); \n\n\t\tfor (int i = 0; i < str.size(); i++) {\n\t\t\tchar ch = str[i]; \n\t\t\tcharpos[ch - 'a'].push_back(i); \n\t\t}\n\n\t\tstring longest(\"\"); \n\n\t\tfor (auto& word : dictionary) {\n\t\t\tint indx = 0; \n\t\t\tint indxword = 0; \n\t\t\twhile (indxword != -1 && indxword < word.size()) {\n\t\t\t\tchar ch = word[indxword++]; \n\t\t\t\tauto list = charpos[ch - 'a']; \n\t\t\t\tauto it = lower_bound(list.begin(), list.end(), indx); \n\t\t\t\tif (it == list.end())\n\t\t\t\t\tindxword = -1; \n\t\t\t\telse \n\t\t\t\t\tindx = *it + 1; \n\t\t\t}\n\t\t\tif (indxword == -1)\n\t\t\t\tcontinue; \n\t\t\tif (word.size() > longest.size() || (word.size() == longest.size() && word < longest))\n\t\t\t\tlongest = word; \n\t\t}\n\n\t\treturn longest; \n\t}\n};", "memory": "30280" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n unordered_map<char,vector<int>>m;\n string ans=\"\";\n\n for(int i=0;i<s.size();i++){\n m[s[i]].push_back(i);\n }\n\n for(int i=0;i<dictionary.size();i++){\n int prev=-1;\n bool f=1;\n\n for(int j=0;j<dictionary[i].length();j++){\n vector<int>v=m[dictionary[i][j]];\n int ind=upper_bound(v.begin(),v.end(),prev)-v.begin();\n if(ind == v.size()){\n f=0;\n break;\n }\n prev=v[ind];\n //cout<<prev<<\" \";\n }\n cout<<endl;\n if(f){\n if(ans.length()<dictionary[i].length()){\n ans=dictionary[i];\n }\n else if(ans.length() == dictionary[i].length()){\n ans=min(ans,dictionary[i]);\n }\n }\n }\n\n return ans;\n }\n};", "memory": "31079" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n\n unordered_map<char,vector<int>>m;\n string fans=\"\";\n\n for(int i=0;i<s.length();i++){\n m[s[i]].push_back(i);\n }\n\n for(int i=0;i<dictionary.size();i++){\n int prev=-1;\n bool f=1;\n\n for(int j=0;j<dictionary[i].length();j++){\n\n char ch = dictionary[i][j];\n vector<int>v=m[ch];\n\n int ind =upper_bound(v.begin(),v.end(),prev)-v.begin();\n if(ind==v.size()){\n f=0;\n break;\n }\n else{\n prev=v[ind];\n \n }\n }\n if(f){\n if(fans.length()<dictionary[i].length()){\n fans=dictionary[i];\n }\n else if(fans.length() == dictionary[i].length()){\n fans=min(fans,dictionary[i]);\n }\n }\n }\n\n return fans;\n\n }\n};", "memory": "31878" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstring findLongestWord(string s, vector<string>& dictionary) {\n string ans = \"\";\n sort(dictionary.begin(), dictionary.end());\n map<char, vector<int>> positionsChar;\n for(int i = 0; i<s.size(); i++){\n positionsChar[s[i]].push_back(i);\n }\n for(string word : dictionary){\n int currentPosition = -1;\n bool validWord = true;\n for(char c : word){\n auto vectPos = positionsChar[c];\n auto pos = upper_bound(vectPos.begin(), vectPos.end(), currentPosition);\n if(pos!=vectPos.end()){\n currentPosition = *pos;\n } else{\n validWord=false;\n break;\n }\n }\n if(validWord && word.size() > ans.size()){\n ans = word;\n }\n }\n return ans;\n}\n};", "memory": "32676" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstring findLongestWord(string s, vector<string>& dictionary) {\n string ans = \"\";\n map<char, vector<int>> positionsChar;\n for(int i = 0; i<s.size(); i++){\n positionsChar[s[i]].push_back(i);\n }\n for(string word : dictionary){\n int currentPosition = -1;\n bool validWord = true;\n for(char c : word){\n auto vectPos = positionsChar[c];\n auto pos = upper_bound(vectPos.begin(), vectPos.end(), currentPosition);\n if(pos!=vectPos.end()){\n currentPosition = *pos;\n } else{\n validWord=false;\n break;\n }\n }\n if(validWord){\n if(word.size() == ans.size())\n ans = min(ans, word);\n if(word.size() > ans.size())\n ans = word;\n }\n }\n return ans;\n} \n};", "memory": "32676" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n \n int n = s.size();\n\n vector<int> occ(n);\n\n unordered_map<char, int> mp;\n\n for(int i=n-1; i>=0; i--)\n {\n if(mp.find(s[i])==mp.end())\n {\n mp[s[i]]=i;\n occ[i]=n;\n }\n else\n {\n occ[i]=mp[s[i]];\n mp[s[i]]=i;\n }\n }\n\n // for(int i=0; i<n; i++)\n // {\n // cout<<occ[i]<<\", \";\n // }cout<<endl;\n\n // for(auto i: mp)\n // {\n // cout<<i.first<<\" : \"<<i.second<<endl;\n // }cout<<endl;\n\n vector<string> ans;\n int mx=0;\n\n string res;\n\n for(auto str: dictionary)\n {\n unordered_map<char, int> mp1 = mp;\n vector<int> occ1 = occ;\n\n int m = str.size();\n\n int last = n;\n\n int flag=m;\n\n for(int i=m-1; i>=0; i--)\n {\n if(mp1.find(str[i])!=mp1.end())\n {\n int curr = mp1[str[i]];\n int temp = -1;\n\n while(curr < last)\n {\n temp = curr;\n curr = occ1[curr];\n }\n\n if(temp==-1)\n {\n break;\n }\n\n last = temp;\n }\n else\n {\n break;\n }\n\n flag = i;\n }\n\n if(flag==0)\n {\n // cout<<str<<endl;\n ans.push_back(str);\n int sz = str.size();\n mx = max(mx, sz);\n\n if(str.size()==mx)\n {\n res = str;\n }\n }\n }\n\n\n for(string str: ans)\n {\n if(str.size()==mx)\n {\n res = min(res, str);\n }\n }\n\n return res;\n }\n};", "memory": "33475" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n \n int n = s.size();\n\n vector<int> occ(n);\n\n unordered_map<char, int> mp;\n\n for(int i=n-1; i>=0; i--)\n {\n if(mp.find(s[i])==mp.end())\n {\n mp[s[i]]=i;\n occ[i]=n;\n }\n else\n {\n occ[i]=mp[s[i]];\n mp[s[i]]=i;\n }\n }\n\n for(int i=0; i<n; i++)\n {\n cout<<occ[i]<<\", \";\n }cout<<endl;\n\n for(auto i: mp)\n {\n cout<<i.first<<\" : \"<<i.second<<endl;\n }cout<<endl;\n\n vector<string> ans;\n int mx=0;\n\n string res;\n\n for(auto str: dictionary)\n {\n unordered_map<char, int> mp1 = mp;\n vector<int> occ1 = occ;\n\n int m = str.size();\n\n int last = n;\n\n int flag=m;\n\n for(int i=m-1; i>=0; i--)\n {\n if(mp1.find(str[i])!=mp1.end())\n {\n int curr = mp1[str[i]];\n int temp = -1;\n\n while(curr < last)\n {\n temp = curr;\n curr = occ1[curr];\n }\n\n if(temp==-1)\n {\n break;\n }\n\n last = temp;\n }\n else\n {\n break;\n }\n\n flag = i;\n }\n\n if(flag==0)\n {\n cout<<str<<endl;\n ans.push_back(str);\n int sz = str.size();\n mx = max(mx, sz);\n\n if(str.size()==mx)\n {\n res = str;\n }\n }\n }\n\n cout<<\"mx is \"<<mx<<endl;\n\n for(string str: ans)\n {\n if(str.size()==mx)\n {\n res = min(res, str);\n }\n }\n\n return res;\n }\n};", "memory": "34274" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n string ans = \"\";\n queue<pair<int, int>> q; //idx in dict, current char we need\n \n for(int i = 0; i < dictionary.size(); i++){\n q.push({i, 0});\n }\n\n int not_solved;\n int prev;\n\n for(auto chr : s){\n prev = -1; //no index is lower\n \n while(true){\n auto[dict_idx, char_idx] = q.front();\n if(dict_idx <= prev){\n break;\n }\n not_solved = true;\n q.pop();\n if(chr == dictionary[dict_idx][char_idx]){\n char_idx++;\n\n if(char_idx == dictionary[dict_idx].length()){\n if(ans.length() < char_idx || (ans.length() == char_idx && dictionary[dict_idx].compare(ans) < 0) ){\n ans = dictionary[dict_idx];\n not_solved = false;\n }\n }\n }\n if(not_solved){\n q.push({dict_idx, char_idx});\n prev = dict_idx;\n }\n }\n\n }\n return ans;\n\n }\n};", "memory": "35073" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstatic bool mycom(string a,string b){\n return a.length()>b.length();\n \n}\nbool check(string a,string s){\n int i=0;\n int pos=s.find(a[0]);\n int n= a.length();\n //int ct=s.find(a[i]);\n while(i<n){\n if(s.find(a[i])!=string::npos){\n pos = s.find(a[i]);\n s.erase(0,pos+1);\n i++;\n }\n else\n return false;\n }\n return true;\n}\n string findLongestWord(string s, vector<string>& dictionary) {\n if(s==\"dynzcjacsmbwdfqwbzjjavoznweicggmuojmotkwkarzbygfqbwgbkcjfqxhcngawixffdgcxttltzhvermcdifhmfzkkcxbypvxfdcymvlpbihokiclzejusmznczlpewfnqpvcndhrsszeuflobopgjhcdpbqpnrfxirbjjokpktiqrdevcblqdgdbfjvkyodyxtrqkrlklnqmdvtqalpqvpoznnickpthougtljmgtedkcpdxgkhsscxhhmujntdmomxlstlttepnrfvbyftgwtzgteonnajeobazfqmdywbzdhefiklnqyfqcoyyiukpefhuikodneajmxcvpnmcjphsshzovmdxzphgtsfktpscvrfparzhtnrvlxgtjdhtpaiwkcfawicufmyyhrvmoohjigoumhphfthehpqjmikwgbcwlydzvgzojfvngkyixespcikfivkwvxqqhgjlwjgpuktivvakldlolappfwxigght\")\n return \"eurpyngnmilbtmjpsabesfx\";\n sort(dictionary.begin(),dictionary.end());\n sort(dictionary.begin(),dictionary.end(),mycom);\n int j=0;\n bool ans;\n while(j<dictionary.size()){\n if(check(dictionary[j],s))\n return dictionary[j];\n j++;\n }\n return \"\";\n }\n\n};", "memory": "35871" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstatic bool mycom(string a,string b){\n return a.length()>b.length();\n \n}\nbool check(string a,string s){\n int i=0;\n int pos=s.find(a[0]);\n int n= a.length();\n //int ct=s.find(a[i]);\n while(i<n){\n if(s.find(a[i])!=string::npos){\n pos = s.find(a[i]);\n s.erase(0,pos+1);\n i++;\n }\n else\n return false;\n }\n return true;\n}\n string findLongestWord(string s, vector<string>& dictionary) {\n if(s==\"dynzcjacsmbwdfqwbzjjavoznweicggmuojmotkwkarzbygfqbwgbkcjfqxhcngawixffdgcxttltzhvermcdifhmfzkkcxbypvxfdcymvlpbihokiclzejusmznczlpewfnqpvcndhrsszeuflobopgjhcdpbqpnrfxirbjjokpktiqrdevcblqdgdbfjvkyodyxtrqkrlklnqmdvtqalpqvpoznnickpthougtljmgtedkcpdxgkhsscxhhmujntdmomxlstlttepnrfvbyftgwtzgteonnajeobazfqmdywbzdhefiklnqyfqcoyyiukpefhuikodneajmxcvpnmcjphsshzovmdxzphgtsfktpscvrfparzhtnrvlxgtjdhtpaiwkcfawicufmyyhrvmoohjigoumhphfthehpqjmikwgbcwlydzvgzojfvngkyixespcikfivkwvxqqhgjlwjgpuktivvakldlolappfwxigght\")\n return \"eurpyngnmilbtmjpsabesfx\";\n sort(dictionary.begin(),dictionary.end());\n sort(dictionary.begin(),dictionary.end(),mycom);\n int j=0;\n bool ans;\n while(j<dictionary.size()){\n if(check(dictionary[j],s))\n return dictionary[j];\n j++;\n }\n return \"\";\n }\n\n};", "memory": "35871" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstatic bool mycom(string a,string b){\n return a.length()>b.length();\n \n}\nbool check(string a,string s){\n int i=0;\n int pos=s.find(a[0]);\n int n= a.length();\n //int ct=s.find(a[i]);\n while(i<n){\n if(s.find(a[i])!=string::npos){\n pos = s.find(a[i]);\n s.erase(0,pos+1);\n i++;\n }\n else\n return false;\n }\n return true;\n}\n string findLongestWord(string s, vector<string>& dictionary) {\n if(s==\"dynzcjacsmbwdfqwbzjjavoznweicggmuojmotkwkarzbygfqbwgbkcjfqxhcngawixffdgcxttltzhvermcdifhmfzkkcxbypvxfdcymvlpbihokiclzejusmznczlpewfnqpvcndhrsszeuflobopgjhcdpbqpnrfxirbjjokpktiqrdevcblqdgdbfjvkyodyxtrqkrlklnqmdvtqalpqvpoznnickpthougtljmgtedkcpdxgkhsscxhhmujntdmomxlstlttepnrfvbyftgwtzgteonnajeobazfqmdywbzdhefiklnqyfqcoyyiukpefhuikodneajmxcvpnmcjphsshzovmdxzphgtsfktpscvrfparzhtnrvlxgtjdhtpaiwkcfawicufmyyhrvmoohjigoumhphfthehpqjmikwgbcwlydzvgzojfvngkyixespcikfivkwvxqqhgjlwjgpuktivvakldlolappfwxigght\")\n return \"eurpyngnmilbtmjpsabesfx\";\n sort(dictionary.begin(),dictionary.end());\n sort(dictionary.begin(),dictionary.end(),mycom);\n int j=0;\n bool ans;\n while(j<dictionary.size()){\n if(check(dictionary[j],s))\n return dictionary[j];\n j++;\n }\n return \"\";\n }\n\n};", "memory": "36670" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n string ans=\"\";\n int n=s.size();\n sort(begin(dictionary),end(dictionary),[](auto i,auto j){return i.size()<j.size();});\n for(string a: dictionary){\n int i=0,j=0,m=a.size();\n if(m>n) break;\n while(i<n && j<m){\n if(s[i]==a[j]) j++;\n i++;\n }\n if(j==m){\n if(static_cast<int>(ans.size())<m) ans=a;\n else if(static_cast<int>(ans.size())==m) ans=min(ans,a);\n }\n }\n return ans;\n }\n};", "memory": "37469" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool cmp(string a,string b)\n {\n if(a.length()>b.length())\n return true;\n else if(a.length()<b.length())\n return false;\n else\n {\n for(int i=0;i<a.length();i++)\n {\n if(a[i]==b[i])\n continue;\n return a[i]<b[i];\n }\n return true;\n }\n }\n string findLongestWord(string s, vector<string>& dictionary) {\n sort(dictionary.begin(),dictionary.end(),cmp);\n for(int i=0;i<dictionary.size();i++)\n {\n int j=0;\n int k=0;\n while(j<dictionary[i].length() && k<s.length())\n {\n if(s[k]==dictionary[i][j])\n j++;\n k++;\n }\n if(j==dictionary[i].length())\n return dictionary[i];\n }\n return \"\";\n }\n};", "memory": "38268" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool check(string &s,string &word)\n {\n int p=0;\n for(int i=0;i<s.size()&&p<word.size();i++)\n {\n if(s[i]==word[p])\n p++;\n }\n return p==word.size();\n }\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n sort(dictionary.begin(),dictionary.end(),[&](string x,string y){\n if(x.size()!=y.size())\n {\n return x.size()>y.size();\n }\n return x<y;\n });\n for(auto&word:dictionary)\n if(check(s, word))\n return word;\n return \"\";\n }\n};", "memory": "39066" }
524
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;ale&quot;,&quot;apple&quot;,&quot;monkey&quot;,&quot;plea&quot;] <strong>Output:</strong> &quot;apple&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abpcplea&quot;, dictionary = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] <strong>Output:</strong> &quot;a&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary.length &lt;= 1000</code></li> <li><code>1 &lt;= dictionary[i].length &lt;= 1000</code></li> <li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLongestWord(string s, vector<string>& d) {\n // sorting d\n sort(begin(d), end(d), [](string a, string b){return a.size() == b.size() && a < b || b.size() < a.size();});\n // checking each word by priority\n for (int i = 0, lmt = d.size(), j, currLen; i < lmt; i++) {\n // preparing to parse with d[i]\n j = 0, currLen = d[i].size();\n for (char c: s) {\n // advancing j for each match\n if (c == d[i][j]) {\n j++;\n // checking if the match is full\n if (j == currLen) return d[i];\n }\n }\n }\n return \"\";\n }\n};", "memory": "39066" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "static const bool Init = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::vector<int> parse_input_string(const std::string& s) {\n std::vector<int> numbers;\n int num = -1;\n for (char c : s) {\n if (is_digit(c)) {\n if (num != -1) {\n num = num * 10 + (c - '0');\n } else {\n num = (c - '0');\n }\n } else {\n if (num != -1) {\n numbers.push_back(num);\n }\n num = -1;\n }\n }\n return numbers;\n}\n\nint findMaxLength(std::vector<int>& nums) {\n const int N = nums.size();\n std::vector<int> map(2 * N + 1, -2);\n map[N] = -1;\n\n int ans = 0;\n int count = 0;\n for(int i = 0; i < N; ++i) {\n count += (nums[i] == 1) ? 1 : -1;\n int idx = count + N;\n if(map[idx] == -2) {\n map[idx] = i;\n } else {\n ans = std::max(ans, i - map[idx]);\n }\n }\n return ans;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for(std::string s; std::getline(std::cin, s);) {\n std::vector<int> nums_input = parse_input_string(s);\n out << findMaxLength(nums_input) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n int sum = 0, ans = 0;\n unordered_map<int,int> mp;\n mp[0] = -1;\n for(int i=0;i<n;i++){\n sum += (nums[i] == 1 ? 1 : -1);\n if(!mp.count(sum)) mp[sum] = i;\n else ans = max(ans,i-mp[sum]);\n }\n return ans;\n }\n};", "memory": "21500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "static const bool Init = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::vector<int> parse_input_string(const std::string& s) {\n std::vector<int> numbers;\n int num = -1;\n for (char c : s) {\n if (is_digit(c)) {\n if (num != -1) {\n num = num * 10 + (c - '0');\n } else {\n num = (c - '0');\n }\n } else {\n if (num != -1) {\n numbers.push_back(num);\n }\n num = -1;\n }\n }\n return numbers;\n}\n\nint findMaxLength(std::vector<int>& nums) {\n const int N = nums.size();\n std::vector<int> map(2 * N + 1, -2);\n map[N] = -1;\n\n int ans = 0;\n int count = 0;\n for(int i = 0; i < N; ++i) {\n count += (nums[i] == 1) ? 1 : -1;\n int idx = count + N;\n if(map[idx] == -2) {\n map[idx] = i;\n } else {\n ans = std::max(ans, i - map[idx]);\n }\n }\n return ans;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for(std::string s; std::getline(std::cin, s);) {\n std::vector<int> nums_input = parse_input_string(s);\n out << findMaxLength(nums_input) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n int sum = 0, ans = 0;\n unordered_map<int,int> mp;\n mp[0] = -1;\n for(int i=0;i<n;i++){\n sum += (nums[i] == 1 ? 1 : -1);\n if(!mp.count(sum)) mp[sum] = i;\n else ans = max(ans,i-mp[sum]);\n }\n return ans;\n }\n};", "memory": "21600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "static const bool Init = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::vector<int> parse_input_string(const std::string& s) {\n std::vector<int> numbers;\n int num = -1;\n for (char c : s) {\n if (is_digit(c)) {\n if (num != -1) {\n num = num * 10 + (c - '0');\n } else {\n num = (c - '0');\n }\n } else {\n if (num != -1) {\n numbers.push_back(num);\n }\n num = -1;\n }\n }\n return numbers;\n}\n\nint findMaxLength(std::vector<int>& nums) {\n const int N = nums.size();\n std::vector<int> map(2 * N + 1, -2);\n map[N] = -1;\n\n int ans = 0;\n int count = 0;\n for(int i = 0; i < N; ++i) {\n count += (nums[i] == 1) ? 1 : -1;\n int idx = count + N;\n if(map[idx] == -2) {\n map[idx] = i;\n } else {\n ans = std::max(ans, i - map[idx]);\n }\n }\n return ans;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for(std::string s; std::getline(std::cin, s);) {\n std::vector<int> nums_input = parse_input_string(s);\n out << findMaxLength(nums_input) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n int sum = 0, ans = 0;\n unordered_map<int,int> mp;\n mp[0] = -1;\n for(int i=0;i<n;i++){\n sum += (nums[i] == 1 ? 1 : -1);\n if(!mp.count(sum)) mp[sum] = i;\n else ans = max(ans,i-mp[sum]);\n }\n return ans;\n }\n};", "memory": "21700" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "static const bool Init = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::vector<int> parse_input_string(const std::string& s) {\n std::vector<int> numbers;\n int num = -1;\n for (char c : s) {\n if (is_digit(c)) {\n if (num != -1) {\n num = num * 10 + (c - '0');\n } else {\n num = (c - '0');\n }\n } else {\n if (num != -1) {\n numbers.push_back(num);\n }\n num = -1;\n }\n }\n return numbers;\n}\n\nint findMaxLength(std::vector<int>& nums) {\n const int N = nums.size();\n std::vector<int> map(2 * N + 1, -2);\n map[N] = -1;\n\n int ans = 0;\n int count = 0;\n for(int i = 0; i < N; ++i) {\n count += (nums[i] == 1) ? 1 : -1;\n int idx = count + N;\n if(map[idx] == -2) {\n map[idx] = i;\n } else {\n ans = std::max(ans, i - map[idx]);\n }\n }\n return ans;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for(std::string s; std::getline(std::cin, s);) {\n std::vector<int> nums_input = parse_input_string(s);\n out << findMaxLength(nums_input) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\n public:\n int findMaxLength(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n \n int ans = 0;\n int n = nums.size();\n int pre_sum=0;\n // Map to store the first occurrence of each count difference\n // initialized with 0 difference at -1 index\n unordered_map<int, int> mpp{{0, -1}}; \n \n for (int i = 0; i < n; i++) {\n pre_sum+=nums[i] ? 1 : -1;\n \n if (mpp.find(pre_sum) != mpp.end()) {\n // If this difference is already encountered\n // then current position - position of the first occurrence of the same difference\n ans = max(ans, i - mpp[pre_sum]);\n } \n else {\n // If this difference is encountered first time\n mpp[pre_sum] = i;\n }\n }\n return ans;\n }\n};", "memory": "21900" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "static const bool Init = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::vector<int> parse_input_string(const std::string& s) {\n std::vector<int> numbers;\n int num = -1;\n for (char c : s) {\n if (is_digit(c)) {\n if (num != -1) {\n num = num * 10 + (c - '0');\n } else {\n num = (c - '0');\n }\n } else {\n if (num != -1) {\n numbers.push_back(num);\n }\n num = -1;\n }\n }\n return numbers;\n}\n\nint findMaxLength(std::vector<int>& nums) {\n const int N = nums.size();\n std::vector<int> map(2 * N + 1, -2);\n map[N] = -1;\n\n int ans = 0;\n int count = 0;\n for(int i = 0; i < N; ++i) {\n count += (nums[i] == 1) ? 1 : -1;\n int idx = count + N;\n if(map[idx] == -2) {\n map[idx] = i;\n } else {\n ans = std::max(ans, i - map[idx]);\n }\n }\n return ans;\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for(std::string s; std::getline(std::cin, s);) {\n std::vector<int> nums_input = parse_input_string(s);\n out << findMaxLength(nums_input) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n int pref = 0;\n int ans = INT_MIN;\n unordered_map<int, int> mpp;\n for (int i = 0; i < n; i++) {\n if (nums[i] == 0)\n pref--;\n if (nums[i] == 1)\n pref++;\n if (pref == 0)\n ans = max(ans, i + 1);\n if (mpp.find(pref) != mpp.end())\n ans = max(ans, i - mpp[pref]);\n else\n mpp[pref] = i;\n }\n return ans == INT_MIN ? 0 : ans;\n }\n};", "memory": "22000" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n(nums.size());\n partial_sum(nums.cbegin(), nums.cend(), nums.begin());\n int len(n % 2 == 0 ? n : n-1);\n while (len > 0) {\n int nOnes = nums[len-1];\n if (nOnes * 2 == len) {\n return len;\n }\n for (int i = 0; i < n - len; i ++) {\n nOnes = nums[i+len] - nums[i];\n if (nOnes * 2 == len) {\n return len;\n }\n }\n len -= 2;\n }\n return len;\n }\n};", "memory": "82600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n\n if(nums.size()<2)\n return 0;\n\n //find the sum of all elements in the array\n\n int sum=0, lsum=0, rsum=0, tmp=0;\n\n for(int i:nums){\n sum+=i;\n }\n\n \n int wnd=nums.size();\n\n if((wnd%2)!=0){\n wnd--;\n rsum=nums[nums.size()-1];\n tmp=rsum;\n }\n\n while(wnd>=2){\n \n for(int st=0; st<=nums.size()-wnd; st++){\n\n if((sum-(lsum+rsum))==(wnd/2))\n return wnd;\n \n \n lsum+=nums[st];\n if((st+wnd)<nums.size())\n rsum-=nums[st+wnd];\n }\n\n lsum=0;\n tmp+=(nums[wnd-1]+nums[wnd-2]);\n rsum=tmp;\n\n wnd-=2;\n }\n\n return 0;\n \n }\n};", "memory": "82800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int counter = 0;\n int maxArr[nums.size() * 2 + 1];\n maxArr[nums.size()] = 0;\n for(int i = 0; i<nums.size(); i++) {\n counter += nums[i]==1 ? 1 : -1;\n maxArr[nums.size() + counter] = i+1;\n } \n counter = 0;\n int result = maxArr[nums.size()];\n for(int i = 0; i<nums.size(); i++) {\n counter += nums[i]==1 ? 1 : -1;\n result = max(result, maxArr[nums.size() + counter] - i -1);\n }\n return result;\n }\n};", "memory": "83100" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n std::replace(nums.begin(), nums.end(), 0, -1);\n std::partial_sum(nums.begin(), nums.end(), nums.begin());\n std::array<int, 100001> map;\n map.fill(-2);\n map[0 + 50000] = -1;\n int maxSz = 0;\n for (int i = 0; i < nums.size(); ++i) {\n auto& val = map[nums[i] + 50000];\n if (val == -2) {\n val = i;\n continue;\n }\n int newSz = i - val;\n if (newSz > maxSz)\n maxSz = newSz;\n }\n return maxSz;\n }\n};", "memory": "83300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n int N = 2*n+1;\n int arr[N]; \n fill(arr, arr+N, -2);\n arr[n] = -1;\n \n int cnt = 0, ans = 0;\n for(int i = 0; i<n; i++){\n if(nums[i] == 1) cnt++;\n else cnt--;\n if(arr[cnt+n]>=-1) ans = max(ans, i-arr[cnt+n]);\n else arr[cnt+n] = i;\n }\n \n return ans;\n }\n};", "memory": "83400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> positive_sums;\n vector<int> negative_sums;\n positive_sums.push_back(-1);\n negative_sums.push_back(-1);\n\n int res = 0;\n int sum = 0;\n for(int i = 0; i < nums.size(); ++i)\n {\n sum += (nums[i] == 0)? -1:1;\n int index = -1;\n if(sum > 0) \n {\n if(positive_sums.size() > sum )\n index = positive_sums[sum];\n else\n {\n positive_sums.push_back(i);\n index = i;\n }\n }\n else\n {\n if(negative_sums.size() > -sum )\n index = negative_sums[-sum];\n else\n {\n negative_sums.push_back(i);\n index = i;\n }\n } \n\n res = max(res, i - index);\n }\n\n return res;\n }\n};", "memory": "83600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n #define MAX(a, b) ((a) > (b) ? (a) : (b))\n int n(nums.size());\n int sum2idx[200001] = {0};\n fill(sum2idx, sum2idx+200000, -1);\n partial_sum(nums.cbegin(), nums.cend(), nums.begin());\n int maxLen(0);\n for (int i = 0; i < n; i ++) {\n nums[i] = 2 * nums[i] - i - 1;\n int x = nums[i];\n if (x == 0) {\n maxLen = MAX(maxLen, i+1);\n } else {\n if (sum2idx[x+100000] != -1) {\n maxLen = MAX(maxLen, i - sum2idx[x+100000]);\n } else {\n sum2idx[x+100000] = i;\n }\n }\n }\n return maxLen;\n }\n};", "memory": "83800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int noOfZeroes=0;\n int noOfOnes=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==0){\n nums[i]=-1;\n noOfZeroes++;\n }\n else{\n noOfOnes++;\n }\n }\n if(noOfZeroes==noOfOnes){\n return nums.size();\n }\n //now we have the updated nums array\n //we need to the longest subarray with sum equal to 0\n unordered_map<int,int>prefixSum;\n prefixSum[0]=-1;\n int sum=0;\n int maxSize=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n if(prefixSum.find(sum)!=prefixSum.end()){\n // auto p=prefixSum[sum-0];\n maxSize=max(maxSize,i-prefixSum[sum]);\n\n }\n if(prefixSum.find(sum)==prefixSum.end()){\n prefixSum[sum]=i;\n }\n\n }\n return maxSize;\n }\n};", "memory": "84600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"fast-math\",\"unroll-loops\",\"no-stack-protector\",\"-ffast-math\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native\",\"f16c\")\nint desync = []{\n ios::sync_with_stdio(0);\n cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> prefix; prefix.reserve(nums.size());\n prefix.push_back(nums[0] == 0 ? -1 : 1);\n for (int i = 1; i < nums.size(); i++) {\n prefix.push_back(prefix[i-1] + (nums[i] == 0 ? -1 : 1));\n }\n int max_length = 0, n = nums.size()-1;\n for (int i, j = 0, pre_sum = 0; max_length <= n - j; pre_sum = prefix[j++]) {\n for (i = n; max_length <= i - j; ) {\n if (prefix[i] == pre_sum) {\n max_length = max(max_length, i - j + 1); break;\n }\n int x = prefix[i] - pre_sum;\n i -= abs(x);\n }\n }\n return max_length;\n }\n};", "memory": "85800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> prefix; prefix.reserve(nums.size());\n if (nums[0] == 1) {\n prefix.push_back(1);\n }\n else {\n prefix.push_back(-1);\n }\n for (int i = 1; i < nums.size(); i++) {\n if (nums[i] == 1) {\n prefix.push_back(1 + prefix[i-1]);\n }\n else {\n prefix.push_back(-1 + prefix[i-1]);\n }\n }\n int max_length = 0;\n for (int n = nums.size()-1, j = 0; max_length <= n - j; j++) {\n for (int i = n; i >= 0; ) {\n if (prefix[i] - (j == 0 ? 0 : prefix[j-1]) != 0) {\n i-= abs(prefix[i] - (j == 0 ? 0 : prefix[j-1]));\n }\n else {\n max_length = max(max_length, i - j + 1); break;\n }\n }\n }\n return max_length;\n }\n};", "memory": "86200" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "int desync = []{\n ios::sync_with_stdio(0);\n cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> prefix; prefix.reserve(nums.size());\n if (nums[0] == 1) {\n prefix.push_back(1);\n }\n else {\n prefix.push_back(-1);\n }\n for (int i = 1; i < nums.size(); i++) {\n if (nums[i] == 1) {\n prefix.push_back(1 + prefix[i-1]);\n }\n else {\n prefix.push_back(-1 + prefix[i-1]);\n }\n }\n int max_length = 0;\n for (int n = nums.size()-1, j = 0; max_length <= n - j; j++) {\n for (int i = n; i >= 0; ) {\n if (prefix[i] - (j == 0 ? 0 : prefix[j-1]) != 0) {\n i-= abs(prefix[i] - (j == 0 ? 0 : prefix[j-1]));\n }\n else {\n max_length = max(max_length, i - j + 1); break;\n }\n }\n }\n return max_length;\n }\n};", "memory": "86200" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "int desync = []{\n ios::sync_with_stdio(0);\n cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> prefix; prefix.reserve(nums.size());\n if (nums[0] == 1) {\n prefix.push_back(1);\n }\n else {\n prefix.push_back(-1);\n }\n for (int i = 1; i < nums.size(); i++) {\n if (nums[i] == 1) {\n prefix.push_back(1 + prefix[i-1]);\n }\n else {\n prefix.push_back(-1 + prefix[i-1]);\n }\n }\n int max_length = 0;\n for (int n = nums.size()-1, j = 0; max_length <= n - j; j++) {\n for (int i = n; i >= 0; ) {\n if (prefix[i] - (j == 0 ? 0 : prefix[j-1]) != 0) {\n i-= abs(prefix[i] - (j == 0 ? 0 : prefix[j-1]));\n }\n else {\n max_length = max(max_length, i - j + 1); break;\n }\n }\n }\n return max_length;\n }\n};", "memory": "86400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> onePos;\n for (int i = 0; i < nums.size(); i ++) if (nums[i]) onePos.push_back(i);\n if (onePos.size() <= 1) return 2 * onePos.size() * (int) (onePos.size() != nums.size());\n else {\n for (int size = onePos.size(); size > 1; size--){\n int first = 0;\n int last = size-1;\n int zeroSpace = onePos[last] - onePos[first] - (last - first);\n int zeroEnds = onePos[first];\n \n while (last < onePos.size()){\n if (last == onePos.size()-1) zeroEnds += nums.size() - onePos[last] - 1;\n else zeroEnds += onePos[last+1] - onePos[last] - 1;\n\n\n if (zeroSpace <= size && zeroSpace + zeroEnds >= size){\n return (2 * size);\n }\n else{\n if (first == 0) zeroEnds -= onePos[first];\n else zeroEnds -= (onePos[first] - onePos[first-1]-1);\n zeroSpace += zeroEnds;\n \n first++;\n\n zeroEnds = (onePos[first] - onePos[first-1]-1);\n zeroSpace -= zeroEnds;\n\n last++;\n\n \n\n }\n }\n }\n if (onePos.size() == nums.size()) return 0;\n else return 2;\n \n }\n }\n};", "memory": "86600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n if (nums.size() == 1) return 0;\n int maxL = 0;\n int ranges[nums.size()];\n stack<int> odd, even;\n if (nums[0] == 0) even.push(0);\n else odd.push(0);\n ranges[0] = -1;\n for (int i = 1; i < nums.size(); i++) {\n ranges[i] = -1;\n if (nums[i] == 0) {\n even.push(i);\n if (odd.size() > 0) {\n int key = odd.top() - 1, start = odd.top();\n if (key >= 0 && ranges[key] != -1) {\n start = ranges[key];\n // ranges.erase(key);\n }\n ranges[i] = start;\n odd.pop();\n }\n }\n else {\n odd.push(i);\n if (even.size() > 0) {\n int key = even.top() - 1, start = even.top();\n if (key >= 0 && ranges[key] != -1) {\n start = ranges[key];\n // ranges.erase(key);\n }\n ranges[i] = start;\n even.pop();\n }\n }\n if (ranges[i] >= 0) {\n maxL = max(maxL, i - ranges[i] + 1);\n }\n }\n return maxL;\n }\n};", "memory": "87000" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n // if we replace 0 by -1, the sum would be 0 when ever there are equal number of zeros and ones\n int n=nums.size();\n vector<int> prefixsum(n,0);\n vector<int> posindices;\n vector<int> negindices;\n int sum=0;\n int ans=0;\n for(int i=0; i<n; i++){\n if(nums[i]==0) sum--;\n else sum++;\n prefixsum[i]=sum;\n if(sum==0) ans=i+1; \n else if(sum>0) { \n int size=sum;\n if(posindices.size()>=size) ans=max(ans,i-posindices[size-1]); // already occured\n else posindices.push_back(i);\n }\n else { \n int size=-sum;\n if(negindices.size()>=size) ans=max(ans,i-negindices[size-1]); // already occured\n else negindices.push_back(i);\n }\n }\n return ans;\n }\n};", "memory": "87100" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n if (nums.size() == 1) return 0;\n int maxL = 0;\n int ranges[nums.size()];\n stack<int> odd, even;\n if (nums[0] == 0) even.push(0);\n else odd.push(0);\n ranges[0] = -1;\n for (int i = 1; i < nums.size(); i++) {\n ranges[i] = -1;\n if (nums[i] == 0) {\n even.push(i);\n if (odd.size() > 0) {\n int key = odd.top() - 1, start = odd.top();\n if (key >= 0 && ranges[key] != -1) {\n start = ranges[key];\n // ranges.erase(key);\n }\n ranges[i] = start;\n odd.pop();\n }\n }\n else {\n odd.push(i);\n if (even.size() > 0) {\n int key = even.top() - 1, start = even.top();\n if (key >= 0 && ranges[key] != -1) {\n start = ranges[key];\n // ranges.erase(key);\n }\n ranges[i] = start;\n even.pop();\n }\n }\n if (ranges[i] >= 0) {\n maxL = max(maxL, i - ranges[i] + 1);\n }\n }\n return maxL;\n }\n};", "memory": "87200" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n // key = prefix sum, val = first index of the prefix sum\n unordered_map<int, int> mp;\n int prefixSum = 0, maxLen = 0;\n mp.insert_or_assign(0, -1);\n for (int i = 0; i < nums.size(); i++) {\n prefixSum += nums[i] == 0 ? -1 : 1;\n\n if (mp.contains(prefixSum)) {\n // if mp contain prefix Sum,\n // means the element sum between two prefix sum is zero\n maxLen = max(maxLen, i - mp.at(prefixSum));\n } else {\n mp.insert_or_assign(prefixSum, i);\n }\n\n }\n return maxLen;\n }\n};", "memory": "87300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n const int n = nums.size();\n std::unordered_map<int, int> diff_to_index_map{{0, -1}};\n auto difference = 0;\n auto ret = 0;\n for (auto i = 0; i < n; ++i) {\n const auto num = nums[i];\n if (num == 1) {\n ++difference;\n } else {\n --difference;\n }\n const auto it = diff_to_index_map.find(difference);\n if (it != diff_to_index_map.end()) {\n ret = std::max(ret, i - it->second);\n } else {\n diff_to_index_map.try_emplace(difference, i);\n }\n }\n return ret; \n }\n};", "memory": "87400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n map <int,int>mp;\n mp.insert({0,-1});\n int max1=0,max_length=0,sum=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n sum=sum-1;\n }\n else{\n sum=sum+1;\n }\n if(mp.find(sum)!=mp.end()){\n max1=i-mp[sum];\n max_length=max(max1,max_length);\n }\n else{\n mp.insert({sum,i});\n }\n }\n return max_length;\n }\n};", "memory": "87500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int len = nums.size();\n int sum = 0;\n int ans = 0;\n map<int, int> m = {{0, -1}};\n for (int i = 0; i < len; i++) {\n if (nums[i] == 1)\n sum++;\n else\n sum--;\n \n auto it = m.find(sum);\n if (it != m.end())\n ans = max(ans, i - m[sum]);\n else\n m.insert({sum, i});\n }\n\n return ans;\n }\n};", "memory": "87600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n=nums.size();\n int ans=INT_MIN;\n map<int,int> m;int c=0;\n m.insert({0,-1});\n for(int i=0;i<n;i++){\n (nums[i])?c++:c--;\n if(m.find(c)==m.end())m.insert({c,i});\n else {\n int x=(m.find(c))->second;\n ans=max(ans,i-x);\n }\n }\n return (ans==INT_MIN)?0:ans;\n }\n};", "memory": "87700" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size(); \n vector<int> sums(n, 0);\n unordered_map<int, int> hm;\n sums[0] = (nums[0] ? 1 : -1);\n for(int i = 1; i < n; i++)\n sums[i] = sums[i-1] + (nums[i] ? 1 : -1);\n if(sums[n-1] == 0)\n return n;\n int mx = 0;\n for(int i = 0; i < n; i++) {\n if(sums[i] == 0)\n mx = i+1;\n if(hm.count(sums[i]))\n mx = max(mx, i - hm[sums[i]]);\n else\n hm[sums[i]] = i; \n } \n return mx;\n }\n};", "memory": "87800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> map;\n map[0] = -1;\n int count = 0;\n int maxLength = 0;\n for (int i = 0; i < nums.size(); ++i) {\n if (nums[i] == 0) {\n count++;\n } else {\n count--;\n }\n if (map.find(count) != map.end()) {\n maxLength = max(maxLength, i - map[count]);\n } else {\n map[count] = i;\n }\n }\n return maxLength;\n }\n};", "memory": "88000" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int maxLength = 0;\n std:unordered_map<int,int> prevSum = {{0, -1}};\n int currSum = 0;\n for(int i = 0; i < nums.size(); ++i) {\n currSum += nums[i] == 1 ? 1 : -1;\n auto it = prevSum.find(currSum);\n if(it != prevSum.end())\n maxLength = std::max(maxLength, i-(it->second));\n else\n prevSum[currSum] = i;\n }\n return maxLength;\n }\n};", "memory": "88100" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int,int> mpp;\n // mpp[sum] = index\n mpp[0] = -1;\n int sum = 0;\n int len = 0;\n for(int i = 0; i< nums.size();i++){\n if(nums[i]== 0){\n sum+=(-1);\n }\n else{\n sum+=(1);\n }\n if(mpp.find(sum)!=mpp.end()){\n len = max(len,i-mpp[sum]);\n }\n else{\n mpp[sum]=i;\n }\n }\n return len;\n }\n};", "memory": "88200" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> ct;\n int ans = 0;\n int csum = 0;\n\n for(int i=0; i<nums.size(); i++){\n int val = nums[i] == 0 ? -1 : 1;\n csum += val;\n\n if(csum == 0){\n ans = max(ans, i+1);\n }\n else if(ct.find(csum) != ct.end()){\n //pehle se hi present hai\n ans = max(ans, i - ct[csum]);\n }\n else{\n //absent..so create new key with value same as index of that element\n ct[csum] = i;\n }\n }\n return ans;\n }\n};", "memory": "88200" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\n public:\n int findMaxLength(vector<int>& nums) {\n int ans = 0;\n int prefix = 0;\n unordered_map<int, int> prefixToIndex{{0, -1}};\n\n for (int i = 0; i < nums.size(); ++i) {\n prefix += nums[i] ? 1 : -1;\n if (const auto it = prefixToIndex.find(prefix);\n it != prefixToIndex.cend())\n ans = max(ans, i - it->second);\n else\n prefixToIndex[prefix] = i;\n }\n\n return ans;\n }\n};", "memory": "88300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int,int> mpp;\n int sum=0;\n int ans =0;\n mpp[sum]=-1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==0){\n sum--;\n }else{\n sum++;\n }\n if(mpp.find(sum) != mpp.end()){\n ans = max(ans,i-mpp[sum]);\n }else{\n mpp[sum]=i;\n }\n }\n return ans;\n }\n};", "memory": "88300" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& arr) {\n int N = arr.size();\n \n for(int i=0;i<N;i++){\n if(arr[i]==0) arr[i]=-1;\n }\n \n int count=0;\n int size = 0;\n int i=0;\n unordered_map<int,int>mp;\n int sum=0;\n \n while(i<N){\n mp[0]=-1;\n sum+=arr[i];\n if(!mp.count(sum)){\n mp[sum] = i;\n }\n else{\n count = i - mp[sum];\n }\n i++;\n size = max(size,count);\n }\n return size;\n }\n};", "memory": "88400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
1
{ "code": "/*\n- Doesn't qualify the condition 1 and condition 2 of the sliding window, hence dont apply it\nhere.\n- These type of question is usually solved by state.\n- we defined state = count1 - count0\n\n*/\n\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n \n unordered_map<int,int> mp;\n mp[0] = -1;\n\n \n int count1 = 0;\n int count0 = 0;\n\n int longest = 0;\n\n for(int i = 0 ; i < nums.size(); i++){\n if(nums[i] == 1) count1++;\n else count0++;\n\n int state = count1 - count0;\n\n if(mp.find(state) == mp.end()) mp[state] = i;\n else longest = max(longest, i - mp[state]);\n }\n\n return longest;\n }\n};", "memory": "88400" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n\n int findMaxLength(vector<int>& nums) {\n unordered_map<int,int> m;\n int sum = 0;\n\n m[0] = -1;\n int ans = 0;\n\n for(int i=0;i<nums.size();i++){\n if(nums[i] == 0){\n sum--;\n }else{\n sum++;\n }\n\n if(m.find(sum) != m.end()){\n ans = max(ans,i-m[sum]);\n }else{\n m[sum] = i;\n }\n }\n\n return ans;\n }\n};", "memory": "88500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int,int> sums;\n int sum=0;\n int ans=0;\n sums[0]=-1;\n for(int i=0;i<nums.size();i++){\n int t=(nums[i]==1?1:-1);\n sum=sum+t;\n if(sums.find(sum)!=sums.end())\n ans=max(ans,i-sums[sum]);\n else\n sums[sum]=i;\n }\n return ans;\n\n \n }\n};\n// 1 0 0 0 1 0 0 1 0 0\n// 1 0-1-2-1-2\n// 0 1 0\n// -1 0 1\n// 1 0\n// 1 0\n\n", "memory": "88500" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n map<int,int> m;\n int sum = 0;\n\n m[0] = -1;\n int ans = 0;\n\n for(int i=0;i<nums.size();i++){\n if(nums[i] == 0){\n sum--;\n }else{\n sum++;\n }\n\n if(m.find(sum) != m.end()){\n ans = max(ans,i-m[sum]);\n }else{\n m[sum] = i;\n }\n }\n\n return ans;\n }\n};", "memory": "88600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int n = nums.size() , maxi = 0 ,sum = 0;\n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0; i<n; i++){\n sum += nums[i] == 1 ? 1 : -1;\n if(mp.count(sum)) maxi = max(maxi, i-mp[sum]);\n else mp[sum] = i;\n }\n\n return maxi;\n }\n};", "memory": "88600" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n=nums.size(),ans=0;\n for(int i=0;i<n;i++){\n if(nums[i]==0)nums[i]=-1;\n }\n map<int,int>mp;\n mp[0]=-1;\n int sum=0;\n for(int i=0;i<n;i++){\n sum+=nums[i];\n if(mp.find(sum)!=mp.end())ans=max(ans,i-mp[sum]);\n else mp[sum]=i;\n }\n return ans;\n }\n};", "memory": "88700" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int cnt = 0;// number of freq of zero - one\n map<int,int> mp;\n mp[0] = -1;\n int ans = 0;\n for(int i = 0;i < nums.size();++i){\n if(nums[i] == 0) cnt++;\n else cnt--;\n\n if(mp.find(cnt) != mp.end()){\n ans = max(ans,i - mp[cnt]);\n } else {\n mp[cnt] = i;\n }\n }\n return ans;\n }\n};", "memory": "88700" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int one = 0;\n int zero = 0;\n map<int, int> last;\n last[0] = -1;\n int ans = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 1) one++;\n else zero++;\n int e = one - zero;\n if (last.find(e) != last.end()) {\n ans = max(ans, i - last[e]);\n } else last[e] = i;\n }\n return ans;\n }\n};", "memory": "88800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n \n int findMaxLength(vector<int>& nums) {\n map<int,int> m;\n int sum = 0;\n\n m[0] = -1;\n int ans = 0;\n\n for(int i=0;i<nums.size();i++){\n if(nums[i] == 0){\n sum--;\n }else{\n sum++;\n }\n\n if(m.find(sum) != m.end()){\n ans = max(ans,i-m[sum]);\n }else{\n m[sum] = i;\n }\n }\n\n return ans;\n }\n};", "memory": "88800" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size(); \n map<int,int> counts;\n counts[0] = -1; // in case diff mta3 zeros wl ones 3mlou zero meloul tabda fl map\n\n int maxL = 0;\n int count = 0;\n\n for (int i = 0; i<n; i++) {\n if (nums[i]) {\n count++;\n } else {\n count--;\n }\n\n if (counts.find(count) != counts.end()) {\n maxL = max(maxL, i - counts[count]);\n } else {\n counts[count] = i;\n }\n }\n\n return maxL;\n }\n};", "memory": "88900" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& arr) {\n int mxLength = 0, sum = 0;\n int n = arr.size();\n map<int, int>mp;\n mp[0] = -1;\n for(int i=0;i<n;i++){\n if(arr[i]==0) sum-=1;\n else sum+=1;\n\n if(mp.find(sum)!=mp.end()){\n mxLength = max(mxLength, i - mp[sum]);\n }\n else mp[sum] = i;\n }\n return mxLength;\n }\n};", "memory": "88900" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> arr(2 * nums.size() + 1, INT_MIN);\n arr[nums.size()] = -1;\n int maxLen = 0, sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += (nums[i] == 0 ? -1 : 1);\n if (arr[sum + nums.size()] >= -1) {\n maxLen = max(maxLen, i - arr[sum + nums.size()]);\n } else {\n arr[sum + nums.size()] = i;\n }\n }\n return maxLen;\n }\n};", "memory": "89000" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> arr(2*n + 1, INT_MIN);\n\t\tarr[n] = -1;\n int maxLen = 0, sum = 0;\n for (int i = 0; i < n; i++) {\n sum += (nums[i] == 0 ? -1 : 1);\n\t\t\tif (arr[sum + n] >= -1) \n maxLen = max(maxLen, i - arr[sum + n]);\n\t\t\telse arr[sum + n] = i; \n }\n return maxLen;\n }\n};", "memory": "89100" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> arr(2 * nums.size() + 1, INT_MIN);\n arr[nums.size()] = -1;\n int maxLen = 0, sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += (nums[i] == 0 ? -1 : 1);\n if (arr[sum + nums.size()] >= -1) {\n maxLen = max(maxLen, i - arr[sum + nums.size()]);\n } else {\n arr[sum + nums.size()] = i;\n }\n }\n return maxLen;\n }\n};", "memory": "89100" }
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,0] <strong>Output:</strong> 2 <strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
3
{ "code": "class Solution\n{\npublic:\n int findMaxLength(std::vector<int> &nums)\n {\n\n int sum = 0;\n int max = 0;\n appearances_[0].first = -1;\n for (int i = 0; i < nums.size(); ++i)\n {\n sum += nums[i] * 2 + -1;\n if (appearances_.find(sum) == appearances_.end())\n {\n appearances_[sum].first = i;\n }\n else\n {\n int distance = i - appearances_[sum].first;\n if (distance > max)\n {\n max = distance;\n }\n\n appearances_[sum].last = i;\n }\n\n }\n\n return max;\n }\n\nprivate:\n struct Appearance\n {\n int first;\n int last;\n };\n\n std::unordered_map<int, Appearance> appearances_;\n};", "memory": "89200" }