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</l... | 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 ... |
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</l... | 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])... |
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</l... | 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 va... |
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</l... | 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]... |
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</l... | 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_m... |
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</l... | 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 ... |
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</l... | 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<<P... |
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</l... | 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 ... |
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</l... | 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) {\... |
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</l... | 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 ... |
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</l... | 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 r... |
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</l... | 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 }... |
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</l... | 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 unor... |
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</l... | 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 th... |
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... | 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() && wor... |
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... | 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 whil... |
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... | 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>& dictionar... |
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... | 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)... |
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... | 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 ... |
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... | 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... |
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... | 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]... |
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... | 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<st... |
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... | 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 ... |
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... | 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 ... |
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... | 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 findL... |
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... | 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 fin... |
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... | 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 }\... |
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... | 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}... |
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... | 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;... |
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... | 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 < dic... |
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... | 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 < dic... |
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... | 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 < dic... |
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... | 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 ... |
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... | 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 ... |
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... | 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 ... |
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... | 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 retu... |
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... | 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... |
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... | 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 ... |
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... | 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 ... |
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... | 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(... |
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... | 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 currentPositi... |
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... | 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... |
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... | 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... |
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... | 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 i... |
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... | 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... |
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... | 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... |
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... | 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... |
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... | 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.si... |
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... | 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... |
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... | 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, ve... |
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... | 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(... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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<... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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<... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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<... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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<... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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<... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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] =... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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;... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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) cn... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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(); ++... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 noOf... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 Soluti... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 (i... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 (i... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 } el... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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.... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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++;... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 in... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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.siz... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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)!=sum... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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];\... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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 ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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+... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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++)... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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[... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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] >= ... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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[... |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1]
<strong>Output:</strong> 2
<strong>... | 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) == appe... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.