id
int64 1
3.58k
| problem_description
stringlengths 516
21.8k
| instruction
int64 0
3
| solution_c
dict |
---|---|---|---|
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n \n vector<int> a(26);\n vector<int> target(26);\n \n for(auto x:word2)\n {\n target[x-'a']+=1;\n }\n \n long long ans=0;\n \n int i=0,j=0;\n \n while(j<word1.length()||i<word1.length())\n {\n int flag=1;\n \n for(int k=0;k<26;k++)\n {\n if(a[k]<target[k])\n {\n flag=0;\n }\n }\n \n if(flag)\n {\n ans+=word1.length()-j+1;\n a[word1[i]-'a']-=1;\n i++;\n }\n else\n {\n if(j==word1.length())\n {\n break;\n }\n \n a[word1[j]-'a']+=1;\n j++;\n }\n }\n \n return ans;\n }\n};",
"memory": "13700"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long a[27];\n long long m[27];\n bool check() {\n for (int i = 0; i < 26; i++) {\n if (m[i] < a[i]) return false;\n }\n return true;\n }\n long long validSubstringCount(string w, string word2) {\n for (int i = 0; i < word2.size(); i++) a[word2[i]-'a']++;\n int n = w.size();\n long long j = 0, ans = 0;\n for (int i = 0; i < n; i++) {\n int c = w[i] - 'a';\n m[c]++;\n while (check() && m[w[j]-'a'] > a[w[j]-'a']) {\n m[w[j]-'a']--;\n j++;\n }\n if (check()) {\n ans += j+1;\n }\n }\n return ans;\n }\n};\n/*\naaa\nbaabaabaabaaa\n\n*/",
"memory": "14100"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long a[27];\n long long m[27];\n bool check() {\n for (int i = 0; i < 26; i++) {\n if (m[i] < a[i]) return false;\n }\n return true;\n }\n long long validSubstringCount(string w, string word2) {\n for (int i = 0; i < word2.size(); i++) a[word2[i]-'a']++;\n int n = w.size();\n long long j = 0, ans = 0;\n for (int i = 0; i < n; i++) {\n int c = w[i] - 'a';\n m[c]++;\n while (check() && m[w[j]-'a'] > a[w[j]-'a']) {\n m[w[j]-'a']--;\n j++;\n }\n if (check()) {\n ans += j+1;\n }\n }\n return ans;\n }\n};\n/*\naaa\nbaabaabaabaaa\n\n*/",
"memory": "14200"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n vector<int> v(26);\n for (char c : word2) {\n ++v[c - 'a'];\n }\n long long r = 0;\n const int n = word1.length();\n vector<vector<int>> p(n + 1, vector<int>(26));\n for (int i = 1; i <= n; ++i) {\n p[i] = p[i - 1];\n ++p[i][word1[i - 1] - 'a'];\n }\n for (int i = 0; i < n; ++i) {\n int left = i, right = n - 1;\n while (left <= right) {\n const int mid = (left + right) >> 1;\n bool ok = true;\n for (int j = 0; ok && j < 26; ++j) {\n ok = p[mid + 1][j] - p[i][j] >= v[j];\n \n }\n if (ok) {\n right = mid - 1;\n } else {\n left = mid + 1;\n }\n }\n r += n - right - 1;\n }\n return r;\n }\n};",
"memory": "152200"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n \n vector<int> a(26);\n vector<int> target(26);\n \n for(auto x:word2)\n {\n target[x-'a']+=1;\n }\n \n long long ans=0;\n \n vector<vector<int>> v(word1.length()+1,vector<int>(26));\n \n for(int i=0;i<word1.length();i++)\n {\n v[i+1][word1[i]-'a']=1;\n \n for(int j=0;j<26;j++)\n {\n v[i+1][j]+=v[i][j];\n }\n }\n \n for(int i=1;i<=word1.length();i++)\n {\n long long beg=i,end=word1.length(),val=word1.length()+1;\n \n while(beg<=end)\n {\n long long mid=beg+(end-beg)/2;\n int flag=1;\n \n for(int k=0;k<26;k++)\n {\n int num=v[mid][k]-v[i-1][k];\n \n if(num<target[k])\n {\n flag=0;\n }\n }\n \n if(flag)\n {\n val=min(val,mid);\n end=mid-1;\n }\n else\n {\n beg=mid+1;\n }\n }\n \n ans+=(word1.length()-val+1);\n }\n \n return ans;\n }\n};",
"memory": "152300"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "\nclass Solution {\n int helper(vector<vector<int>>& pre, vector<int>& f, int p) {\n int l = p, r = pre.size() - 2, res = pre.size() - 1;\n auto check = [&](int x) {\n for(int i = 0; i < 26; i++) {\n if(pre[x+1][i] - pre[p][i] < f[i]) return false;\n }\n return true;\n };\n while(l <= r) {\n int m = l + (r - l) / 2;\n bool ok = check(m);\n if(ok) {\n res = m;\n r = m - 1;\n } else l = m + 1;\n }\n \n return pre.size() - 1 - res;\n }\npublic:\n long long validSubstringCount(string word1, string word2) {\n vector<int> freq(26);\n for(auto& ch : word2) freq[ch-'a']++;\n \n vector<int> now(26);\n vector<vector<int>> pre{now};\n for(int i = 0; i < word1.size(); i++) {\n now[word1[i] - 'a']++;\n pre.push_back(now);\n }\n \n long long res = 0;\n for(int i = 0; i < word1.size(); i++) {\n int now = helper(pre,freq,i);\n if(!now) break;\n res += now;\n }\n return res;\n }\n};",
"memory": "178300"
} |
3,573 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n int freq[26] = {0};\n for(auto ch : word2) freq[ch - 'a']++;\n int n = word1.size();\n vector<vector<int>> prefix(n, vector<int>(26, 0));\n for(int i = 0; i < n; i++) {\n if(i == 0) {\n prefix[i][word1[i] - 'a']++;\n }\n else {\n prefix[i] = prefix[i-1];\n prefix[i][word1[i] - 'a']++;\n }\n }\n vector<int> rfreq(26, 0);\n long long res = 0;\n for(int i = 0; i < n; i++) {\n vector<int> temp = rfreq;\n for(int j = 0; j < 26; j++) temp[j] += freq[j];\n int l = i, r = n - 1;\n int ans = -1;\n while(l <= r) {\n int m = l + (r - l) / 2;\n bool flag = true;\n for(int j = 0; j < 26; j++) {\n if(prefix[m][j] < temp[j]) {\n flag = false;\n break;\n }\n }\n if(flag) {\n ans = m;\n r = m - 1;\n }\n else l = m + 1;\n }\n if(ans != -1) {\n res += n - ans;\n }\n rfreq[word1[i] - 'a']++;\n }\n return res;\n }\n};",
"memory": "273700"
} |
3,572 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p><strong>Note</strong> that the memory limits in this problem are <strong>smaller</strong> than usual, so you <strong>must</strong> implement a solution with a <em>linear</em> runtime complexity.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>6</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n int freq[26] = {0};\n for(auto ch : word2) freq[ch - 'a']--;\n long long n = word1.size();\n int r = 0, l = 0;\n int ct = 0;\n for(auto it:freq) if(it==0) ++ct;\n long long res = n * (n + 1) / 2;\n \n while(r < n) {\n int ch = word1[r]-'a';\n freq[ch]++;\n if(freq[ch]==0) {\n ++ct; \n }\n while(ct==26) {\n int c = word1[l] - 'a';\n freq[c]--;\n ++l;\n if(freq[c]<0) {\n --ct;\n break;\n }\n \n }\n res -= (r - l + 1);\n ++r;\n }\n return res;\n }\n};",
"memory": "46300"
} |
3,572 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p><strong>Note</strong> that the memory limits in this problem are <strong>smaller</strong> than usual, so you <strong>must</strong> implement a solution with a <em>linear</em> runtime complexity.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>6</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n int freq[26] = {0};\n for(auto ch : word2) freq[ch - 'a']--;\n long long n = word1.size();\n int r = 0, l = 0;\n long long res = n * (n + 1) / 2;\n \n while(r < n) {\n int ch = word1[r]-'a';\n freq[ch]++;\n int ct = 0;\n for(auto it:freq) if(it >= 0) ++ct;\n while(ct==26) {\n int c = word1[l] - 'a';\n freq[c]--;\n ++l;\n if(freq[c]<0) {\n --ct;\n break;\n }\n \n }\n res -= (r - l + 1);\n ++r;\n }\n return res;\n }\n};",
"memory": "46400"
} |
3,572 | <p>You are given two strings <code>word1</code> and <code>word2</code>.</p>
<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> can be rearranged to have <code>word2</code> as a <span data-keyword="string-prefix">prefix</span>.</p>
<p>Return the total number of <strong>valid</strong> <span data-keyword="substring-nonempty">substrings</span> of <code>word1</code>.</p>
<p><strong>Note</strong> that the memory limits in this problem are <strong>smaller</strong> than usual, so you <strong>must</strong> implement a solution with a <em>linear</em> runtime complexity.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "bcca", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The only valid substring is <code>"bcca"</code> which can be rearranged to <code>"abcc"</code> having <code>"abc"</code> as a prefix.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<p>All the substrings except substrings of size 1 and size 2 are valid.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">word1 = "abcabc", word2 = "aaabc"</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length <= 10<sup>6</sup></code></li>
<li><code>1 <= word2.length <= 10<sup>4</sup></code></li>
<li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long validSubstringCount(string word1, string word2) {\n \n vector<int> a(26);\n vector<int> target(26);\n \n for(auto x:word2)\n {\n target[x-'a']+=1;\n }\n \n long long ans=0;\n \n int i=0,j=0;\n \n while(j<word1.length()||i<word1.length())\n {\n int flag=1;\n \n for(int k=0;k<26;k++)\n {\n if(a[k]<target[k])\n {\n flag=0;\n }\n }\n \n if(flag)\n {\n ans+=word1.length()-j+1;\n a[word1[i]-'a']-=1;\n i++;\n }\n else\n {\n if(j==word1.length())\n {\n break;\n }\n \n a[word1[j]-'a']+=1;\n j++;\n }\n }\n \n return ans;\n }\n};",
"memory": "46500"
} |