id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,134
<p>A teacher is writing a test with <code>n</code> true/false questions, with <code>&#39;T&#39;</code> denoting true and <code>&#39;F&#39;</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (mu...
3
{ "code": "class Solution {\npublic:\n int m = 0;\n void lr_point0 (vector<int>& vc, int l, int r, int k, int& vs, int now) {\n m = max(m, now + k);\n if (r + 1 >= vs - 1)\n return ;\n\n if(vc[r + 1] <= k) {\n now += vc[r + 1] + vc[r + 2];\n k -= vc[r + 1];\n ...
2,134
<p>A teacher is writing a test with <code>n</code> true/false questions, with <code>&#39;T&#39;</code> denoting true and <code>&#39;F&#39;</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (mu...
3
{ "code": "class Solution {\npublic:\n int maxConsecutiveAnswers(string answerKey, int k) {\n \n int ans = 0;\n queue<int> q;\n int L = 0;\n for (int i = 0; i < answerKey.size(); i++) {\n if (answerKey[i] == 'F') {\n if (q.size() < k) q.push(i);\n ...
2,134
<p>A teacher is writing a test with <code>n</code> true/false questions, with <code>&#39;T&#39;</code> denoting true and <code>&#39;F&#39;</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (mu...
3
{ "code": "class Solution {\npublic:\n int maxConsecutiveAnswers(string s, int k) {\n int retVal{0};\n int start{0}, start2{0};\n int end{0};\n const int n = s.length();\n int count{0}, count2{0};\n queue<int> index;\n queue<int> index2;\n k++;\n while...
2,134
<p>A teacher is writing a test with <code>n</code> true/false questions, with <code>&#39;T&#39;</code> denoting true and <code>&#39;F&#39;</code> denoting false. He wants to confuse the students by <strong>maximizing</strong> the number of <strong>consecutive</strong> questions with the <strong>same</strong> answer (mu...
3
{ "code": "class Solution {\npublic:\n int maxConsecutiveAnswers(string answerKey, int k) {\n queue<int> tr,fs;\n int re = k;\n for(int i=0; i<answerKey.size(); i++){\n if(answerKey[i] == 'T') {\n fs.push(i);\n if(fs.size() > k+1){\n ...
1,929
<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </strong>that satisfies the following conditions:</p> <ul> <li><code>nums.length == n</code></li> <li><code>nums[i]</code> ...
0
{ "code": "class Solution {\npublic:\n int maxValue(int n, int index, int maxSum) {\n long long int a = 0, b = INT_MAX;\n while (a < b) {\n long long int m = (a+b+1)/2;\n\n m--;\n\n long long int left, beginLeft = m-index;\n beginLeft > 1 ? left = (m-1)*(m)...
1,929
<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </strong>that satisfies the following conditions:</p> <ul> <li><code>nums.length == n</code></li> <li><code>nums[i]</code> ...
0
{ "code": "class Solution {\npublic:\n /*binary search\n 1 2 3 4 5 4 3 2\n 0 index n-1\n\n 0 0 0 1 2 3 4 3\n 0 7 8 \n\n 1 1 1 1 2 3 mid ... 3 2 1 1 1\n 1 1 2 3 index \n\n 0 0 0 0 1 2 mid-1 ... 2 1 0...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(const string& s) {\n const int size = s.size();\n if(size == 1) [[unlikely]] return 0;\n else if(size == 2) [[unlikely]] return s[0] == s[1];\n else if(size == 3) [[unlikely]] return (s[0] == s[1] || s[1] == s[2]);\n\n int ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(string &s) {\n // support variables\n int c1 = 0, c2 = 0, step = 0;\n // parsing s\n for (char c: s) {\n if ((c == '1') ^ (step++ & 1)) c1++;\n else c2++;\n }\n return min(c1, c2);\n }\n};", ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(const string& s) {\n int c0 = 0, c1 = 0;\n for(int i = 0; i < s.size(); ++i) {\n if(i & 1) {\n if(s[i] == '1') {\n c1++;\n }\n else\n {\n ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(const string& s) {\n if(s.size() == 1) return 0;\n\n int c0 = 0, c1 = 0;\n for(int i = 0; i < s.size(); ++i) {\n if(i & 1) {\n if(s[i] == '1') {\n c1++;\n }\n els...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n int startWithZero = 0, startWithOne = 0;\n\n for(int i = 0; i < s.length(); ++i) {\n if(i % 2 == 0 && s[i] != '0') ++startWithZero;\n else if(i % 2 == 1 && s[i] != '1') ++startWithZero;\n \n ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n int temp1 = 0, temp2 = 0;\n for (int i = 0; i < s.size(); i++) {\n if (((i % 2 == 0) && (s[i] != '1') ||\n ((i % 2 != 0) && (s[i] != '0'))))\n temp1++;\n }\n for (int i = 0; ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "\nclass Solution {\npublic:\n int minOperations(string s) {\n int n=s.size(), ans=0;\n for(int i=0;i<n;i++) {\n if(s[i]-'0' != i%2)\n ans++;\n }\n return min(ans, n-ans);\n }\n};\n", "memory": "8400" }
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
0
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n // 1st altr pattern 010101..etc\n // 2nd altr pattern 101010..etc\n // result is min to acheive either of them\n int n = s.size();\n int op1=0,op2=0;\n int i=0,j=1;\n while(i<n){\n if(...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
1
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n // 1st altr pattern 010101..etc\n // 2nd altr pattern 101010..etc\n // result is min to acheive either of them\n int n = s.size();\n int op1=0,op2=0;\n int i=0,j=1;\n while(i<n){\n if(...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
1
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n int resultPattern1 = 0; // pattern 010101...\n int resultPattern2 = 0; // pattern 101010...\n \n for (int i = 0; i < s.length(); i++) {\n // pattern 010101...\n if (i % 2 == 0 && s[i] != '0') ...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
2
{ "code": "\nclass Solution {\npublic:\n int minOperations(string s) {\n int n=s.size(), ans=0;\n for(int i=0;i<n;i++) {\n if(s[i]-'0' != i%2)\n ans++;\n }\n return min(ans, n-ans);\n }\n};\n", "memory": "8600" }
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
2
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n int count = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] - '0' == i%2){\n count+=1;\n }\n }\n return min(count,static_cast<int>(s.length())-count);\n }\n};", "memory": "8600...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
3
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n string s2 = s;\n int cnt1 = 0, cnt2 = 1, n = s.size();\n for(int i = 1; i < n; i++){\n if(s[i-1] == s[i]){\n if(s[i-1] == '0'){\n s[i] = '1';\n } else s[i] = '0';\...
1,884
<p>You are given a string <code>s</code> consisting only of the characters <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>. In one operation, you can change any <code>&#39;0&#39;</code> to <code>&#39;1&#39;</code> or vice versa.</p> <p>The string is called alternating if no two adjacent characters are equal. For...
3
{ "code": "class Solution {\npublic:\n int minOperations(string s) {\n // make the binaray string start with 1\n string S = s;\n int startwithone = 0;\n if(s[0] == '0'){\n startwithone++;\n s[0] = '1';\n }\n char prev = s[0];\n for(int i = 1;i<s.size();i++)...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(const string& s) \n {\n \n long long occuranceCount = 0;\n\n // For Each Character\n for(unsigned int head = 1, length = 1, size = s.size(); head <= size; head++)\n {\n\n // If Its The Same Character As Befo...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(const string& s) \n {\n \n // Occurances Is Going To Be Excessively Large\n long long occuranceCount = 0;\n\n // For Each Character\n for(unsigned int head = 1, length = 1, size = s.size(); head <= size; head++)\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(const string& s) \n {\n \n long long occuranceCount = 0;\n\n // For Each Character\n for(unsigned int head = 1, length = 1, size = s.size(); head <= size; head++)\n {\n\n if(s[head - 1] == s[head])\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(const string& s) \n {\n \n // Occurances Is Going To Be Excessively Large\n long long occuranceCount = 0;\n\n // For Each Character\n for(unsigned int head = 1, length = 1, size = s.size(); head <= size; head++)\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(const string& s) \n {\n \n long long occuranceCount = 0;\n\n // For Each Character\n for(unsigned int head = 1, length = 1, size = s.size(); head <= size; head++)\n {\n\n if(s[head - 1] == s[head])\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n const int MOD = 1e9 + 7;\n long long total = 0;\n int count = 1;\n for(int i =1;i<s.length();i++){\n if(s[i]==s[i-1]){\n count++;\n }else{\n total= (total+ (long l...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int mod = 1e9 + 7;\n long result=0;\n int streak=1;\n for(int i=1;i<s.size();i++){\n if(s[i]==s[i-1]){\n streak++;\n }\n else{\n result += (streak*(str...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int streak=1;\n long ans=0;\n int mod = 1e9 + 7;\n for(int i=1;i<s.size();i++){\n if(s[i-1]==s[i]){\n streak++;\n }\n else{\n ans += (streak*(streak+1L...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
0
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) \n {\n int n=s.size();\n long long ans=1,c=1,m=1e9+7;\n for(int i=1;i<n;i++)\n { \n if(s[i]==s[i-1]) c++;\n else c=1;\n ans=(ans+c)%m;\n }\n return ans;\n }\n};", "memor...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
1
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n long long count = 0;\n long long current_count = 1 ;\n long long mod = 1e9 + 7;\n\n\n for(int i = 1; i < s.length() ; ++i){\n if(s[i] == s[i-1]){\n current_count ++;\n } else {\...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
1
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n long long ans = 0;\n long long cont = 1;\n int n = s.size();\n\n for (int i=0; i+1<n; ++i){\n if (s[i] == s[i+1]) cont++;\n else {\n ans += (cont * (cont + 1))/2;\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n\n int MOD = 1e9+7;\n int countHomogenous(string s) {\n int n = s.length();\n int length = 0;\n int result = 0;\n\n for(int i=0; i<n; i++) {\n if(i>0 && s[i] == s[i-1]) {\n length +=1;\n }\n else {\...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int MOD = 1e9 + 7;\n long int ans = 0;\n\n stack<char> stk;\n\n int n = s.size();\n\n for(int i = 0; i < n; i++) {\n char c = s[i];\n\n if(stk.empty()) {\n stk.push(c);\n...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int mod = 1000000007;\n int pre[s.length()];\n pre[0] = 1;\n for(int i = 1; i < s.length(); i++){\n pre[i] = 1;\n if(s[i] == s[i-1]) pre[i] = (pre[i] + pre[i-1]) % mod;\n }\n int...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n const int MOD = 1000000007;\n long long ans=0;\n string res=\"\";\n long long j = 0;\n for(int i=0;i<s.length();i++)\n {\n if(res[0]==s[i]|| res.empty())\n {\n res...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n unsigned long long ans = 0;\n const int n = s.length();\n string stack;\n for (int l = 0, r = 0; r < n; ++r) {\n if (stack.empty() || stack.back() == s[r]) {\n stack.push_back(s[r]);\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int mod = 1e9 + 7;\n int ans = 0;\n\n int st = 0;\n int end = 0;\n unordered_map<char, int>um;\n int cnt = 0;\n while(end < s.size())\n {\n um[s[end]]++;\n if (um[s...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n map<char,int> mp;\n for(int i=0;i<s.size();i++){\n mp[s[i]]++;\n }\n long final =0;\n long ans=0;\n final = final + s.size();\n for(int i=1;i<=s.size();i++){\n if(s[i]==s[...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "const int mod=1e9+7;\nclass Solution {\npublic:\n //basic stack\n int countHomogenous(string s) {\n stack<int>st;\n int ans=0;\n for(int i=0;i<s.size();i++)\n {\n if(st.empty())\n {\n st.push(s[i]);\n ans=((ans%mod)+((1)...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n std::vector<int>v ;\n for(int i=0;i<s.size();i++){\n int cn=1;\n while(i+1<s.size()&&s[i+1]==s[i]){\n cn++;\n i++;\n }\n \n v.push_back(cn);\n }\n long long sum=0;\n for(auto &v:v){\n long long val = stat...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n long long int atif=1000000000+7;\n int countHomogenous(string s) {\n vector<int>bhosda;\n for(int i=0;i<s.size();){\n int a=0;\n int j=0;\n for(j=i;j<s.size();){\n if(s[i]==s[j]){\n a++;\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n long long int atif=1000000000+7;\n int countHomogenous(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL); \n cout.tie(NULL);\n vector<int>bhosda;\n for(int i=0;i<s.size();){\n int a=0;\n int j=0;\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "\n\nclass Solution {\npublic:\n long long int atif=1000000000+7;\n int countHomogenous(string s) {\n vector<int>bhosda;\n for(int i=0;i<s.size();){\n int a=0;\n int j=0;\n for(j=i;j<s.size();){\n if(s[i]==s[j]){\n a++;\n...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n map<char, map<int, int>> m = {};\n int start_idx = 0;\n for (int i = 1; i < s.size(); i++) {\n if (s[i] != s[i - 1]) {\n // printf(\"%d\\n\", start_idx);\n m[s[start_idx]][i - star...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int countHomogenous(string s) {\n int count=1,n=s.size(),ans=0;\n vector<ll> arr;\n for(int i=1;i<n;i++){\n if(s[i]==s[i-1]){\n count++;\n }\n else{\n arr.push_back(cou...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int countHomogenous(string s) {\n ll count=1,n=s.size(),ans=0;\n vector<ll> arr;\n for(int i=1;i<n;i++){\n if(s[i]==s[i-1]){\n count++;\n }\n else{\n arr.push_back(coun...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n int countHomogenous(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n ll count=1,n=s.size(),ans=0;\n vector<ll> arr;\n for(int i=1;i<n;i++){\n if(s[i]==s[i-1]){\n count++;\n ...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "const int mod = 1e9+7;\nclass Solution {\npublic:\n int countHomogenous(string s) {\n int cnt = 0;\n char prev = 0;\n vector<long long> cntList;\n for(auto c:s){\n if(c == prev){\n cnt++;\n }\n else{\n if(cnt > 0)...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "// class Solution {\n// public:\n// int mod = 1e9+7;\n// void computeLPSArray(string& t, int* lps) {\n// int m = t.length();\n// lps[0] = 0;\n// int len = 0;\n// int i = 1;\n// while (i < m) {\n// if (t[i] == t[len]) {\n// len++;\n// lps[i] = len;\n//...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n \n int countHomogenous(string s) {\n int mod = 1000000007;\n int n = s.size();\n int cnt = 0;\n vector<int> sim_cnt(n,0);\n sim_cnt[0] = 1;\n for(int i=1;i<n;i++){\n if(s[i] == s[i-1]){\n sim_cnt[i] = 1+si...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int n = s.size();\n if (n == 1)\n return 1;\n map<string, int> map;\n string curr = s.substr(0, 1);\n char currChar = s[0];\n for (int i = 1; i < s.size(); i++) {\n char j = s[i]...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n long long mod=1e9+7;\n \n int countHomogenous(string s) {\n map<string,long long>maps;\n string count=\"\";\n long long n=s.size();\n long long ans=0;\n for(long long i=0;i<n;i++)\n { \n if(count.empty()||s[i]!=coun...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\nprivate:\n int mod = 1e9+7 ; \npublic:\n int countHomogenous(string s) {\n map<char , vector<long long>> mp ; \n\n char curr = s[0] ; \n\n for(auto& it:s){\n if(it == curr){\n if(mp.find(it) != mp.end()){\n mp[it].bac...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int mod=1e9+7;\n long long ans=0;\n int l=0,r=0;\n unordered_map<char,int>m;\n while(r<s.size()){\n m[s[r]]++;\n while(m.size()>1){\n m[s[l]]--;\n if(m[s[l...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "\nclass Solution {\npublic:\n int countHomogenous(string s) {\n int mod = 1e9 + 7; \n int cnt = 0;\n int i = 0;\n int j = 0;\n unordered_map<char, int> mpp;\n int n = s.size();\n\n while (j < n) {\n mpp[s[j]]++;\n if (!(mpp.size() ==...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "\nclass Solution {\npublic:\n int countHomogenous(string s) {\n int mod = 1e9 + 7; // Correctly initialize mod to 10^9 + 7\n int cnt = 0;\n int i = 0;\n int j = 0;\n unordered_map<char, int> mpp;\n int n = s.size();\n\n while (j < n) {\n mpp[s[...
1,885
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A string is <strong>homogenous</strong> if all the characters of the string are...
3
{ "code": "class Solution {\npublic:\n int countHomogenous(string s) {\n int mod=1e9+7;\n long long ans=0;\n int l=0,r=0;\n unordered_map<char,int>m;\n while(r<s.size()){\n m[s[r]]++;\n while(m.size()>1){\n m[s[l]]--;\n if(m[s[l...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\npublic:\n int maxProductDifference(vector<int>& nums) {\n int biggest = 0, secondBiggest = 0;\n int smallest = INT_MAX, secondSmallest = INT_MAX;\n \n for (int num : nums) {\n // Update biggest and second biggest\n if (num > biggest) {\...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n int biggest_0 = INT_MIN;\r\n int biggest_1 = INT_MIN;\r\n int smallest_0 = INT_MAX;\r\n int smallest_1 = INT_MAX;\r\n for (const auto& num : nums) {\r\n if (num > biggest_0) {\...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n \r\n int largest=INT_MIN;\r\n int secLargest=INT_MIN;\r\n\r\n int smallest=INT_MAX;\r\n int secSmallest=INT_MAX;\r\n\r\n for(auto &num:nums){\r\n if(num>largest){\r\n ...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n \r\n int n=nums.size();\r\n\r\n int largest = INT_MIN;\r\n int sLargest = INT_MIN;\r\n\r\n int smallest = INT_MAX;\r\n int sSmallest = INT_MAX;\r\n\r\n for(int i=0;i<n;i++){...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n int max1 = 0, max2 = 0;\r\n int min1 = 1e5, min2 = 1e5;\r\n\r\n for(int ele : nums){\r\n if(ele > max2){\r\n if(ele > max1){\r\n max2 = max1;\r\n ...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n int largest = INT_MIN;\r\n int secondLargest = INT_MIN;\r\n\r\n int smallest = INT_MAX;\r\n int secondSmallest = INT_MAX;\r\n\r\n for(int &num : nums) { // go one by one in nums\r\n\r\n ...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n int largestElement = std::numeric_limits<int>::min();\r\n int secondLargestElement = std::numeric_limits<int>::min();\r\n int smallestElement = std::numeric_limits<int>::max();\r\n int secondSma...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n/*\r\ninfo:\r\n - product difference: (a * b) - (c * d) = k\r\n - distinct: a != b != c != d\r\n - maximizum difference: max(k)\r\n\r\n design:\r\n 1.\r\n n = number of value in nums;\r\n time complexity : f(n) <= O(n*log(n)) / space complexity : f(n) <= ...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n priority_queue<int> maxpq;\r\n priority_queue<int,vector<int>,greater<int>> minpq;\r\n maxpq.push(nums[0]);maxpq.push(nums[1]);\r\n minpq.push(nums[0]);minpq.push(nums[1]);\r\n for(int i...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n priority_queue<int> maxpq;\r\n priority_queue<int,vector<int>,greater<int>> minpq;\r\n maxpq.push(nums[0]);maxpq.push(nums[1]);\r\n minpq.push(nums[0]);minpq.push(nums[1]);\r\n for(int i...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n priority_queue<int> maxpq;\r\n priority_queue<int,vector<int>,greater<int>> minpq;\r\n maxpq.push(nums[0]);maxpq.push(nums[1]);\r\n minpq.push(nums[0]);minpq.push(nums[1]);\r\n for(int i...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n\r\n int n=nums.size();\r\n\r\n sort(nums.begin(),nums.end());\r\n return ((nums[n-1]*nums[n-2])-(nums[0]*nums[1]));\r\n \r\n }\r\n};", "memory": "23700" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(),nums.end());\r\n int n=nums.size()-1;\r\n int max=1,min=1;\r\n for(int i=0;i<2;i++){\r\n min*=nums[i];\r\n }\r\n for(int i=n;i>n-2;i--){\r\n ...
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(),nums.end());\r\n // int n=nums.size();\r\n int res=(nums[nums.size()-1]*nums[nums.size()-2])-(nums[0]*nums[1]);\r\n return res;\r\n }\r\n};", "memory": "23800" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
0
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n int n = nums.size();\r\n sort(nums.begin(), nums.end());\r\n\r\n return nums[n-1]*nums[n-2] - nums[0]*nums[1];\r\n }\r\n};", "memory": "23800" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
1
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(),nums.end());\r\n int n=nums.size();\r\n int k=(nums[n-1]*nums[n-2])-(nums[0]*nums[1]);\r\n return k;\r\n }\r\n};", "memory": "23900" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
1
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(),nums.end());\r\n int n=nums.size();\r\n\r\n return ( nums[n-1]*nums[n-2] - nums[0]*nums[1]);\r\n \r\n }\r\n};", "memory": "23900" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
1
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(), nums.end());\r\n return (nums[nums.size()-1]*nums[nums.size()-2]-nums[0]*nums[1]);\r\n }\r\n};", "memory": "24000" }
2,042
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p> <ul> <li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li> </ul> <p>Given...
1
{ "code": "class Solution {\r\npublic:\r\n int maxProductDifference(vector<int>& nums) {\r\n sort(nums.begin(), nums.end());\r\n return (nums[nums.size() - 1] * nums[nums.size() - 2]) - (nums[1] * nums[0]);\r\n }\r\n};", "memory": "24000" }
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\r\n\r\nclass Solution {\r\n\r\npublic:\r\n\r\n long long wonderfulSubstrings(string& word) {\r\n\r\n uint16_t freq[1024] = {0}; // parity for 'a'~'j'\r\n\r\n uint16_t sum = 0;\r\n\r\n freq[0] = 1; // empty string counts 1\r\n\r\n ...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\r\nclass Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n uint16_t freq[1024] = {0}; // parity for 'a'~'j'\r\n uint16_t sum = 0;\r\n freq[0] = 1; // empty string counts 1\r\n for (int c : word) {\r\n ...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class\r\n Solution { // https://leetcode.com/problems/number-of-wonderful-substrings/solutions/5090157/3-c-count-freq-for-parity-states-17ms-beats-100/\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n int n = word.size();\r\n long long cnt = 0;\r\n uint16_t freq[1...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n int n=word.size();\r\n long long cnt=0;\r\n uint16_t freq[1024]={0}; //parity for 'a'~'j'\r\n freq[0]=1;\r\n uint16_t sum=0;\r\n for(int c: word){\r\n int index=c-'a';\r...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n int n=word.size();\r\n long long cnt=0;\r\n uint16_t freq[1024]={0}; //parity for 'a'~'j'\r\n freq[0]=1;\r\n uint16_t sum=0;\r\n for(int c: word){\r\n int index=c-'a';\r...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n int n=word.size();\r\n long long cnt=0;\r\n uint16_t freq[1024]={0}; //parity for 'a'~'j'\r\n freq[0]=1;\r\n uint16_t sum=0;\r\n for(int c: word){\r\n int index=c-'a';\r...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n uint16_t freq[1024] = {0}; \r\n uint16_t sum = 0;\r\n freq[0] = 1; \r\n for (int c : word) {\r\n int index = c - 'a';\r\n sum ^= (1 << index);\r\n freq[sum]++;\r...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n int n=word.size();\r\n long long cnt=0;\r\n uint16_t freq[1024]={0}; //parity for 'a'~'j'\r\n freq[0]=1;\r\n uint16_t sum=0;\r\n for(int c: word){\r\n int index=c-'a';\r...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\r\nclass Solution {\r\npublic:\r\n long long wonderfulSubstrings(string& word) {\r\n uint16_t freq[1024] = {0}; // parity for 'a'~'j'\r\n bitset<10> charSet=0;\r\n vector<uint16_t> prefix={0};\r\n vector<char> chars;\r\n u...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n\tstatic size_t wonderfulSubstrings(const std::string& str)\r\n\t{\r\n\t\tsize_t szCount = 0;\r\n\t\tstd::vector<size_t> vCounts(1024);\r\n\t\tvCounts[0] = 1;\r\n\t\tuint32_t uiXor = 0;\r\n\t\tfor (const auto c : str)\r\n\t\t{\r\n\t\t\tuiXor ^= 1 << (c - 'a');\r\n\t\t\tszCoun...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "int cnt[(1<<10)] = {};\r\n\r\nclass Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n // map<int, int> cnt;\r\n memset(cnt, 0, sizeof(cnt));\r\n cnt[0] = 1;\r\n int parity = 0;\r\n long long ans = 0;\r\n for(int i = 0; i < word.size(); i...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n typedef long long ll;\r\n\r\n long long wonderfulSubstrings(string word) {\r\n int n = word.size();\r\n \r\n ll cnt = 0;\r\n\r\n uint16_t freq[1024] = {0};\r\n\r\n freq[0] = 1;\r\n\r\n uint16_t sum = 0;\r\n\r\n for(int ...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n uint16_t mask = 0; /* 记录每个字母出现次数的奇偶性 */\r\n std::vector<unsigned int> count(1 << 10, 0); /* 每种奇偶性情况的出现次数 */\r\n auto result = 0ll;\r\n\r\n count[0] = 1; /* 记录当字符串为空时的情况 */\r\n for(auto i =...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n typedef long long ll;\r\n long long wonderfulSubstrings(string word) {\r\n vector<int>freq(1024,0);\r\n ll res=0;\r\n ll cum_xor=0;\r\n freq[0]=1;\r\n for(auto &ch:word){\r\n int shift = ch-'a';\r\n cum_xor^=(1<<...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string s) {\r\n ios::sync_with_stdio(0); cin.tie(0);\r\n int n = s.size();\r\n long long res = 0;\r\n int val = 0;\r\n vector<int>mp(1024); mp[0] = 1;\r\n for(int i=0;i<n;i++){\r\n val ^= 1...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\n public:\r\n long long wonderfulSubstrings(string word) {\r\n long ans = 0;\r\n int prefix = 0; // the binary prefix\r\n vector<int> count(1024); // the binary prefix count\r\n count[0] = 1; // the empty string \"\"\r\n\r\n for (const char c : word...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n int freq[1024] = {0};\r\n int cur = 0;\r\n long long ans = 0LL;\r\n\r\n freq[0] = 1;\r\n for (char& c : word) {\r\n cur ^= 1 << (c - 'a');\r\n ans += freq[cur];\r\n ...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n int cnt[1024] = {1};\r\n long long ans = 0;\r\n int st = 0;\r\n for (char c : word) {\r\n st ^= 1 << (c - 'a');\r\n ans += cnt[st];\r\n for (int i = 0; i < 10; ++...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n vector<long long> count(1024, 0); // 2^10 to store XOR values\r\n long long result = 0;\r\n int prefixXor = 0;\r\n count[prefixXor] = 1;\r\n\r\n for (char ch : word) {\r\n int c...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
0
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n long long cnt=0;\r\n int x=0,n=word.size();\r\n vector<int>mp(2000);\r\n mp[0]=1;\r\n for(int i=0;i<n;i++){\r\n int d=1<<(word[i]-'a');\r\n x^=d;\r\n cnt+=...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
1
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n vector<long long> count(1024, 0); // 2^10 to store XOR values\r\n long long result = 0;\r\n int prefixXor = 0;\r\n count[prefixXor] = 1;\r\n\r\n for (char ch : word) {\r\n int c...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
1
{ "code": "class Solution {\r\npublic:\r\n int get(char c) {\r\n return c - 'a';\r\n }\r\n\r\n long long wonderfulSubstrings(string word) {\r\n vector<long long> cnt(1024, 0);\r\n cnt[0] = 1;\r\n\r\n int curState = 0;\r\n long long res = 0;\r\n\r\n for (char c : word...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
1
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(string word) {\r\n // Initialize count array for all possible masks\r\n long long count[1024] = {0};\r\n count[0] = 1; // Empty prefix has mask 0\r\n\r\n int mask = 0;\r\n long long result = 0;\r\n\r\n ...
2,044
<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p> <ul> <li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li> </ul> <p>Given a s...
1
{ "code": "class Solution {\r\npublic:\r\n long long wonderfulSubstrings(std::string word) {\r\n const int size = 1 << 10;\r\n long long frequencies[size];\r\n std::fill(frequencies, frequencies + size, 0LL);\r\n\r\n frequencies[0] = 1;\r\n\r\n long long result = 0;\r\n\r\n ...