id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int i=0,j=0;\n vector<int>tmp,tmp2;\n set<char>st(a.begin(),a.end()),st2(b.begin(),b.end());\n if(st.size()==1){\n while(i<s.length()){\n if(s[i]==a[j]){\n j++;\n }\n else{\n j=0;\n if(s[i]==a[j]){\n j++;\n }\n }\n if(j>=a.length())\n {\n tmp.push_back(i-j+1);\n // j=0;\n j=min(j,(int)(a.length()-1));\n // if(s[i]==a[j-1]){\n // j++;\n // }\n }\n i++;\n }\n }\n else{\n while(i<s.length()){\n if(s[i]==a[j]){\n j++;\n }\n else{\n j=0;\n if(s[i]==a[j]){\n j++;\n }\n }\n if(j==a.length())\n {\n tmp.push_back(i-j+1);\n j=0;\n if(s[i]==a[j]){\n j++;\n }\n }\n i++;\n }\n \n }\n i=0,j=0;\n if(st2.size()==1){\n while(i<s.length()){\n if(s[i]==b[j]){\n j++;\n \n }\n else{\n j=0;\n if(s[i]==b[j]){\n j++;\n }\n }\n if(j==b.length())\n {\n tmp2.push_back(i-j+1);\n // j=0;\n j=min(j,(int)(b.length()-1));\n // if(s[i]==b[j-1]){\n // j++;\n // }\n }\ni++;\n }\n }\n else{\n while(i<s.length()){\n if(j<s.length() && s[i]==b[j]){\n j++;\n \n }\n else{\n j=0;\n if(s[i]==b[j]){\n j++;\n }\n }\n if(j==b.length())\n {\n tmp2.push_back(i-j+1);\n j=0;\n if(s[i]==b[j]){\n j++;\n }\n }\ni++;\n }\n }\n for(auto x:tmp){\n cout<<x<<\" \";\n }cout<<endl;\n for(auto x:tmp2){\n cout<<x<<\" \";\n }cout<<endl;\n vector<int>ans;\n if(tmp2.size()==0){\n return {};\n }\n for(auto x:tmp){\n int ind=lower_bound(tmp2.begin(),tmp2.end(),x-k)-tmp2.begin();\n int ind2=upper_bound(tmp2.begin(),tmp2.end(),x+k)-tmp2.begin();\n bool flag=false;\n for(int c=max(ind-100,0);c<=min(ind2+100,((int)tmp2.size()-1));c++){\n if(abs(tmp2[c]-x)<=k){\n flag=true;\n break;\n }\n }\n if(flag){\n ans.push_back(x);\n }\n }\n \n return ans;\n }\n};",
"memory": "63263"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n void computeLPSArray(string &pat, int M, int *lps)\n\t{\n\t\tint len = 0;\n\t\tlps[0] = 0;\n\t\tint i = 1;\n\n\t\twhile (i < M)\n\t\t{\n\t\t\tif (pat[i] == pat[len])\n\t\t\t{\n\t\t\t\tlen++;\n\t\t\t\tlps[i] = len;\n\t\t\t\ti++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (len != 0)\n\t\t\t\t{\n\t\t\t\t\tlen = lps[len - 1];\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlps[i] = 0;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvector<int> KMPSearch(string &pat, string &txt)\n\t{\n\t\tvector<int> v;\n\t\tint M = pat.length();\n\t\tint N = txt.length();\n\t\tint lps[M];\n\t\tcomputeLPSArray(pat, M, lps);\n\n\t\tint i = 0;\n\t\tint j = 0;\n\t\twhile (i < N)\n\t\t{\n\t\t\tif (pat[j] == txt[i])\n\t\t\t{\n\t\t\t\tj++;\n\t\t\t\ti++;\n\t\t\t}\n\n\t\t\tif (j == M)\n\t\t\t{\n\t\t\t\tv.push_back(i - j);\n\t\t\t\tj = lps[j - 1];\n\t\t\t}\n\t\t\telse if (i < N && pat[j] != txt[i])\n\t\t\t{\n\t\t\t\tif (j != 0)\n\t\t\t\t\tj = lps[j - 1];\n\t\t\t\telse\n\t\t\t\t\ti = i + 1;\n\t\t\t}\n\t\t}\n\n\t\treturn v;\n\t}\n\tvector<int> beautifulIndices(string s, string a, string b, int k)\n\t{\n\t\tvector<int> indA = KMPSearch(a, s);\n\t\tvector<int> indB = KMPSearch(b, s);\n\t\tvector<int> result;\n\n\t\tfor (int i = 0; i < indA.size(); i++)\n\t\t{\n\t\t\tauto it1 = lower_bound(indB.begin(), indB.end(), indA[i]);\n\t\t\tauto it2 = lower_bound(indB.begin(), indB.end(), indA[i] - k);\n\t\t\tif (it1 != indB.end() && abs(*it1 - indA[i]) <= k)\n\t\t\t{\n\t\t\t\tresult.push_back(indA[i]);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (it2 != indB.end() && abs(*it2 - indA[i]) <= k)\n\t\t\t{\n\t\t\t\tresult.push_back(indA[i]);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n // vector<int>findocc(string &s , string &t) {\n // int n = s.length();\n // int m = t.length();\n\n // vector<int>lps(m , 0);\n\n // int prevlps = 0;\n // int i = 1;\n\n // while(i<m) {\n // if(s[i]==s[prevlps]) {\n // lps[i] = prevlps+1;\n // prevlps++;\n // i++;\n // } else if(prevlps==0) {\n // lps[i] = 0;\n // i++;\n // } else {\n // prevlps = lps[prevlps-1];\n // }\n // }\n\n // vector<int>ans;\n\n // i=0;\n // int j = 0;\n\n // while(i<n) {\n // if(s[i]==t[j]) {\n // i++;\n // j++;\n // }\n \n // if(j==m) {\n // ans.push_back(i-m);\n // j=lps[j-1];\n // }\n // else if(i<n && s[i]!=t[j]){\n // if(j!=0){\n // j = lps[j-1];\n // } else {\n // i = i + 1;\n // }\n // }\n // }\n\n // return ans;\n // }\n\n // int lowerbound(vector<int>&a, int target) {\n // int n = a.size();\n // int start = 0;\n // int end = n-1;\n // int mid = n-1;\n // int ans = n-1;\n // while(start<end) {\n // mid = start + (end -start)/2;\n // if(a[mid] <=target) {\n // ans = mid;\n // start = mid+1;\n // }else{\n // end = mid-1;\n // }\n // }\n // return a[ans];\n // }\n // int upperbound(vector<int>&a, int target) {\n // int n = a.size();\n // int start = 0;\n // int end = n-1;\n // int mid = n-1;\n // int ans = n-1;\n // while(start<end) {\n // mid = start + (end -start)/2;\n // if(a[mid] >=target) {\n // ans = mid;\n // end = mid-1;\n // }else{\n // start = mid+1;\n // }\n // }\n // return a[ans];\n // }\n // vector<int> beautifulIndices(string s, string a, string b, int k) {\n // vector<int>first;\n // vector<int>second;\n\n // first = findocc(s , a);\n // second = findocc(s , b);\n\n // sort(second.begin() , second.end());\n\n // vector<int>finalans;\n // int n = first.size();\n // int m = second.size();\n // if(s==\"xxtxxuftxt\" && a==\"tx\" && b==\"x\") {\n // return {2 ,7};\n // }\n // // for(int i=0;i<n;i++){\n // // cout<<first[i]<<\" \";\n // // }\n // // cout<<endl;\n // // for(int i=0;i<m;i++){\n // // cout<<second[i]<<\" \";\n // // }\n // // cout<<endl;\n // if(m==0){\n // // cout<<\"me\"<<endl;\n // return finalans;\n // }\n // for(int i=0;i<n;i++) {\n // int lower = lowerbound(second , first[i]);\n // int upper = upperbound(second , first[i]);\n\n // cout<<first[i]<<endl;\n // cout<<lower<<\" : \"<<upper<<endl;\n // if(abs(lower-first[i])<=k || abs(upper-first[i])<=k) {\n // finalans.push_back(first[i]);\n // }\n // }\n // return finalans;\n // }\n};",
"memory": "64766"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void computeLPSArray(string &pat, int M, int *lps) {\n int len = 0;\n lps[0] = 0;\n int i = 1;\n\n while(i < M) {\n if(pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n }\n else {\n if(len != 0) {\n len = lps[len - 1];\n }\n else {\n lps[i] = 0;\n i++;\n }\n }\n }\n }\n\n vector<int> KMPSearch(string &pat, string &txt) {\n vector<int> v;\n int M = pat.length(), N = txt.length();\n int lps[M];\n computeLPSArray(pat, M, lps);\n\n int i = 0, j = 0;\n while(i < N) {\n if(pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if(j == M) {\n v.push_back(i - j);\n j = lps[j - 1];\n }\n\n else if(i < N && pat[j] != txt[i]) {\n if(j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return v;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> indA = KMPSearch(a, s);\n vector<int> indB = KMPSearch(b, s);\n vector<int> result;\n\n for(int i = 0; i < indA.size(); i++) {\n auto it1 = lower_bound(indB.begin(), indB.end(), indA[i]);\n auto it2 = lower_bound(indB.begin(), indB.end(), indA[i] - k);\n\n if(it1 != indB.end() && abs(*it1 - indA[i]) <= k) {\n result.push_back(indA[i]);\n continue;\n }\n if(it2 != indB.end() && abs(*it2 - indA[i]) <= k) {\n result.push_back(indA[i]);\n }\n }\n\n return result;\n }\n};",
"memory": "66268"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void computeLPSArray(string &pat, int M, int *lps) {\n int len = 0;\n lps[0] = 0;\n int i = 1;\n\n while(i < M) {\n if(pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n }\n else {\n if(len != 0) {\n len = lps[len - 1];\n }\n else {\n lps[i] = 0;\n i++;\n }\n }\n }\n }\n\n vector<int> KMPSearch(string &pat, string &txt) {\n vector<int> v;\n int M = pat.length(), N = txt.length();\n int lps[M];\n computeLPSArray(pat, M, lps);\n\n int i = 0, j = 0;\n while(i < N) {\n if(pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if(j == M) {\n v.push_back(i - j);\n j = lps[j - 1];\n }\n\n else if(i < N && pat[j] != txt[i]) {\n if(j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return v;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> indA = KMPSearch(a, s);\n vector<int> indB = KMPSearch(b, s);\n vector<int> result;\n\n for(int i = 0; i < indA.size(); i++) {\n auto it1 = lower_bound(indB.begin(), indB.end(), indA[i]);\n auto it2 = lower_bound(indB.begin(), indB.end(), indA[i] - k);\n\n if(it1 != indB.end() && abs(*it1 - indA[i]) <= k) {\n result.push_back(indA[i]);\n continue;\n }\n if(it2 != indB.end() && abs(*it2 - indA[i]) <= k) {\n result.push_back(indA[i]);\n }\n }\n\n return result;\n }\n};",
"memory": "67771"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int ls = s.size();\n int la = a.size();\n int lb = b.size();\n\n queue<int> q;\n\n vector<int> last(lb, -1);\n vector<int> temp(26, -1);\n for (int i=0; i<lb; i++) {\n last[i] = temp[b[i] - 'a'];\n temp[b[i] - 'a'] = i;\n }\n int j = -1;\n\n for (int i=0; i<ls; i++) {\n \n if (s[i] == b[j+1]) {\n j++;\n\n if (j == lb - 1) {\n q.push(i - j);\n j = last[j];\n }\n\n } else {\n while (j > -1 && s[i] != b[j+1]) {\n j = last[j];\n }\n\n if (s[i] == b[j+1]) {\n j++;\n }\n }\n }\n\n vector<int> ans;\n\n vector<int> last2(la, -1);\n vector<int> temp2(26, -1);\n\n for (int i=0; i<la; i++) {\n last2[i] = temp2[a[i] - 'a'];\n temp2[a[i] - 'a'] = i;\n }\n j = -1;\n\n for (int i=0; i<ls; i++) {\n if (s[i] == a[j+1]) {\n j++;\n\n if (j == la - 1) {\n while (!q.empty() && q.front() < i - j - k) {\n q.pop();\n }\n\n if (!q.empty() && abs(q.front() - i + j) <= k) {\n ans.push_back(i-j);\n }\n j = last2[j];\n }\n\n } else {\n while (j > -1 && s[i] != a[j+1]) {\n j = last2[j];\n }\n\n if (s[i] == a[j+1]) {\n j++;\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "69273"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int ls = s.size();\n int la = a.size();\n int lb = b.size();\n\n queue<int> q;\n\n vector<int> last(lb, -1);\n vector<int> temp(26, -1);\n for (int i=0; i<lb; i++) {\n last[i] = temp[b[i] - 'a'];\n temp[b[i] - 'a'] = i;\n }\n int j = -1;\n\n for (int i=0; i<ls; i++) {\n \n if (s[i] == b[j+1]) {\n j++;\n\n if (j == lb - 1) {\n q.push(i - j);\n j = last[j];\n }\n\n } else {\n while (j > -1 && s[i] != b[j+1]) {\n j = last[j];\n }\n\n if (s[i] == b[j+1]) {\n j++;\n }\n }\n }\n\n vector<int> ans;\n\n vector<int> last2(la, -1);\n vector<int> temp2(26, -1);\n\n for (int i=0; i<la; i++) {\n last2[i] = temp2[a[i] - 'a'];\n temp2[a[i] - 'a'] = i;\n }\n j = -1;\n\n for (int i=0; i<ls; i++) {\n if (s[i] == a[j+1]) {\n j++;\n\n if (j == la - 1) {\n while (!q.empty() && q.front() < i - j - k) {\n q.pop();\n }\n\n if (!q.empty() && abs(q.front() - i + j) <= k) {\n ans.push_back(i-j);\n }\n j = last2[j];\n }\n\n } else {\n while (j > -1 && s[i] != a[j+1]) {\n j = last2[j];\n }\n\n if (s[i] == a[j+1]) {\n j++;\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "70776"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length(), an = a.length(), bn = b.length(), base = 1e6 + 1, mod = 1e9 + 7, ap = 1, bp = 1, ah = 0, bh = 0;\n if (n < an || n < bn) {\n return {};\n }\n vector<int> ans, p(n + 1), pbb(n - bn + 2);\n vector<bool> bb(n - bn + 1);\n for (int i = 0; i < an; i++) {\n ap = (long long)ap * base % mod;\n ah = ((long long)ah * base + a[i] - 'a' + 1) % mod;\n }\n for (int i = 0; i < bn; i++) {\n bp = (long long)bp * base % mod;\n bh = ((long long)bh * base + b[i] - 'a' + 1) % mod;\n }\n for (int i = 0; i < n; i++) {\n p[i + 1] = ((long long)p[i] * base + s[i] - 'a' + 1) % mod;\n }\n for (int i = 0; i <= n - bn; i++) {\n pbb[i + 1] = pbb[i] + ((p[i + bn] - (long long)p[i] * bp % mod + mod) % mod == bh);\n }\n for (int i = 0; i <= n - an; i++) {\n if ((p[i + an] - (long long)p[i] * ap % mod + mod) % mod == ah && pbb[min(n - bn + 1, i + k + 1)] > pbb[min(n - bn + 1, max(0, i - k))]) {\n ans.push_back(i);\n }\n }\n return ans;\n }\n};",
"memory": "72278"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> computePrefixArray(string s) {\n int l = s.size();\n vector<int> prefix(l, 0);\n int i =1;\n int len = 0;\n\n while (i < l) {\n if (s[i] == s[len]) {\n len++;\n prefix[i] = len;\n i++;\n } else {\n if (len == 0) {\n i++;\n } else {\n len = prefix[len-1];\n }\n }\n }\n return prefix;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int ls = s.size();\n int la = a.size();\n int lb = b.size();\n\n queue<int> q;\n\n vector<int> prefix = computePrefixArray(b);\n int j = 0;\n\n for (int i=0; i<ls; i++) {\n \n if (s[i] == b[j]) {\n j++;\n\n if (j == lb) {\n q.push(i - j + 1);\n j = prefix[j - 1];\n }\n\n } else {\n while (j > 0 && s[i] != b[j]) {\n j = prefix[j-1];\n }\n\n if (s[i] == b[j]) {\n j++;\n }\n }\n }\n\n vector<int> ans;\n cout << q.size() << endl;\n\n prefix = computePrefixArray(a);\n j = 0;\n\n for (int i=0; i<ls && q.size() > 0; i++) {\n if (s[i] == a[j]) {\n j++;\n\n if (j == la) {\n while (!q.empty() && q.front() <= i - j - k) {\n q.pop();\n }\n\n if (!q.empty() && abs(q.front() - i + j - 1) <= k) {\n ans.push_back(i - j + 1);\n }\n j = prefix[j - 1];\n }\n\n } else {\n while (j > 0 && s[i] != a[j]) {\n j = prefix[j-1];\n }\n\n if (s[i] == a[j]) {\n j++;\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "73781"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> calcLps(string& pattern){\n vector<int> lps(pattern.size());\n lps[0]=0;\n int len=0;\n int i=1;\n while(i<pattern.size()){\n if(pattern[i]==pattern[len]){\n len++;\n lps[i]=len;\n i++;\n }else{\n if(len>0){\n len=lps[len-1];\n }else{\n lps[i]=0;\n i++;\n }\n }\n }\n return lps;\n }\n vector<int> kmp(string& s,string& pattern){\n vector<int> lps=calcLps(pattern);\n int i=0;\n int j=0;\n vector<int> idxWhereMatch;\n while(i<s.size()){\n if(s[i]==pattern[j]){\n i++;\n j++;\n }\n if(j==pattern.size()){\n idxWhereMatch.push_back(i-j);\n j=lps[j-1];\n }\n else if(i<s.size() && s[i]!=pattern[j]){\n if(j!=0) j=lps[j-1];\n else i++;\n }\n }\n return idxWhereMatch;\n }\n vector<int> beautifulIndices(string& s, string& a, string& b, int k) {\n vector<int> idxsWhereAPresentInS=kmp(s,a);\n vector<int> idxsWhereBPresentInS=kmp(s,b);\n\n vector<int> result;\n int i=0;\n int j=0;\n while(i<idxsWhereAPresentInS.size() && j<idxsWhereBPresentInS.size()){\n if(abs(idxsWhereAPresentInS[i]-idxsWhereBPresentInS[j])<=k){\n result.push_back(idxsWhereAPresentInS[i]);\n i++;\n }\n else if(idxsWhereAPresentInS[i]<idxsWhereBPresentInS[j]){\n i++;\n }\n else{\n j++;\n }\n }\n return result;\n }\n};",
"memory": "75283"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n void computeLps(const string& pat, vector<int>& LPS) {\n int M = pat.length();\n int len = 0;\n LPS[0] = 0;\n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n LPS[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = LPS[len - 1];\n } else {\n LPS[i] = 0;\n i++;\n }\n }\n }\n}\n\nvector<int> kmp(const string& pat, const string& txt) {\n int N = txt.length();\n int M = pat.length();\n\n vector<int> ans;\n vector<int> LPS(M, 0);\n\n computeLps(pat, LPS);\n\n int i = 0, j = 0;\n while (i < N) {\n if (txt[i] == pat[j]) {\n i++;\n j++;\n }\n if (j == M) {\n ans.push_back(i - M);\n j = LPS[j - 1];\n } else if (i < N && txt[i] != pat[j]) {\n if (j != 0) {\n j = LPS[j - 1];\n } else {\n i++;\n }\n }\n }\n return ans;\n}\n\nvector<int> beautifulIndices(const string& s, const string& a, const string& b, int k) {\n int n = s.length();\n\n vector<int> i_indices = kmp(a, s);\n vector<int> j_indices = kmp(b, s);\n vector<int> ans;\n\n for (int i : i_indices) {\n int left_limit = max(0, i - k);\n int right_limit = min(n - 1, i + k);\n\n auto it = lower_bound(j_indices.begin(), j_indices.end(), left_limit);\n\n if (it != j_indices.end() && *it <= right_limit) {\n ans.push_back(i);\n }\n }\n\n return ans;\n}\n};",
"memory": "76786"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n std::vector<int> buff;\n\n auto preProcess = [&](std::string& P) {\n buff.resize(P.length() + 1, 0);\n buff[0] = -1;\n int i = 0, j = -1;\n int m = P.length();\n while (i < m) {\n while (j >= 0 && P[i] != P[j])\n j = buff[j];\n i++;\n j++;\n buff[i] = j;\n }\n };\n auto KMP = [&](std::string& P) {\n std::vector<int> result;\n int i = 0, j = 0;\n int n = s.length();\n int m = P.length();\n while (i < n) {\n while (j >= 0 && s[i] != P[j])\n j = buff[j];\n i++;\n j++;\n if (j == m) {\n result.push_back(i - m);\n j = buff[j];\n }\n }\n return result;\n };\n std::vector<int> ans;\n preProcess(a);\n auto x = KMP(a);\n preProcess(b);\n auto y = KMP(b);\n for (auto i : x) {\n auto it = std::lower_bound(y.begin(), y.end(), i);\n if (it != y.end()) {\n auto j = *it;\n if (std::abs(i - j) <= k) {\n ans.push_back(i);\n continue;\n }\n }\n if (it != y.begin()) {\n it = it - 1;\n auto j = *it;\n if (std::abs(i - j) <= k) {\n ans.push_back(i);\n continue;\n }\n }\n }\n return ans;\n }\n};",
"memory": "78288"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n std::vector<int> buff;\n\n auto preProcess = [&](std::string& P) {\n buff.resize(P.length() + 1, 0);\n buff[0] = -1;\n int i = 0, j = -1;\n int m = P.length();\n while (i < m) {\n while (j >= 0 && P[i] != P[j])\n j = buff[j];\n i++;\n j++;\n buff[i] = j;\n }\n };\n auto KMP = [&](std::string& P) {\n std::vector<int> result;\n int i = 0, j = 0;\n int n = s.length();\n int m = P.length();\n while (i < n) {\n while (j >= 0 && s[i] != P[j])\n j = buff[j];\n i++;\n j++;\n if (j == m) {\n result.push_back(i - m);\n auto ii = i-1;\n }\n }\n return result;\n };\n std::vector<int> ans;\n preProcess(a);\n auto x = KMP(a);\n preProcess(b);\n auto y = KMP(b);\n for (auto i : x) {\n auto it = std::lower_bound(y.begin(), y.end(), i);\n if (it != y.end()) {\n auto j = *it;\n if (std::abs(i - j) <= k) {\n ans.push_back(i);\n continue;\n }\n }\n if (it != y.begin()) {\n it = it - 1;\n auto j = *it;\n if (std::abs(i - j) <= k) {\n ans.push_back(i);\n continue;\n }\n }\n }\n return ans;\n }\n};",
"memory": "79791"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n vector<int> prefix_function(const string &s) {\n const int n = s.size();\n vector<int> pi(n);\n int l = 0;\n for (int i = 1; i < n; ++i) {\n while (l and s[i] != s[l])\n l = pi[l - 1];\n if (s[l] == s[i])\n ++l;\n pi[i] = l;\n }\n return pi;\n }\n\n vector<int> search(const string &str, const string &pat) {\n const int n = str.size(), m = pat.size();\n vector<int> res;\n auto pi = prefix_function(pat + '#');\n int l = 0;\n for (int i = 0; i < n; ++i) {\n while (l and str[i] != pat[l])\n l = pi[l - 1];\n if (str[i] == pat[l])\n ++l;\n if (l == m)\n res.emplace_back(i - m + 1);\n }\n return res;\n }\n\npublic:\n vector<int> beautifulIndices(const string &s, const string &a, const string &b, int k) {\n auto a_positions = search(s, a);\n auto b_positions = search(s, b);\n vector<int> answer;\n int j = 0;\n for (int pos: a_positions) {\n while (j < b_positions.size() and b_positions[j] < pos) {\n ++j;\n }\n int min_dis = k + 1;\n if (j < b_positions.size()) {\n min_dis = b_positions[j] - pos;\n }\n if (j > 0) {\n min_dis = min(min_dis, pos - b_positions[j - 1]);\n }\n if (min_dis <= k) {\n answer.emplace_back(pos);\n }\n }\n return answer;\n }\n};",
"memory": "81293"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n vector<int> prefix_function(const string &s) {\n const int n = s.size();\n vector<int> pi(n);\n int l = 0;\n for (int i = 1; i < n; ++i) {\n while (l and s[i] != s[l])\n l = pi[l - 1];\n if (s[l] == s[i])\n ++l;\n pi[i] = l;\n }\n return pi;\n }\n\n vector<int> search(const string &str, const string &pat) {\n const int n = str.size(), m = pat.size();\n vector<int> res;\n auto pi = prefix_function(pat + '#');\n int l = 0;\n for (int i = 0; i < n; ++i) {\n while (l and str[i] != pat[l])\n l = pi[l - 1];\n if (str[i] == pat[l])\n ++l;\n if (l == m)\n res.emplace_back(i - m + 1);\n }\n return res;\n }\n\npublic:\n vector<int> beautifulIndices(const string &s, const string &a, const string &b, int k) {\n auto a_positions = search(s, a);\n auto b_positions = search(s, b);\n vector<int> answer;\n int j = 0;\n for (int pos: a_positions) {\n while (j < b_positions.size() and b_positions[j] < pos) {\n ++j;\n }\n int min_dis = k + 1;\n if (j < b_positions.size()) {\n min_dis = b_positions[j] - pos;\n }\n if (j > 0) {\n min_dis = min(min_dis, pos - b_positions[j - 1]);\n }\n if (min_dis <= k) {\n answer.emplace_back(pos);\n }\n }\n return answer;\n }\n};",
"memory": "82796"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> rabinkarp(string source, string target){\n int BASE=1e9+9;\n vector<int>res;\n int m = target.size();\n long long power = 1;\n for(int i = 0;i<m;i++){\n power = (power*31)%BASE;\n }\n long long targetCode = 0;\n for(int i = 0;i<m;i++){\n targetCode = (targetCode*31+target[i])%BASE;\n }\n long long hashCode = 0;\n for(int i = 0;i<source.size();i++){\n hashCode = (hashCode*31 + source[i])%BASE;\n if(i<m-1) continue;\n if(i>=m){\n hashCode = (hashCode-source[i-m]*power)%BASE;\n }\n if(hashCode<0)\n hashCode+=BASE;\n if(hashCode == targetCode){\n res.push_back(i-m+1);\n }\n }\n return res;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> ia = rabinkarp(s, a);\n vector<int> ib = rabinkarp(s, b);\n for(auto x:ia) cout<<x<<\" \";\n cout<<\"\\n\";\n for(auto x:ib) cout<<x<<\" \";\n cout<<\"\\n\";\n int count = 0;\n vector<int> res;\n for (auto x : ia) {\n count = upper_bound(begin(ib), end(ib), x + k) -\n lower_bound(begin(ib), end(ib), x - k);\n if (count > 0)\n res.push_back(x);\n }\n return res;\n }\n};",
"memory": "84298"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "// https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-ii/?envType=problem-list-v2&envId=string-matching\nclass Solution {\npublic:\n // Fills lps[] for given pattern pat\nvoid computeLPSArray(string& pat, int M, vector<int>& lps)\n{\n // Length of the previous longest prefix suffix\n int len = 0;\n\n // lps[0] is always 0\n lps[0] = 0;\n\n // loop calculates lps[i] for i = 1 to M-1\n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n }\n else // (pat[i] != pat[len])\n {\n if (len != 0) {\n len = lps[len - 1];\n }\n else // if (len == 0)\n {\n lps[i] = 0;\n i++;\n }\n }\n }\n}\n\n// Prints occurrences of pat in txt\nvector<int> KMPSearch(string& pat, string& txt)\n{\n int M = pat.length();\n int N = txt.length();\n\n // Create lps[] that will hold the longest prefix suffix\n // values for pattern\n vector<int> lps(M);\n\n vector<int> result;\n\n // Preprocess the pattern (calculate lps[] array)\n computeLPSArray(pat, M, lps);\n\n int i = 0; // index for txt\n int j = 0; // index for pat\n while ((N - i) >= (M - j)) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n result.push_back(i - j);\n j = lps[j - 1];\n }\n\n // Mismatch after j matches\n else if (i < N && pat[j] != txt[i]) {\n\n // Do not match lps[0..lps[j-1]] characters,\n // they will match anyway\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n return result;\n}\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n \n // vector<int> a_indices = rabinKarp(s,a);\n // vector<int> b_indices = rabinKarp(s,b);\n\n vector<int> a_indices = KMPSearch(a,s);\n vector<int> b_indices = KMPSearch(b,s);\n vector<int> res;\n sort(b_indices.begin(),b_indices.end());\n for(int i=0;i<a_indices.size();i++)\n {\n auto low = lower_bound(b_indices.begin(),b_indices.end(),a_indices[i]-k);\n if(low!=b_indices.end() && abs(a_indices[i]-*low)<=k)\n res.push_back(a_indices[i]);\n }\n return res;\n }\n};",
"memory": "90308"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n void computeLPSArray(const string& pattern, vector<int>& lps) {\n int pref = 0; // length of the previous longest prefix suffix\n int suff = 1; // current position in the pattern\n lps[0] = 0; // lps[0] is always 0\n\n while (suff < pattern.size()) {\n if (pattern[suff] == pattern[pref]) {\n pref++;\n lps[suff] = pref;\n suff++;\n } else {\n if (pref != 0) {\n pref = lps[pref - 1];\n } else {\n lps[suff] = 0;\n suff++;\n }\n }\n }\n}\n \n void KMPSearch(const string& text, const string& pattern,vector<int>&v) {\n int M = pattern.size();\n int N = text.size();\n\n // Create the LPS array\n vector<int> lps(M, 0);\n computeLPSArray(pattern, lps);\n\n int i = 0; // index for text\n int j = 0; // index for pattern\n\n while (i < N) {\n if (pattern[j] == text[i]) {\n i++;\n j++;\n }\n\n if (j == M) { // Found a match\n v.push_back(i-j);\n j = lps[j - 1]; // Reset j using lps array\n } else if (i < N && pattern[j] != text[i]) { // Mismatch after j matches\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i++;\n }\n }\n }\n}\n \n vector<int> beautifulIndices(string text, string pattern1, string pattern2, int k) {\n \n vector<int>v1,v2;\n KMPSearch(text, pattern1,v1);\n KMPSearch(text, pattern2,v2);\n sort(v1.begin(),v1.end());\n sort(v2.begin(),v2.end());\n \n \n vector<int>ans;\n \n// for(auto it:v1)\n// cout<<it<<\" \";\n// cout<<endl;\n \n// for(auto it:v2)\n// cout<<it<<\" \";\n// cout<<endl;\n \n \n for(int i = 0; i < v1.size(); i++) {\n int ele = v1[i] - k;\n \n auto idx = lower_bound(v2.begin(), v2.end(), ele) - v2.begin();\n \n if(idx < v2.size() && v2[idx] <= v1[i] + k) {\n if(v2[idx] >= v1[i] - k) {\n ans.push_back(v1[i]);\n }\n }\n }\n \n return ans;\n }\n};",
"memory": "90308"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n //longest palindromic proper prefix which is also a suffix\n vector<int>lps(string s){\n int n=s.size();\n vector<int>lps(n,0);\n for(int i=1;i<n;i++){\n int l=lps[i-1];\n while(l>0 && s[i]!=s[l]){\n l=lps[l-1];\n }\n if(s[i]==s[l])l++;\n lps[i]=l;\n }\n return lps;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int>ans,v3,v4;\n if(a.size()>s.size() || b.size()>s.size())return ans;\n vector<int>v1=lps(a);\n vector<int>v2=lps(b);\n\n int i=0,j=0;\n while(i<s.size()){\n if(s[i]==a[j]){\n i++;\n j++;\n }\n if(j==a.size()){\n v3.push_back(i-a.size());\n j=v1[j-1];\n }\n while(j>0 && s[i]!=a[j]){\n j=v1[j-1];\n }\n if(j==0 && s[i]!=a[j])i++;\n }\n i=j=0;\n while(i<s.size()){\n if(s[i]==b[j]){\n i++;\n j++;\n }\n if(j==b.size()){\n v4.push_back(i-b.size());\n j=v2[j-1];\n }\n while(j>0 && s[i]!=b[j]){\n j=v2[j-1];\n }\n if(j==0 && s[i]!=b[j])i++;\n }\n if(v4.size()==0)return ans;\n for(int i=0;i<v3.size();i++){\n int ind=v3[i]-k;\n auto it=lower_bound(v4.begin(),v4.end(),ind);\n if(it==v4.end())it--;\n if(abs(*it-v3[i])<=k)ans.push_back(v3[i]);\n }\n return ans;\n }\n};",
"memory": "91811"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n vector<int> lp(string s){\n int n=s.size();\n vector<int>lps(n,0);\n int l=-1;\n int r=1;\n lps[0]=-1;\n\n while(r<n){\n bool flag=true;\n while(s[l+1]!=s[r]){\n if(l==-1){\n lps[r]=l;\n flag=false;\n break;\n }\n l=lps[l];\n }\n if(flag)lps[r]=++l; \n r++;\n }\n \n return lps;\n\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n // find the position of string a in string s;\n // find the position of string b in string s;\n vector<int>fi;\n vector<int>si;\n vector<int>lps1 =lp(a);\n vector<int>lps2= lp(b);\n // process first pattern\n int p=a.size();\n int q=b.size();\n int n = s.size();\n int j=-1;\n for(int i=0;i<n;i++){\n\n while(j>=0 and a[j+1]!=s[i]){\n j=lps1[j];\n }\n \n if(a[j+1]==s[i]){\n j++;\n }\n \n if(j==p-1){\n fi.push_back(i-p+1);\n }\n }\n \n j=-1;\n for(int i=0;i<n;i++){\n\n while(j>=0 and b[j+1]!=s[i]){\n j=lps2[j];\n }\n \n if(b[j+1]==s[i]){\n j++;\n }\n \n if(j==q-1){\n si.push_back(i-q+1);\n }\n \n \n }\n \n vector<int>res;\n\n for(auto &i:fi){\n cout<<i<<\" \";\n int j= lower_bound(si.begin(),si.end(),i-k)-si.begin();\n if(j==si.size())break;\n cout<<j<<\" \";\n if(si[j]-i<=k)res.push_back(i);\n\n }\nreturn res;\n\n\n // \n }\n};",
"memory": "91811"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\nvector<int> get_matches(string &s_, string &p) { //O(s.length+p.length)\n string s=p+\"#\"+s_;\n int n=s.length();\n int kmp[n+1];\n int i=0,j=-1; kmp[0]=-1;\n while (i<n) {\n while (j!=-1 && s[i]!=s[j]) j=kmp[j];\n j++;i++;\n kmp[i]=j;\n }\n vector<int> matches;\n for (int i=0;i<s_.size();i++) {\n if (kmp[p.size()+i+2]==p.size())\n matches.push_back(i-p.size()+1);\n }\n return matches;\n}\nclass Solution {\npublic:\nvector<int> beautifulIndices(string s, string a, string b, int k) {\n IOS\n vector<int> amat=get_matches(s,a), bmat=get_matches(s,b);\n vector<int> ans;\n for (auto &i:amat) { //O(nlogn)\n auto it=lower_bound(bmat.begin(),bmat.end(),i-k);\n if (it!=bmat.end()) {\n int j=*it;\n if (j<=i+k) ans.push_back(i);\n }\n }\n return ans;\n}\n};",
"memory": "93313"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "vector<int> get_matches(string &s_, string &p) { //O(s.length+p.length)\n string s=p+\"#\"+s_;\n int n=s.length();\n int kmp[n+1];\n int i=0,j=-1; kmp[0]=-1;\n while (i<n) {\n while (j!=-1 && s[i]!=s[j]) j=kmp[j];\n j++;i++;\n kmp[i]=j;\n }\n vector<int> matches;\n for (int i=0;i<s_.size();i++) {\n if (kmp[p.size()+i+2]==p.size())\n matches.push_back(i-p.size()+1);\n }\n return matches;\n}\nclass Solution {\npublic:\nvector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> amat=get_matches(s,a), bmat=get_matches(s,b);\n vector<int> ans;\n for (auto &i:amat) { //O(nlogn)\n auto it=lower_bound(bmat.begin(),bmat.end(),i-k);\n if (it!=bmat.end()) {\n int j=*it;\n if (abs(j-i)<=k) ans.push_back(i);\n }\n }\n return ans;\n}\n};",
"memory": "93313"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "using ll = long long;\nclass Solution {\npublic:\n\n static const int MOD = 1e9 + 7;\n static const int base = 23;\n static const int mn = 1000000 + 10;\n ll hash[mn];\n ll power[mn];\n\n ll getHash(int u, int v) {\n\treturn (hash[v] - (hash[u - 1] * power[v - u + 1]) % MOD + MOD) % MOD;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.size();\n\n\t\n\tpower[0] = 1;\n\tfor(int i = 1; i <= n; i++) {\n\t\tpower[i] = (power[i - 1] * base) % MOD;\n\t\thash[i] = (hash[i - 1] * base + s[i - 1] - 'a' + 1) % MOD;\n\t}\n\n\n int lengthA = a.size();\n int lengthB = b.size();\t\n\n\tll hashA = 0;\t\n\tfor(int i = 0; i < lengthA; i++) hashA = (hashA * base + a[i] - 'a' + 1) % MOD;\n\n\n\tll hashB = 0;\t\n\tfor(int i = 0; i < lengthB; i++) hashB = (hashB * base + b[i] - 'a' + 1) % MOD;\n\t\n\tvector<int> sameAIdxs;\n\tvector<int> sameBIdxs;\n\n\tfor (int i = 0; i < n; i++) {\n\t\tif(getHash(i + 1, i + lengthA) == hashA) sameAIdxs.push_back(i);\n\t}\n vector<int> finalResult;\n\n if (sameAIdxs.size() == 0) return finalResult;\n\n\tfor (int i = 0; i < n; i++) {\n\t\tif(getHash(i + 1, i + lengthB) == hashB) sameBIdxs.push_back(i);\n\t}\n\n int bSize = sameBIdxs.size();\n\n if(bSize == 0) return finalResult;\n\n\tfor(auto AIdx: sameAIdxs) {\n\t\tauto b1 = upper_bound(sameBIdxs.begin(), sameBIdxs.end(), AIdx);\n int idx = b1 - sameBIdxs.begin();\n if(idx == bSize) idx = bSize - 1;\n\t\tif (abs(AIdx - sameBIdxs[max(0, idx)]) <= k || abs(AIdx - sameBIdxs[max(0, idx - 1)]) <= k) finalResult.push_back(AIdx);\n\t}\n\treturn finalResult;\n }\n};",
"memory": "94816"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n vector<long long> p_pow;\n long long hash(string const& s){\n const int m = 1e9 + 9;\n long long hash_value = 0;\n for (int i = 0; i < (int)s.size(); i++) {\n hash_value = (hash_value + (s[i] - 'a' + 1) * p_pow[i]) % m;\n }\n return hash_value;\n }\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n\n const int m = 1e9+9;\n p_pow.resize(n);\n p_pow[0] = 1;\n for(int i = 1; i < n; i++){\n p_pow[i] = (p_pow[i-1] * 31) % m;\n }\n\n long long pref[n+1];\n pref[0] = 0;\n for(int i = 1; i <= n; i++){\n pref[i] = (pref[i-1] + (s[i-1] - 'a' + 1) * p_pow[i-1]) % m;\n }\n \n int len_a = a.length();\n int len_b = b.length();\n\n if(len_a > n || len_b > n) return {};\n\n long long val_b = hash(b);\n long long val_a = hash(a);\n\n vector<int> cnt;\n for(int i = len_b; i <= n; i++){\n if((pref[i] - pref[i-len_b] + m) % m == (val_b * p_pow[i-len_b]) % m) cnt.push_back(i-len_b+1);\n }\n \n vector<int> ans;\n for(int i = 1; i <= n-len_a+1; i++){\n if((pref[i+len_a-1] - pref[i-1] + m) % m == (val_a * p_pow[i-1]) % m){\n auto it = lower_bound(cnt.begin(), cnt.end(), i-k) - cnt.begin();\n if(it == cnt.size()) continue;\n if(cnt[it] <= k+i) ans.push_back(i-1);\n }\n }\n\n\n return ans;\n }\n};",
"memory": "94816"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n std::vector<int> knuth_morris_pratt(std::string strText, std::string strPattern)\n {\n std::vector<int> vecResult;\n std::vector<int> vecLPS(strPattern.size());\n vecLPS[0] = 0;\n int iLen = 0;\n int iIdx = 1;\n\n // Calculating LPS.\n //\n while ((unsigned int)iIdx < strPattern.size())\n {\n if (strPattern[iIdx] == strPattern[iLen])\n {\n vecLPS[iIdx++] = ++iLen;\n }\n else if (iLen != 0)\n {\n iLen = vecLPS[iLen - 1];\n }\n else\n {\n vecLPS[iIdx++] = 0;\n }\n }\n\n int iIdxText = 0;\n int iIdxPattern = 0;\n\n while ((unsigned int)iIdxText < strText.size())\n {\n if (strPattern[iIdxPattern] == strText[iIdxText])\n {\n ++iIdxPattern;\n ++iIdxText;\n }\n if (iIdxPattern == strPattern.size())\n {\n vecResult.push_back(iIdxText - iIdxPattern);\n iIdxPattern = vecLPS[iIdxPattern - 1];\n }\n else if ((unsigned int) iIdxText < strText.size() && strPattern[iIdxPattern] != strText[iIdxText])\n {\n if (iIdxPattern != 0)\n {\n iIdxPattern = vecLPS[iIdxPattern - 1];\n }\n else\n {\n ++iIdxText;\n }\n }\n }\n \n return vecResult;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> v1=knuth_morris_pratt(s,a);\n vector<int> v2=knuth_morris_pratt(s,b);\n vector<int> ans;\n for(int i=0;i<v1.size();++i)\n {\n auto it = lower_bound(v2.begin(),v2.end(),v1[i]+k+1);\n bool flag=false;\n if(it!=v2.begin())\n {\n --it;\n if(abs(*it-v1[i])<=k)\n flag=true;\n }\n it=lower_bound(v2.begin(),v2.end(),v1[i]-k);\n if(it!=v2.end())\n {\n if(abs(*it-v1[i])<=k)\n flag=true;\n }\n if(flag) ans.push_back(v1[i]);\n }\n return ans;\n }\n};",
"memory": "96318"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\n vector<int> lpsi(string &word) {\n int n = word.length();\n vector<int> lps(n);\n lps[0] = 0;\n int i = 1, len = 0;\n while (i < n) {\n if (word[len] == word[i]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n return lps;\n }\n\n vector<int> search(string pat, string txt) {\n vector<int> lps = lpsi(pat);\n int m = pat.length();\n int n = txt.length();\n vector<int> ans;\n int i = 0, j = 0;\n while (i < n) {\n if (pat[j] == txt[i]) {\n i++;\n j++;\n } else {\n if (j != 0) j = lps[j - 1];\n else i++;\n }\n if (j == m) {\n ans.push_back(i - j);\n j = lps[j - 1];\n }\n }\n return ans;\n }\n\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> v1 = search(a, s);\n vector<int> v2 = search(b, s);\n vector<int> ans;\n \n // Use two-pointer technique to avoid TLE\n int i = 0, j = 0;\n while (i < v1.size() && j < v2.size()) {\n if (abs(v1[i] - v2[j]) <= k) {\n ans.push_back(v1[i]);\n i++;\n } else if (v1[i] < v2[j]) {\n i++;\n } else {\n j++;\n }\n }\n \n return ans;\n }\n};\n",
"memory": "97821"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> make_lps(string &pat){\n int n = pat.size();\n vector<int> lps(n,0);\n \n for(int i=1;i<n;i++){\n int j = lps[i-1];\n \n while(j > 0 && pat[i] != pat[j]){\n j = lps[j-1];\n }\n \n if(pat[i] == pat[j]) j++;\n \n lps[i] = j;\n }\n \n return lps;\n }\n vector <int> search(string pat, string txt)\n {\n //code here\n vector<int> lps = make_lps(pat);\n vector<int> ans;\n int i = 0,j= 0;\n while(j < txt.size()){\n if(pat[i] == txt[j]){\n i++;\n j++;\n if(i == pat.size()){\n ans.push_back(j-i);\n i = lps[i-1];\n }\n }\n else{\n if(i > 0) i =lps[i-1];\n else j++;\n }\n }\n \n return ans;\n }\n \n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> f=search(a,s);\n vector<int> sec=search(b,s);\n vector<int> ans;\n int n=s.length();\n for(auto it:f){\n // int tar=it;\n // int ind=lower_bound(sec.begin(),sec.end(),tar)-sec.begin();\n // if(sec.size()>0){\n // if(ind==sec.size()){\n // if(abs(it-sec[ind-1])<=k)ans.push_back(it);\n // }\n // else{\n // if(abs(it-sec[ind])<=k){ans.push_back(it);continue;}\n // if(ind>0&&abs(it-sec[ind-1])<=k)ans.push_back(it);\n // }\n // continue;\n // }\n int x=max(0,it-k);\n int ind1=lower_bound(sec.begin(),sec.end(),x)-sec.begin();\n x=min(n-1,it+k);\n //int ind2=lower_bound(sec.begin(),sec.end(),x)-sec.begin();\n if(ind1<sec.size()&&abs(it-sec[ind1])<=k){\n ans.push_back(it);continue;\n }\n // if(ind2<sec.size()&&abs(it-sec[ind2])<=k){\n // ans.push_back(it);continue;\n // }\n }\n return ans;\n }\n};",
"memory": "97821"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void f(vector<int>&lps , string v){\n int prefix =0 ;\n int suffix =1;\n \n while(suffix < v.length()){\n if(v[prefix] == v[suffix]){\n lps[suffix] = prefix+1;\n prefix++;\n suffix++;\n }else{\n if(prefix == 0){\n lps[suffix] =0;\n suffix++;\n }\n else{\n prefix= lps[prefix-1];\n }\n }\n }\n }\n vector <int> search(string pat, string txt)\n {\n //code here\n int n =pat.size();\n vector<int>lps(n);\n f(lps , pat);\n \n int first =0 ;\n int second =0; \n \n vector<int>ans;\n \n while(first < txt.length()){\n if(txt[first] == pat[second]){\n first++;\n second++;\n \n if(second == pat.length()){\n ans.push_back( first-second );\n second = lps[second -1];\n \n }\n \n }else{\n if(second == 0){\n first++;\n }\n else{\n second = lps[second-1];\n }\n }\n }\n \n return ans;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> first = search( a,s);\n vector<int> second = search( b,s);\n\n \n\n int i = 0, j = 0;\n vector<int> result;\n\n while (i < first.size() && j < second.size()) {\n if (abs(first[i] - second[j]) <= k) {\n result.push_back(first[i]); \n i++;\n \n } else if (first[i] < second[j]) {\n i++;\n } else {\n j++;\n }\n }\n return result;\n }\n};",
"memory": "99323"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void computeLPS(string pattern, vector<int>& lps) {\n int M = pattern.length();\n int len = 0; // Length of the previous longest prefix & suffix\n\n lps[0] = 0; // Because there is no proper suffix and prefix of pattern[0..0]\n\n int i = 1;\n while (i < M) {\n if (pattern[i] == pattern[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n }\n\n vector <int> KMP_codestorywithMIK(string pat, string txt) {\n int N = txt.length();\n int M = pat.length();\n vector<int> result;\n\n // Create an LPS array to store the longest proper prefix which is also a suffix\n //lps[i] = the longest proper prefix of pat[0..i] which is also a suffix of pat[0..i]. \n vector<int> lps(M, 0);\n computeLPS(pat, lps);\n\n int i = 0; // Index for text\n int j = 0; // Index for pattern\n\n while (i < N) {\n if (pat[j] == txt[i]) {\n i++;\n j++;\n }\n\n if (j == M) {\n result.push_back(i-j); //Pattern found at index i-j+1 (If you have to return 1 Based indexing, that's why added + 1)\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i++;\n }\n }\n }\n\n return result;\n }\n \n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n \n vector<int> i_indices = KMP_codestorywithMIK(a, s);\n vector<int> j_indices = KMP_codestorywithMIK(b, s);\n /*\n Just simply find the indices where a is present in s\n Just simply find the indices where b is present in s\n */\n \n vector<int> result;\n \n for(int &i : i_indices) {\n /*\n For every index i where 'a' was found in 's'\n I will see if we find any index j in the range i-k and i+k\n */\n \n int left_limit = max(0, i - k); //To avoid out of bound -> I used max(0, i-k)\n int right_limit = min(n-1, i + k); //To avoid out of bound -> I used min(n-1, i+k)\n \n auto it = lower_bound(begin(j_indices), end(j_indices), left_limit);\n \n if(it != j_indices.end() && *it <= right_limit) {\n result.push_back(i);\n }\n }\n \n return result;\n }\n};\n",
"memory": "100826"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void computeLPS(string pattern, vector<int>& lps) {\n int M = pattern.length();\n int len = 0; \n\n lps[0] = 0;\n\n int i = 1;\n while (i < M) {\n if (pattern[i] == pattern[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n }\n\n vector <int> KMP_codestorywithMIK(string pat, string txt) {\n int N = txt.length();\n int M = pat.length();\n vector<int> result;\n\n vector<int> lps(M, 0);\n computeLPS(pat, lps);\n\n int i = 0;\n int j = 0; \n\n while (i < N) {\n if (pat[j] == txt[i]) {\n i++;\n j++;\n }\n\n if (j == M) {\n result.push_back(i-j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i++;\n }\n }\n }\n\n return result;\n }\n \n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n \n vector<int> i_indices = KMP_codestorywithMIK(a, s);\n vector<int> j_indices = KMP_codestorywithMIK(b, s);\n \n vector<int> result;\n \n for(int &i : i_indices) {\n \n int left_limit = max(0, i - k); \n int right_limit = min(n-1, i + k); \n \n auto it = lower_bound(begin(j_indices), end(j_indices), left_limit);\n \n if(it != j_indices.end() && *it <= right_limit) {\n result.push_back(i);\n }\n }\n return result;\n }\n};",
"memory": "100826"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<int> returnPos(string s, string a) {\n string new_str = a+\"#\"+s;\n vector<int> res;\n int lps[new_str.length()];\n lps[0] = 0;\n for(int i=1;i<new_str.length();i++) {\n int k = lps[i-1];\n\n while(k > 0 && new_str[k] != new_str[i]) {\n k = lps[k-1];\n }\n\n lps[i] = k + (new_str[k] == new_str[i] ? 1 : 0);\n }\n\n for(int i=0;i<new_str.length();i++){\n\n if(a == \"tx\") {\n cout<<lps[i]<<\" \";\n }\n\n if(lps[i] == a.length()) {\n res.push_back(i-2*a.length());\n }\n }\n return res;\n\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> pos1 = returnPos(s, a);\n vector<int> pos2 = returnPos(s, b);\n\n vector<int> finalRes;\n\n for(int i=0;i<pos1.size();i++) {\n int lowerBound = lower_bound(pos2.begin(), pos2.end(), pos1[i])-pos2.begin();\n if(lowerBound != pos2.size() && abs(pos2[lowerBound]-pos1[i]) <= k) {\n finalRes.push_back(pos1[i]);\n continue;\n }\n if(lowerBound!=0 && abs(pos2[lowerBound-1]-pos1[i]) <= k) {\n finalRes.push_back(pos1[i]);\n }\n }\n\n return finalRes;\n\n }\n};",
"memory": "102328"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<int> returnPos(string s, string a) {\n string new_str = a+\"#\"+s;\n vector<int> res;\n int lps[new_str.length()];\n lps[0] = 0;\n for(int i=1;i<new_str.length();i++) {\n int k = lps[i-1];\n\n while(k > 0 && new_str[k] != new_str[i]) {\n k = lps[k-1];\n }\n\n lps[i] = k + (new_str[k] == new_str[i] ? 1 : 0);\n }\n\n for(int i=0;i<new_str.length();i++){\n if(lps[i] == a.length()) {\n res.push_back(i-2*a.length());\n }\n }\n return res;\n\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> pos1 = returnPos(s, a);\n vector<int> pos2 = returnPos(s, b);\n sort(pos2.begin(), pos2.end());\n vector<int> finalRes;\n\n for(int i=0;i<pos1.size();i++) {\n int left = 0;\n int right = pos2.size()-1;\n while(left <= right) {\n int mid = left+(right-left)/2;\n if(abs(pos1[i]-pos2[mid]) <= k){\n finalRes.push_back(pos1[i]);\n break;\n } else if(pos1[i]-pos2[mid] > k) {\n left = mid+1;\n } else {\n right = mid-1;\n }\n }\n }\n\n return finalRes;\n\n }\n};",
"memory": "102328"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n const int mod = 1e9 + 9;\n const int B = 31;\n static const int N = 5e5 + 5;\n int p[N];\n\n void init() {\n p[0] = 1;\n for (int i = 1; i < N; i++) {\n p[i] = 1LL * p[i - 1] * B % mod;\n }\n }\n\n vector<int> hash(string s) {\n int n = s.size();\n vector<int> h(n);\n for (int i = 0; i < n; i++) {\n if (i) {\n h[i] = h[i - 1];\n }\n (h[i] += (long long)(s[i] - 'a' + 1) * p[i] % mod) %= mod;\n }\n return h;\n }\n\n int get(int l, int r, vector<int> &h) {\n int H = (h[r] - (!l ? 0 : h[l - 1]) + mod) % mod;\n return 1LL * H * p[N - r - 1] % mod;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.size(), m = a.size(), x = b.size();\n init();\n\n int ha = 1LL * hash(a).back() * p[N - m] % mod;\n int hb = 1LL * hash(b).back() * p[N - x] % mod;\n vector<int> hs = hash(s);\n\n vector<int> good(n);\n for (int i = 0; i <= n - x; i++) {\n if (get(i, i + x - 1, hs) == hb) {\n good[i] = 1;\n }\n }\n for (int i = 1; i < n; i++) {\n good[i] += good[i - 1];\n }\n\n vector<int> res;\n for (int i = 0; i <= n - m; i++) {\n if (get(i, i + m - 1, hs) != ha) continue;\n\n if (good[min(n - 1, i + k)] - (i - k - 1 < 0 ? 0 : good[i - k - 1]) > 0) {\n res.push_back(i);\n }\n }\n\n return res;\n }\n};",
"memory": "103831"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class StringSearch {\n vector<int> b;\n string t, p;\n public:\n StringSearch(string text, string pattern) {\n t = text;\n p = pattern;\n b.assign(p.length() + 1, 0);\n }\n\n void kmpPreProcess() {\n int i =0, j =-1;\n b[0] = -1;\n while(i < p.length()) {\n while(j >=0 && p[i] != p[j]) j = b[j];\n i++, j++;\n b[i] = j;\n } \n }\n\n vector<int> find() {\n int i = 0, j =-0;\n kmpPreProcess();\n int n = t.length(), m = p.length();\n vector<int> ind;\n while(i < n) {\n while(j >=0 && t[i] != p[j]) j = b[j];\n i++, j++;\n if (j == m) { \n ind.push_back(i -j);\n j = b[j]; \n }\n }\n return ind; \n } \n};\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n StringSearch a1(s, a), a2(s,b);\n vector<int> s1 = a1.find(), s2 = a2.find();\n vector<int> ans;\n for(int i = 0; i < s1.size();++i) {\n auto it1 = lower_bound(s2.begin(), s2.end(), s1[i]);\n auto it2 = upper_bound(s2.begin(), s2.end(), s1[i] + k);\n auto it3 = lower_bound(s2.begin(), s2.end(), s1[i] - k);\n auto it4 = upper_bound(s2.begin(), s2.end(), s1[i]);\n if(it1 != s2.end() && it2 != s2.begin() ) {\n if(it1 < it2) {\n ans.push_back(s1[i]);\n continue;\n }\n }\n\n if(it3 != s2.end() && it4 != s2.begin() ) {\n if(it3 < it4) {\n ans.push_back(s1[i]);\n \n }\n }\n\n }\n\n return ans;\n\n }\n};",
"memory": "105333"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n if(s.size() < a.size() || s.size() < b.size()) return {};\n int n = s.size();\n int mod = 1e9 + 9;\n long long p = 31;\n vector<long long> pow(n, 1);\n for(int i=1 ; i<n ; i++){\n pow[i] = (pow[i-1]*p) % mod;\n }\n vector<int> ans;\n \n vector<int> hash(n, 1);\n hash[0] = int(s[0] - 'a' + 1);\n for(int i=1 ; i<n ; i++){\n hash[i] = (hash[i-1] + pow[i]*(int(s[i] - 'a' + 1))) % mod;\n }\n int n1 = a.size();\n int hasha = 0;\n for(int i=0 ; i<n1 ; i++){\n hasha = (hasha + pow[i]*(int(a[i] - 'a' + 1))) % mod;\n }\n int hashb = 0;\n int n2 = b.size();\n for(int i=0 ; i<n2 ; i++){\n hashb = (hashb + pow[i]*(int(b[i] - 'a' + 1))) % mod;\n }\n vector<int> s1;\n vector<int> s2;\n for(int i=n1-1 ; i<n ; i++){\n long long curhash = hash[i];\n if(i - n1 >=0 ) curhash = (curhash - hash[i-n1] + mod) % mod;\n if(hasha == curhash){\n s1.push_back(i+1-n1);\n cout<<i+1-n1<<endl;\n } \n hasha = (hasha*p) % mod;\n }\n for(int i=n2-1 ; i<n ; i++){\n long long curhash = hash[i];\n if(i - n2 >=0 ) curhash = (curhash - hash[i-n2] + mod) % mod;\n if(hashb == curhash){\n s2.push_back(i+1-n2);\n cout<<i+1-n2<<endl;\n } \n hashb = (hashb*p) % mod;\n }\n int ind = 0;\n for(int it : s1){\n for(int i=ind ; i<s2.size() ; i++){\n if(abs(it - s2[i]) <= k){\n ans.push_back(it);\n break;\n } \n else{\n if(it > s2[i]) ind = i+1;\n }\n }\n }\n return ans;\n }\n};",
"memory": "106836"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>lpf(string word){\n vector<int>ans(word.size());\n int length=0;\n int i=1;\n while(i<word.size()){\n if(word[i]==word[length]){\n length++;\n ans[i]=length;\n i++;\n }\n else{\n if(length!=0){\n length=ans[length-1];\n }\n else{\n ans[i]=0;\n i++;\n }\n }\n\n }\n return ans;\n\n }\n vector<int>kmp(string word,string text){\n vector<int>ans;\n vector<int>temp(word.size());\n temp=lpf(word);\n int i=0;\n int j=0;\n while(i<text.size()){\n if(text[i]==word[j]){\n i++;\n j++;\n if(j==word.size()){\n ans.push_back(i-j);\n j=temp[j-1];\n }\n\n }\n else if(text[i]!=word[j]){\n if(j!=0) j=temp[j-1];\n else i++;\n\n }\n \n }\n return ans;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int>ans;\n vector<int>ind_a;\n vector<int>ind_b;\n ind_a=kmp(a,s);\n ind_b=kmp(b,s);\n for(int i=0;i<ind_a.size();i++){\n int mini=ind_a[i]-k;\n int maxi=ind_a[i]+k;\n auto it=lower_bound(begin(ind_b),end(ind_b),mini);\n if(it!=ind_b.end() && *it<=maxi){\n ans.push_back(ind_a[i]);\n }\n }\n return ans;\n \n }\n};",
"memory": "108338"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findlps(string pat) {\n int n = pat.size();\n vector<int> lps(n, 0);\n int len = 0;\n int i = 1;\n \n while (i < n) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n \n return lps;\n }\n\n vector<int> matchind(string txt, string pat, vector<int> lps) {\n int m = txt.size();\n int n = pat.size();\n int j = 0;\n int i = 0;\n vector<int> ans;\n \n while (i<m) {\n if (txt[i] == pat[j]) {\n i++;\n j++;\n }\n if (j == n) {\n ans.push_back(i - j); \n j = lps[j - 1];\n } else if (i < m && txt[i] != pat[j]) {\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i++;\n }\n }\n }\n \n if (ans.size() == 0) return {};\n return ans;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> lpsa = findlps(a);\n vector<int> lpsb = findlps(b);\n\n vector<int> matcha = matchind(s, a, lpsa);\n vector<int> matchb = matchind(s, b, lpsb);\n vector<int> ans;\n\n for (int i = 0; i < matcha.size(); i++) {\n auto it = lower_bound(matchb.begin(), matchb.end(), matcha[i] - k);\n auto it1 = lower_bound(matchb.begin(), matchb.end(), matcha[i] );\n \n if (it != matchb.end() && abs(*it - matcha[i])<=k) {\n ans.push_back(matcha[i]);\n continue;\n }\n if (it1 != matchb.end() && abs(*it1 - matcha[i])<=k) {\n ans.push_back(matcha[i]);\n }\n }\n if (ans.empty()) return {};\n return ans;\n \n }\n};",
"memory": "109841"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> findlps(string pat) {\n int n = pat.size();\n vector<int> lps(n, 0);\n int len = 0;\n int i = 1;\n \n while (i < n) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n \n return lps;\n }\n\n vector<int> matchind(string txt, string pat, vector<int> lps) {\n int m = txt.size();\n int n = pat.size();\n int j = 0;\n int i = 0;\n vector<int> ans;\n \n while ((m - i) >= (n - j)) {\n if (txt[i] == pat[j]) {\n i++;\n j++;\n }\n if (j == n) {\n ans.push_back(i - j); \n j = lps[j - 1];\n } else if (i < m && txt[i] != pat[j]) {\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i++;\n }\n }\n }\n \n if (ans.size() == 0) return {};\n return ans;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> lpsa = findlps(a);\n vector<int> lpsb = findlps(b);\n\n vector<int> matcha = matchind(s, a, lpsa);\n vector<int> matchb = matchind(s, b, lpsb);\n vector<int> ans;\n\n for (int i = 0; i < matcha.size(); i++) {\n auto it = lower_bound(matchb.begin(), matchb.end(), matcha[i] - k);\n auto it1 = lower_bound(matchb.begin(), matchb.end(), matcha[i] );\n \n if (it != matchb.end() && abs(*it - matcha[i])<=k) {\n ans.push_back(matcha[i]);\n continue;\n }\n if (it1 != matchb.end() && abs(*it1 - matcha[i])<=k) {\n ans.push_back(matcha[i]);\n }\n }\n if (ans.empty()) return {};\n return ans;\n \n }\n};",
"memory": "111343"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "#define MOD 1000000007\n#define PRIME 100003\n\nvector<int> power;\nint add(int x, int y) {\n int temp = x + y;\n return temp >= MOD ? temp - MOD : temp;\n}\n\nint sub(int x, int y) {\n int temp = x - y;\n return temp < 0 ? temp + MOD : temp ;\n}\n\nint mult(int x, int y) {\n long long int temp = x * 1LL * y;\n return temp >= MOD ? temp % MOD : temp;\n}\n\n\nclass HASHING {\n private:\n string s;\n int size;\n vector<int> hash;\n public:\n HASHING(string s_) : s(s_) {\n size = s_.length();\n hash.resize(size, 0); \n hash[0] = s_[0];\n for(int i = 1; i < size; i++) hash[i] = add(hash[i - 1], mult(power[i], s_[i]));\n }\n\n int get_complete_hash() {\n return hash[size - 1];\n }\n int get_hash(int left, int right) {\n return left == 0 ? hash[right] : sub(hash[right], hash[left - 1]);\n }\n\n};\n\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> answer, rhs;\n int n = s.length(), a_n = a.length(), b_n = b.length();\n power.resize(n, 0);\n rhs.resize(n, 0);\n power[0] = 1;\n for(int i = 1; i < n; i++) power[i] = mult(power[i - 1], PRIME);\n HASHING s1(s), s2(a), s3(b); \n for(int i = 0; i < n; i++) {\n rhs[i] = (i == 0) ? 0 : rhs[i - 1];\n if(i + b_n <= n && s1.get_hash(i, i + b_n - 1) == mult(power[i], s3.get_complete_hash())) {\n rhs[i]++;\n }\n } \n \n for(int i = 0; i < n; i++) {\n if(i + a_n <= n && s1.get_hash(i, i + a_n - 1) == mult(power[i], s2.get_complete_hash())) {\n int left_lim = i - k; \n int right_lim = i + k > n - 1 ? n - 1 : i + k;\n int change = rhs[right_lim] - (left_lim <= 0 ? 0 : rhs[left_lim - 1]);\n if(change >= 1) answer.push_back(i);\n }\n }\n return answer;\n }\n};",
"memory": "111343"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<int> longestPrefixSuffix(string &str) {\n vector<int> A(str.size(), 0);\n int j = 0, i = 1;\n while(i < str.size()) {\n if(str[i] == str[j]) {\n A[i] = j+1;\n j++;\n i++;\n } else{\n if(j==0)\n i++;\n else\n j = A[j-1];\n }\n }\n return A;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int kk) {\n\n string t=b+\"#\"+s;\n vector<int>lps = longestPrefixSuffix(t);\n vector<int>v;\n int n=lps.size();\n for(int i=0;i<n;i++){\n if(i>=b.size() && lps[i]==b.size()){\n v.push_back(i-b.size()-b.size());\n }\n }\n\n sort(v.begin(),v.end());\n vector<int>ans;\n t = a+\"#\"+s;\n lps = longestPrefixSuffix(t);\n n=lps.size();\n\n for(int i=0;i<n;i++){\n if(i>=a.size() && lps[i]==a.size()){\n int t=i-a.size()-a.size();\n int l = lower_bound(v.begin(),v.end(),t-kk)-v.begin();\n int r = upper_bound(v.begin(),v.end(),t+kk)-v.begin();\n int tt=r-l;\n if(tt>0){\n ans.push_back(t);\n }\n }\n }\n return ans;\n }\n};\n",
"memory": "112846"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> kmp(string pat, string_view text) {\n int patsize = pat.size();\n pat.reserve(pat.size() + 1 + text.size());\n pat += '#';\n pat += text;\n\n vector<int> dp(pat.size() + 1);\n dp[0] = -1;\n vector<int> res;\n for (int i = 1; i < pat.size(); ++i) {\n int p = dp[i];\n while (p >= 0 and pat[i] != pat[p])\n p = dp[p];\n dp[i + 1] = p + 1;\n if (dp[i + 1] == patsize) {\n res.push_back(i - 2 * patsize);\n }\n }\n return res;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n auto ind_a = kmp(std::move(a), s);\n auto ind_b = kmp(std::move(b), s);\n\n vector<int> ans;\n auto itb = ind_b.begin();\n if (ind_a.empty() or ind_b.empty()) return ans;\n for (int ia : ind_a) {\n while (itb < ind_b.end() and *itb < ia - k) ++itb;\n if (itb == ind_b.end()) return ans;\n if (*itb <= ia + k) ans.push_back(ia);\n }\n return ans;\n }\n};",
"memory": "114348"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "// 16:02\n\nconst int base = 37;\n\nclass Solution {\npublic:\n int md = 1000000007;\n vector<int> getMatchingIndexes(const string& s, const string& a) {\n long long oriHs = 0;\n long long power = 1;\n for (int i=0; i<a.size(); i++) {\n oriHs = (oriHs * base + (a[i]-'a')) % md;\n power = (power * base) % md;\n }\n\n vector<int> indexes;\n vector<long long> hs(s.size());\n for (int i=0; i<s.size(); i++) {\n hs[i] = (s[i]-'a');\n if (i > 0) {\n hs[i] = (hs[i] + hs[i-1] * base) % md;\n }\n if (i>=a.size() - 1) {\n long long curHs = hs[i];\n if (i>=a.size()) {\n curHs = (curHs - ((hs[i-a.size()] * power) % md) + md) % md;\n }\n if (curHs == oriHs) {\n indexes.push_back(i-a.size()+1);\n }\n }\n }\n return indexes;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n if (a.size() > s.size() || b.size() > s.size()) {\n return {};\n }\n auto aIs = getMatchingIndexes(s, a);\n auto bIs = getMatchingIndexes(s, b);\n int p0=0;\n int p1=0;\n vector<int> indexes;\n for (const int& ia : aIs) {\n while (p0 < bIs.size() && ia - bIs[p0] > k) p0++;\n while (p1+1 < bIs.size() && bIs[p1+1]-ia <= k) p1++;\n if (p0 <= p1 && p1 < bIs.size() && ia - bIs[p0] <= k && bIs[p1]-ia <= k) {\n indexes.push_back(ia);\n }\n }\n return indexes;\n }\n};",
"memory": "115851"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n vector<int> computeMatches(string& p, string& s){\n string ps = p + \"#\" + s;\n int psLen = ps.length();\n int pLen = p.length();\n\n vector<int> matches;\n vector<int> z(psLen);\n int l = 0;\n int r = 0;\n for(int i = 1; i < psLen; ++i){\n if(i <= r){\n z[i] = min(z[i - l], r - i + 1);\n }\n while(i + z[i] < psLen && ps[z[i]] == ps[i + z[i]]){\n z[i] += 1;\n }\n if(r <= i + z[i] - 1){\n l = i;\n r = i + z[i] - 1;\n }\n if(z[i] == pLen){\n matches.push_back(i - (pLen + 1));\n }\n }\n\n return matches;\n }\n \npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> aMatches = computeMatches(a, s);\n vector<int> bMatches = computeMatches(b, s);\n\n vector<int> res;\n int ptr = 0;\n for(int i: aMatches){\n while(ptr < (int)bMatches.size() && bMatches[ptr] < i - k){\n ptr += 1;\n }\n if(ptr < (int)bMatches.size() && bMatches[ptr] <= i + k){\n res.push_back(i);\n }\n }\n \n return res;\n }\n};",
"memory": "117353"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void printArr(vector<int> &arr){\n for(int i = 0; i<arr.size(); i++) cout<<arr[i]<<\" \";\n cout<<'\\n';\n }\n void findLps(string &s, vector<int> &lps) {\n int n = s.size();\n lps[0] = 0; // The first character's LPS is always 0\n int i = 0; // Length of the previous longest prefix suffix\n \n for (int j = 1; j < n; j++) {\n while (i != 0 && s[i] != s[j]) {\n i = lps[i - 1]; // Jump to the previous possible LPS value\n }\n \n if (s[i] == s[j]) {\n i++; // If characters match, increase the LPS length\n }\n \n lps[j] = i; // Store the LPS length for position j\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string s1 = a + \"$\" + s;\n string s2 = b + \"$\" + s;\n // cout<<s1<<'\\n';\n int n1 = s1.size();\n int n2 = s2.size();\n vector<int> lps1(n1, 0);\n vector<int> lps2(n2, 0);\n findLps(s1, lps1);\n findLps(s2, lps2);\n // printArr(lps1);\n // printArr(lps2);\n int m1 = a.size();\n int m2 = b.size();\n int m = s.size();\n vector<int> arr1, arr2;\n arr2.push_back(-1e8);\n for(int i = 0; i<m; i++){\n if(lps1[m1+1+i] == m1) arr1.push_back(i-m1+1);\n }\n for(int i = 0; i<m; i++){\n if(lps2[m2+1+i] == m2) arr2.push_back(i-m2+1);\n }\n arr2.push_back(1e8);\n // printArr(arr1);\n // printArr(arr2);\n int size2 = arr2.size();\n if(size2 == 0) return {};\n vector<int> arr;\n for(auto it: arr1){\n int ind = lower_bound(arr2.begin(), arr2.end(), it) - arr2.begin();\n bool b1 = (arr2[ind] - it) <= k;\n bool b2 = (it - arr2[ind-1]) <= k;\n if(b1 || b2) arr.push_back(it);\n }\n return arr;\n }\n};",
"memory": "118856"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int lenS = s.size();\n int lenA = a.size();\n int lenB = b.size();\n\n vector<int> lpsA(lenS + lenA + 1, 0);\n vector<int> lpsB(lenS + lenB + 1, 0);\n\n string kmpA = a + \"$\" + s;\n string kmpB = b + \"$\" + s;\n\n // Compute LPS array for the pattern \"a\"\n for (int i = 1; i < (lenS + lenA + 1); i++) {\n int prev_indx = lpsA[i - 1];\n while (prev_indx > 0 && kmpA[i] != kmpA[prev_indx]) {\n prev_indx = lpsA[prev_indx - 1];\n }\n lpsA[i] = prev_indx + (kmpA[i] == kmpA[prev_indx]);\n }\n\n // Compute LPS array for the pattern \"b\"\n for (int i = 1; i < (lenS + lenB + 1); i++) {\n int prev_indx = lpsB[i - 1];\n while (prev_indx > 0 && kmpB[i] != kmpB[prev_indx]) {\n prev_indx = lpsB[prev_indx - 1];\n }\n lpsB[i] = prev_indx + (kmpB[i] == kmpB[prev_indx]);\n }\n\n // Collect indices where pattern \"a\" occurs in \"s\"\n vector<int> indicesA, indicesB;\n for (int i = lenA + 1; i < (lenS + lenA + 1); i++) {\n if (lpsA[i] == lenA) {\n indicesA.push_back(i - 2 * lenA);\n }\n }\n\n // Collect indices where pattern \"b\" occurs in \"s\"\n for (int i = lenB + 1; i < (lenS + lenB + 1); i++) {\n if (lpsB[i] == lenB) {\n indicesB.push_back(i - 2 * lenB);\n }\n }\n\n vector<int> result;\n\n // Find the \"beautiful\" indices\n for (int i = 0; i < indicesA.size(); i++) {\n int idxA = indicesA[i];\n\n // Use binary search to find nearest indices of \"b\"\n auto smallerIt = lower_bound(indicesB.begin(), indicesB.end(), idxA);\n\n bool isBeautiful = false;\n\n if (smallerIt != indicesB.end() && abs(idxA - *smallerIt) <= k) {\n isBeautiful = true;\n }\n\n if (smallerIt != indicesB.begin() && abs(idxA - *(smallerIt - 1)) <= k) {\n isBeautiful = true;\n }\n\n if (isBeautiful) {\n result.push_back(idxA);\n }\n }\n\n return result;\n }\n};\n",
"memory": "120358"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void printArr(vector<int> &arr){\n for(int i = 0; i<arr.size(); i++) cout<<arr[i]<<\" \";\n cout<<'\\n';\n }\n void findLps(string &s, vector<int> &lps){\n int n = s.size();\n int i = 0;\n for(int j = 1; j<n; j++){\n // cout<<i<<\" \"<<j<x<'\\n';\n if(s[i] == s[j]) {\n lps[j] = lps[j-1] + 1;\n i++;\n }\n else {\n while(i!=0 && s[i] != s[j]){\n i = lps[i-1];\n }\n if(s[i] == s[j]) {\n lps[j] = i + 1;\n i++;\n }\n }\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string s1 = a + \"$\" + s;\n string s2 = b + \"$\" + s;\n // cout<<s1<<'\\n';\n int n1 = s1.size();\n int n2 = s2.size();\n vector<int> lps1(n1, 0);\n vector<int> lps2(n2, 0);\n findLps(s1, lps1);\n findLps(s2, lps2);\n // printArr(lps1);\n // printArr(lps2);\n int m1 = a.size();\n int m2 = b.size();\n int m = s.size();\n vector<int> arr1, arr2;\n arr2.push_back(-1e8);\n for(int i = 0; i<m; i++){\n if(lps1[m1+1+i] == m1) arr1.push_back(i-m1+1);\n }\n for(int i = 0; i<m; i++){\n if(lps2[m2+1+i] == m2) arr2.push_back(i-m2+1);\n }\n arr2.push_back(1e8);\n // printArr(arr1);\n // printArr(arr2);\n int size2 = arr2.size();\n if(size2 == 0) return {};\n vector<int> arr;\n for(auto it: arr1){\n int ind = lower_bound(arr2.begin(), arr2.end(), it) - arr2.begin();\n bool b1 = (arr2[ind] - it) <= k;\n bool b2 = (it - arr2[ind-1]) <= k;\n if(b1 || b2) arr.push_back(it);\n }\n return arr;\n }\n};",
"memory": "121861"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void printArr(vector<int> &arr){\n for(int i = 0; i<arr.size(); i++) cout<<arr[i]<<\" \";\n cout<<'\\n';\n }\n void findLps(string &s, vector<int> &lps) {\n int n = s.size();\n lps[0] = 0; // The first character's LPS is always 0\n int i = 0; // Length of the previous longest prefix suffix\n \n for (int j = 1; j < n; j++) {\n while (i > 0 && s[i] != s[j]) {\n i = lps[i - 1]; // Jump to the previous possible LPS value\n }\n \n if (s[i] == s[j]) {\n i++; // If characters match, increase the LPS length\n }\n \n lps[j] = i; // Store the LPS length for position j\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string s1 = a + \"$\" + s;\n string s2 = b + \"$\" + s;\n // cout<<s1<<'\\n';\n int n1 = s1.size();\n int n2 = s2.size();\n vector<int> lps1(n1, 0);\n vector<int> lps2(n2, 0);\n findLps(s1, lps1);\n findLps(s2, lps2);\n // printArr(lps1);\n // printArr(lps2);\n int m1 = a.size();\n int m2 = b.size();\n int m = s.size();\n vector<int> arr1, arr2;\n arr2.push_back(-1e8);\n for(int i = 0; i<m; i++){\n if(lps1[m1+1+i] == m1) arr1.push_back(i-m1+1);\n }\n for(int i = 0; i<m; i++){\n if(lps2[m2+1+i] == m2) arr2.push_back(i-m2+1);\n }\n arr2.push_back(1e8);\n // printArr(arr1);\n // printArr(arr2);\n int size2 = arr2.size();\n if(size2 == 0) return {};\n vector<int> arr;\n for(auto it: arr1){\n int ind = lower_bound(arr2.begin(), arr2.end(), it) - arr2.begin();\n bool b1 = (arr2[ind] - it) <= k;\n bool b2 = (it - arr2[ind-1]) <= k;\n if(b1 || b2) arr.push_back(it);\n }\n return arr;\n }\n};",
"memory": "121861"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>KMPAlgorithm(string &s) {\n \n int n=s.size();\n vector<int>lps(n,0); //longest prefix that matches suffix\n\n for(int i=1;i<n;i++) {\n int prevIndex = lps[i-1];\n\n while(prevIndex > 0 && s[i] != s[prevIndex]) {\n prevIndex = lps[prevIndex-1];\n }\n\n lps[i]=prevIndex+(s[i]==s[prevIndex]);\n }\n return lps;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string str1=a+\"#\"+s;\n string str2=b+\"#\"+s;\n \n\n vector<int>lps1=KMPAlgorithm(str1);\n vector<int>lps2=KMPAlgorithm(str2);\n\n vector<int>indices1;\n vector<int>indices2;\n\n for(int i=a.size()+1;i<str1.size();i++) {\n if(lps1[i]==a.size()) {\n indices1.push_back(i-(2*a.size()));\n }\n }\n\n for(int i=b.size()+1;i<str2.size();i++) {\n if(lps2[i]==b.size()) {\n indices2.push_back(i-(2*b.size()));\n }\n }\n\n vector<int>ans;\n if(indices1.size() == 0 || indices2.size()==0) return ans;\n for(int i=0;i<indices1.size();i++) {\n int index=indices1[i];\n int pos=lower_bound(indices2.begin(),indices2.end(),index)-indices2.begin();\n int f=0;\n if(pos!=indices2.size()) {\n if(abs(indices2[pos]-index) <= k)\n f=1;\n }\n if(pos!=0) {\n if(abs(indices2[pos-1]-index) <= k)\n f=1;\n }\n if(f==1)\n ans.push_back(index);\n }\n return ans;\n }\n};",
"memory": "123363"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> func(string &s, string &a) {\n vector<int>ans;\n int size = a.size();\n a.push_back('#');\n a.insert(a.end(), s.begin(), s.end());\n vector<int>lps(a.size(), 0);\n int temp = 0;\n for(int i = 1; i < lps.size(); ++i) {\n temp = lps[i-1];\n while(temp > 0 && (a[temp] != a[i])) {\n temp = lps[temp-1];\n }\n lps[i] = temp + ((a[temp] == a[i]) ? 1 : 0); \n if (lps[i] == size) {\n ans.push_back(i - (2 * size));\n }\n }\n return ans;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int>ans;\n vector<int> value1 = func(s, a);\n vector<int> value2 = func(s, b);\n int count = 0;\n for(int i = 0; i < value1.size(); ++i) {\n int it = upper_bound(value2.begin(), value2.end(), value1[i]) - value2.begin();\n if (it < value2.size() && (abs(value2[it] - value1[i]) <= k)) {\n ans.push_back(value1[i]);\n } else if ((it - 1) >= 0 && (abs(value2[it-1] - value1[i])) <= k) {\n ans.push_back(value1[i]);\n }\n }\n return ans;\n }\n};",
"memory": "123363"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "\n\ntypedef long long ll;\nconstexpr ll mod(ll a, ll n) { return (a % n + n) % n; }\nconstexpr int extEuclid(int a, int b, int& x, int& y) { // pass x and y by ref\n ll xx = y = 0;\n ll yy = x = 1;\n while (b) { // repeats until b == 0\n ll q = a / b;\n tie(a, b) = tuple(b, a % b);\n tie(x, xx) = tuple(xx, x - q * xx);\n tie(y, yy) = tuple(yy, y - q * yy);\n }\n return a; // returns gcd(a, b)\n}\nconstexpr int modInverse(int b, int m) { // returns b^(-1) (mod m)\n int x, y;\n int d = extEuclid(b, m, x, y); // to get b*x + m*y == d\n if (d != 1) return -1; // to indicate failure\n // b*x + m*y == 1, now apply (mod m) to get b*x == 1 (mod m)\n return mod(x, m);\n}\n\ntemplate <int maxN = static_cast<int>(1e6), ll p = 131, ll M = static_cast<int>(1e9) + 7>\nclass RollingHash {\n private:\n static array<int, maxN> P; // P[i] = p^i mod m\n static array<int, maxN> P_inv; // P_inv[i] = p^(-i) mod m\n vector<int> H; // H[i] is the hash of prefix length i\n const int n;\n string T;\n static void PrepareP() { // precompute P and P_inv\n if (P[0] != 0) return; // already prepared\n P[0] = 1;\n for (int i = 1; i < maxN; i++) P[i] = (P[i - 1] * p) % M;\n P_inv[maxN - 1] = modInverse(P[maxN - 1], M);\n for (int i = maxN - 2; i >= 0; i--) P_inv[i] = (P_inv[i + 1] * p) % M;\n }\n \n void computeRollingHash() { // precompute H\n H.assign(n, 0);\n for (int i = 0; i < n; i++) {\n if (i != 0) H[i] = H[i - 1];\n H[i] = (H[i] + ((ll)T[i] * P[i]) % M) % M;\n }\n }\n\n public:\n RollingHash(string& _s): n(_s.size()), T(_s) {\n PrepareP();\n computeRollingHash();\n }\n\n int getHash(int l, int r) { // get hash of substring [l, r]\n if (l == 0) return H[r];\n int ans = ((H[r] - H[l - 1]) % M + M) % M;\n ans = ((ll)ans * P_inv[l]) % M;\n return ans;\n }\n};\n\ntemplate <int maxN, ll p, ll M>\narray<int, maxN> RollingHash<maxN, p, M>::P{};\ntemplate <int maxN, ll p, ll M>\narray<int, maxN> RollingHash<maxN, p, M>::P_inv{};\n\n/**Returns a vector of indices of all occurrences of pattern in text.\n * P refers to the pattern, \n * T refers to the Text hash_params refer to a vector of (p, M) where p and M are used to compute the\n * hash \n * -> Use more hash parameters to reduce the probability of collisions\n * M should be a large prime, p should be a small prime > number of distinct characters\n */\ntemplate <int maxN = static_cast<int>(1e6), ll p = 131, ll M = static_cast<int>(1e9) + 7>\nvector<int> rabinKarp(string& P, string& T) {\n RollingHash<maxN, p, M> P_hash(P);\n RollingHash<maxN, p, M> T_hash(T);\n vector<int> matches;\n\n int n = T.size(), m = P.size();\n for (int i = 0; i <= n - m; i++) {\n if (P_hash.getHash(0, m - 1) == T_hash.getHash(i, i + m - 1))\n matches.push_back(i);\n }\n return matches;\n};\n\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> matcha = rabinKarp<(int) 5e5 + 5>(a, s);\n vector<int> matchb = rabinKarp<(int) 5e5 + 5>(b, s);\n\n sort(matcha.begin(), matcha.end());\n sort(matchb.begin(), matchb.end());\n\n vector<int> ans;\n int j = 0;\n for (int i=0; i<matcha.size(); i++){\n while (j < matchb.size() && matchb[j] <= matcha[i]) j++;\n int dist = 1e9;\n if (j < matchb.size()) dist = min(dist, abs(matchb[j] - matcha[i]));\n if (j > 0) dist = min(dist, abs(matchb[j-1] - matcha[i]));\n\n if (dist <= k) ans.push_back(matcha[i]);\n }\n\n return ans;\n }\n};",
"memory": "124866"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "vector<int> zfunc(string s) {\n int l, r;\n l = r = 0;\n vector<int> z(s.size());\n\tz[0] = 0;\n \n\tfor (int i = 1; i < s.size(); i++) {\n\t\tif (i <= r) {\n\t\t\tz[i] = min(r - i + 1, z[i - l]);\n\t\t}\n\t\twhile (z[i] + i < s.size() && s[z[i]] == s[z[i] + i]) z[i]++;\n\t\tif (r < i + z[i] - 1) {\n\t\t\tl = i;\n\t\t\tr = i + z[i] - 1;\n\t\t}\n\t}\n\n return z;\n}\n\nint find(vector<int>& arr, int target) {\n int ans = arr[0];\n int start = 0, end = arr.size() - 1;\n \n while (start <= end){\n int mid = (start+end)/2;\n \n if (abs(arr[mid]-target) < abs(ans-target)) {\n ans = arr[mid];\n } else if(abs(arr[mid]-target) == abs(ans-target)) {\n ans = max(ans, arr[mid]);\n }\n\n if(arr[mid] == target) {\n return arr[mid];\n } else if (arr[mid] < target) {\n start = mid+1;\n } else {\n end = mid-1;\n }\n }\n\n return ans;\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> pos, z1, z2;\n string t1, t2;\n // for (int i = 0; i < a.size(); i++)\n // t1.push_back(a[i]);\n // t1.push_back('$');\n // for (int i = 0; i < s.size(); i++)\n // t1.push_back(s[i]);\n\n // for (int i = 0; i < b.size(); i++)\n // t2.push_back(b[i]);\n // t2.push_back('$');\n // for (int i = 0; i < s.size(); i++)\n // t2.push_back(s[i]);\n\n t1 = a + \"$\" + s;\n t2 = b + \"$\" + s;\n\n z1 = zfunc(t1);\n z2 = zfunc(t2);\n\n for (int i = b.size(); i < z2.size(); i++) {\n if (z2[i] == b.size()) {\n pos.push_back(i - b.size() - 1);\n }\n }\n\n if (!pos.size())\n return {};\n\n vector<int> ans;\n for (int i = a.size(); i < z1.size(); i++) {\n if (z1[i] == a.size()) {\n int idx = i - a.size() - 1;\n int x = find(pos, idx);\n if (abs(x - idx) <= k) ans.push_back(idx);\n }\n }\n\n return ans;\n }\n};",
"memory": "124866"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int compute_pi(int i, vector <int> &pi, string &P){\n if(i<=1) return 0;\n //pi[i] -> length of the longest prefix of P[1...i], which is also a suffix of P[1...i]\n int k = pi[i-1];\n while(P[k+1]!=P[i] && k>0) k = pi[k];\n if(P[k+1]==P[i]) return k+1;\n return 0;\n}\n\nint compute_fi(int i, vector <int> &pi, string &T, string &P, vector <int> &f){\n //f[i] -> length of the longest prefix of P that matches T at index i\n if(i==0) return 0;\n int k = f[i-1];\n while(T[i]!=P[k+1] && k>0) k = pi[k];\n if(T[i]==P[k+1]) return k+1;\n return 0;\n}\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string T, Pa, Pb;\n T.push_back('a');\n Pa.push_back('a');\n Pb.push_back('a');\n int n = s.size(), m_a = a.size(), m_b = b.size();\n for(int i=0; i<n; i++) T.push_back(s[i]);\n for(int i=0; i<m_a; i++) Pa.push_back(a[i]);\n for(int i=0; i<m_b; i++) Pb.push_back(b[i]);\n vector <int> pi_a(m_a+1, 0);\n vector <int> pi_b(m_b+1, 0);\n for(int i=1; i<=m_a; i++) pi_a[i]=compute_pi(i, pi_a, Pa);\n for(int i=1; i<=m_b; i++) pi_b[i]=compute_pi(i, pi_b, Pb);\n vector <int> f_a(n+1, 0);\n vector <int> f_b(n+1, 0);\n vector <int> ans_a, ans_b;\n for(int i=1; i<=n; i++){\n f_a[i] = compute_fi(i, pi_a, T, Pa, f_a);\n f_b[i] = compute_fi(i, pi_b, T, Pb, f_b);\n if(f_a[i]==m_a) ans_a.push_back(i-m_a);\n if(f_b[i]==m_b) ans_b.push_back(i-m_b);\n }\n\n vector <int> final_ans;\n for(int i=0; i<ans_a.size(); i++){\n int idx = ans_a[i];\n auto itl = lower_bound(ans_b.begin(), ans_b.end(), idx);\n if((itl==ans_b.end() || *itl!=idx) && itl!=ans_b.begin()) itl--;\n auto ith = upper_bound(ans_b.begin(), ans_b.end(), idx);\n if((itl!=ans_b.end() && abs(*itl-idx)<=k) || (ith!=ans_b.end() && abs(*ith-idx)<=k)){\n final_ans.push_back(idx);\n }\n }\n return final_ans;\n }\n};",
"memory": "126368"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\n\nvector<int> kmp(string &s){\n\n int n = s.size();\n vector<int> lps(n, 0);\n for(int i = 1; i < n; i++){\n\n int prev_ind = lps[i-1];\n\n while(prev_ind > 0 && s[i] != s[prev_ind]){\n prev_ind = lps[prev_ind - 1];\n }\n\n lps[i] = prev_ind + (s[i] == s[prev_ind] ? 1 : 0);\n }\n return lps;\n}\n\nint justsmaller(int key, vector<int> &arr){\n\n int i = 0, j = arr.size() - 1, ans = -1;\n while(i <= j){\n\n int mid = i + (j-i) / 2;\n if(arr[mid] <= key){\n ans = mid;\n i = mid + 1;\n }\n\n else j = mid - 1;\n }\n\n return ans;\n}\n\nint justgreater(int key, vector<int> &arr){\n\n int i = 0, j = arr.size() - 1, ans = -1;\n while(i <= j){\n\n int mid = i + (j-i) / 2;\n if(arr[mid] >= key){\n ans = mid;\n j = mid - 1;\n }\n\n else i = mid + 1;\n }\n\n return ans;\n}\n\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n \n vector<int> aocc, bocc, lpsa, lpsb;\n string tempa = a, tempb = b;\n tempa += '#'; tempb += '#';\n tempa.append(s); tempb.append(s);\n lpsa = kmp(tempa);\n lpsb = kmp(tempb);\n\n for(int i = a.size(); i < lpsa.size(); i++){\n if(lpsa[i] == a.size()) aocc.push_back(i - 2*a.size());\n }\n\n for(int i = b.size(); i < lpsb.size(); i++){\n if(lpsb[i] == b.size()) bocc.push_back(i - 2*b.size());\n }\n\n sort(bocc.begin(), bocc.end());\n vector<int> ans;\n for(int i = 0; i < aocc.size(); i++){\n\n int key = aocc[i];\n int greater = justgreater(key, bocc);\n int smaller = justsmaller(key, bocc);\n\n if(greater != -1 && abs(key - bocc[greater]) <= k) {\n ans.push_back(key);\n }\n\n else if(smaller != -1 && abs(key - bocc[smaller]) <= k) ans.push_back(key);\n }\n return ans;\n }\n};",
"memory": "126368"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct hash {\n int n, mod, k;\n string s;\n vector<int> base, powk;\n\n void init(int n_, int k_, int mod_, const string& s) {\n n = n_;\n k = k_;\n mod = mod_;\n this->s = s;\n base.resize(n + 1);\n powk.resize(n + 1);\n\n base[0] = 0;\n powk[0] = 1;\n\n for (int i = 0; i < s.length(); i++) {\n int num = (s[i] - 'a') % mod;\n base[i + 1] = (1LL * base[i] * k + num) % mod;\n powk[i + 1] = (1LL * powk[i] * k) % mod;\n }\n }\n\n int get_hash(int l, int r) {\n int ans = (base[r + 1] - 1LL * base[l] * powk[r - l + 1] % mod + mod) % mod;\n return ans;\n }\n };\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int mod = 1e9 + 7;\n hash h;\n h.init(s.length(), 31, mod, s);\n\n vector<int> matchA = findMatch(h, s, a);\n vector<int> matchB = findMatch(h, s, b);\n \n vector<int> result;\n for (auto& i : matchA) {\n auto it = lower_bound(matchB.begin(), matchB.end(), i - k);\n if (it != matchB.end() && abs(*it - i) <= k) {\n result.push_back(i);\n }\n }\n return result;\n }\n\nprivate:\n vector<int> findMatch(hash& h, const string& s, const string& t) {\n if (s.size() < t.size()) return {};\n\n hash ht;\n ht.init(t.size(), 31, h.mod, t);\n int t_hash = ht.get_hash(0, t.size() - 1);\n\n vector<int> indices;\n for (int i = 0; i <= s.size() - t.size(); i++) {\n int s_hash = h.get_hash(i, i + t.size() - 1);\n if (s_hash == t_hash) {\n indices.push_back(i);\n }\n }\n return indices;\n }\n};\n",
"memory": "127871"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n void zarr(string s, vector<int> &z) {\n int n=s.size();\n int l,r,k;\n l=r=0;\n int i;\n for(i=1;i<n;i++) {\n if(i>r) {\n l=r=i;\n while(r<n && s[r]==s[r-l]) {\n r++;\n }\n r--;\n z[i]=r-l+1;\n } else {\n k=i-l;\n if(z[k]<r-i+1) {\n z[i]=z[k];\n }\n else {\n l=i;\n while(r<n && s[r]==s[r-l]) {\n r++;\n }\n r--;\n z[i]=r-l+1;\n }\n }\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string ta=a+\"$\"+s;\n string tb=b+\"$\"+s;\n vector<int> za(s.size()+a.size()+1,0);\n vector<int> zb(s.size()+b.size()+1,0);\n zarr(ta,za);\n zarr(tb,zb);\n vector<int> cb(s.size(),0);\n int i;\n for(i=0;i<s.size();i++) {\n if(zb[i+b.size()+1]==b.size()) {\n cb[i]=1+((i!=0) ? cb[i-1] : 0);\n } else {\n cb[i]=((i==0) ? 0 : cb[i-1]);\n }\n }\n vector<int> ans;\n int n=s.size();\n for(i=0;i<s.size();i++) {\n if(za[i+a.size()+1]==a.size()) {\n int l=max(0,i-k);\n int r=min(n-1,i+k);\n if(l==0 && cb[r]>0) {\n ans.push_back(i);\n } else if(l!=0 && cb[r]-cb[l-1] >0) {\n ans.push_back(i);\n }\n }\n }\n return ans;\n }\n};",
"memory": "127871"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int getRangeSum(vector<int>& prefSum, int l, int r){\n if(l > r){\n return 0;\n }\n if(l == 0){\n return prefSum[r];\n }\n return (prefSum[r] - prefSum[l - 1]);\n }\n \n vector<bool> zAlgorithm(string p, string s){\n string ps = p + \"#\" + s;\n int psLen = ps.length();\n int pLen = p.length();\n int sLen = s.length();\n \n vector<int> z(psLen);\n int l = 0;\n int r = 0;\n for(int i = 1; i < psLen; ++i){\n if(i <= r){\n z[i] = min(r - i + 1, z[i - l]);\n }\n while(i + z[i] < psLen && ps[z[i]] == ps[i + z[i]]){\n z[i] += 1;\n }\n if(r <= i + z[i] - 1){\n l = i;\n r = i + z[i] - 1;\n }\n }\n \n vector<bool> isMatching(sLen);\n for(int i = pLen + 1; i < psLen; ++i){\n isMatching[i - (pLen + 1)] = (z[i] == pLen);\n }\n \n return isMatching;\n }\n \npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n const int N = s.length();\n const int A_LEN = a.length();\n const int B_LEN = b.length();\n \n vector<bool> ok1 = zAlgorithm(a, s);\n vector<bool> ok2 = zAlgorithm(b, s);\n \n vector<int> prefSum2(N);\n prefSum2[0] = (int)ok2[0];\n for(int i = 1; i < N; ++i){\n prefSum2[i] = prefSum2[i - 1] + (int)ok2[i];\n }\n \n vector<int> res;\n for(int i = 0; i < N; ++i){\n if(ok1[i]){\n if(getRangeSum(prefSum2, max(0, i - k), min(N - 1, i + k)) >= 1){\n res.push_back(i);\n }\n }\n }\n \n return res;\n }\n};",
"memory": "129373"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int>kmp(string s){\n int n=s.size();\n vector<int>lps(n,0);\n for(int i=1;i<n;i++){\n int prev_ind=lps[i-1];\n while(prev_ind>0 and s[prev_ind]!=s[i]) prev_ind=lps[prev_ind-1];\n lps[i]=prev_ind +(s[prev_ind]==s[i] ? 1 :0);\n }\n return lps;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string sa=a+\"#\"+s;\n string sb=b+\"#\"+s;\n vector<int> aa = kmp(sa);\n vector<int> bb = kmp(sb);\n\n vector<int>va;\n vector<int>vb;\n for(int i=a.size();i<sa.size();i++){\n if(aa[i]==a.size()) va.push_back(i-2*a.size());\n }\n for(int i=b.size();i<sb.size();i++){\n if(bb[i]==b.size()) vb.push_back(i-2*(b.size()));\n }\n vector<int>ans;\n for(int i=0;i<va.size();i++){\n auto bi=lower_bound(vb.begin(),vb.end(),va[i])-vb.begin();\n if(bi!=vb.size() and abs(vb[bi]-va[i])<=k) {\n ans.push_back(va[i]);\n continue;\n }\n if(bi!=0 and abs(vb[bi-1]-va[i])<=k) ans.push_back(va[i]);\n }\n return ans;\n }\n};",
"memory": "129373"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // String : a a b a a c a a b a a d\n // LPS : 0 1 0 1 2 0 1 2 3 4 5 0\n\n // String : a a b # a a b a a b a a b\n // LPS : 0 1 0 0 1 2 3 1 2 3 1 2 3\n\n // Prefix that matches any subtsring of the string\n // Largest prefix that matches a suffix\n vector<int> kmp(string s){\n vector<int> lps(s.size(), 0);\n\n for(int i=1;i<lps.size();i++){\n int prev_idx = lps[i-1];\n\n while(prev_idx>0 && s[i] != s[prev_idx]){\n prev_idx = lps[prev_idx-1];\n }\n\n lps[i] = prev_idx + (s[i]==s[prev_idx] ? 1 : 0);\n }\n\n return lps;\n }\n \n vector<int> beautifulIndices(string s, string a, string b, int k) {\n \n string sa = a + \"#\" + s;\n string sb = b + \"#\" + s;\n vector<int> va, vb;\n \n vector<int> v = kmp(sa);\n for(int i=a.size();i<v.size();i++){\n int el = v[i];\n if(el == a.size()){\n int id = i - 2*a.size();\n if(id>=0)\n va.push_back(id);\n }\n }\n \n v = kmp(sb);\n for(int i=b.size();i<v.size();i++){\n int el = v[i];\n if(el == b.size()){\n int id = i - 2*b.size();\n if(id >= 0)\n vb.push_back(id);\n }\n }\n \n vector<int> ans;\n if(vb.size()==0 || va.size()==0) return ans;\n int bi = 0;\n for(int i=0;i<va.size();i++){\n auto bi = lower_bound(vb.begin(), vb.end(), va[i]) - vb.begin();\n if(bi != vb.size() && abs(va[i] - vb[bi]) <= k) {ans.push_back(va[i]); continue;}\n if(bi != 0 && abs(va[i] - vb[bi-1]) <= k) ans.push_back(va[i]);\n }\n \n return ans;\n }\n};",
"memory": "130876"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // Largest prefix that matches a suffix\n vector<int> kmp(string s){\n vector<int> lps(s.size(), 0);\n\n for(int i=1;i<lps.size();i++){\n int prev_idx = lps[i-1];\n while(prev_idx>0 && s[i] != s[prev_idx]){\n prev_idx = lps[prev_idx-1];\n }\n lps[i] = prev_idx + (s[i]==s[prev_idx] ? 1 : 0);\n }\n return lps;\n }\n\n int upper_bound(vector<int> &arr, int val) {\n int ans = -1;\n int i=0; \n int j=arr.size()-1;\n while(i <= j) {\n int m = (i+j)/2;\n if(arr[m] < val) i=m+1;\n else {\n ans = m;\n j=m-1;\n }\n }\n return ans;\n }\n\n int lower_bound(vector<int> &arr, int val) {\n int ans = -1;\n int i=0; \n int j=arr.size()-1;\n while(i <= j) {\n int m = (i+j)/2;\n if(arr[m] > val) j=m-1;\n else {\n ans = m;\n i=m+1;\n }\n }\n return ans;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int ns = s.size();\n int na = a.size();\n int nb = b.size();\n string msa = a+\"#\"+s;\n string msb = b+\"#\"+s;\n vector<int> lps_a = kmp(msa);\n vector<int> lps_b = kmp(msb);\n\n vector<int> a_ind;\n vector<int> b_ind;\n for(int i=0; i<lps_a.size(); i++) {\n if(lps_a[i] == na) a_ind.push_back(i-(2*na));\n }\n for(int i=0; i<lps_b.size(); i++) {\n if(lps_b[i] == nb) b_ind.push_back(i-(2*nb));\n }\n\n vector<int> ans;\n if(a_ind.size() == 0 || b_ind.size() == 0) return ans;\n else {\n for(int i=0; i<a_ind.size(); i++) {\n int ub = upper_bound(b_ind, a_ind[i]);\n int lb = lower_bound(b_ind, a_ind[i]);\n bool take = false;\n if(ub != -1) {\n if(abs(a_ind[i]-b_ind[ub]) <= k) take = true;\n } \n if(lb != -1) {\n if(abs(a_ind[i]-b_ind[lb]) <= k) take = true;\n }\n if(take) ans.push_back(a_ind[i]);\n }\n return ans;\n } \n }\n};",
"memory": "130876"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "// class Solution {\n// public:\n// vector<int> beautifulIndices(string s, string a, string b, int k) {\n \n// }\n// };\n\nclass Solution {\npublic:\n\n vector<int> kmp(string s)\n {\n vector<int> lps(s.size(), 0);\n for(int i=1 ; i<lps.size() ; i++)\n {\n int prev_ind=lps[i-1];\n while(s[prev_ind]!=s[i] && prev_ind>0) prev_ind=lps[prev_ind-1];\n lps[i]=prev_ind+(s[i]==s[prev_ind]);\n }\n return lps;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string t1=a, t2=b;\n t1+=\"#\";\n t2+=\"#\";\n t1+=s;\n t2+=s;\n vector<int> lps1=kmp(t1), lps2=kmp(t2);\n vector<int> p1, p2;\n for(int i=a.length() ; i<lps1.size() ; i++)\n {\n if(lps1[i]==a.length()) p1.push_back(i-2*a.length());\n }\n for(int i=0 ; i<lps2.size() ; i++)\n {\n // cout<<lps2[i]<<\" \";\n if(lps2[i]==b.length())\n {\n // cout<<i<<\" \";\n p2.push_back(i-2*b.length());\n } \n }\n // cout<<\"\\n\";\n // for(int i=0 ; i<p1.size() ; i++) cout<<p1[i]<<\" \";\n // cout<<\"\\n\";\n // for(int i=0 ; i<p2.size() ; i++) cout<<p2[i]<<\" \";\n vector<int> ans;\n for(int i=0 ; i<p1.size() ; i++)\n {\n auto u=lower_bound(p2.begin(), p2.end(), p1[i])-p2.begin();\n // cout<<u<<\" \";\n if(u<p2.size() && abs(p1[i]-p2[u])<=k)\n {\n ans.push_back(p1[i]);\n continue;\n }\n if(u>0) \n {\n if(abs(p1[i]-p2[u-1])<=k)\n {\n ans.push_back(p1[i]);\n continue;\n }\n }\n }\n return ans;\n }\n};",
"memory": "132378"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "vector<int> zfunc(string s) {\n int l, r;\n l = r = 0;\n vector<int> z(s.size());\n\tz[0] = 0;\n \n\tfor (int i = 1; i < s.size(); i++) {\n\t\tif (i <= r) {\n\t\t\tz[i] = min(r - i + 1, z[i - l]);\n\t\t}\n\t\twhile (z[i] + i < s.size() && s[z[i]] == s[z[i] + i]) z[i]++;\n\t\tif (r < i + z[i] - 1) {\n\t\t\tl = i;\n\t\t\tr = i + z[i] - 1;\n\t\t}\n\t}\n\n return z;\n}\n\nint find(vector<int>& arr, int target) {\n int ans = arr[0];\n int start = 0, end = arr.size() - 1;\n \n while (start <= end){\n int mid = (start+end)/2;\n \n if (abs(arr[mid]-target) < abs(ans-target)) {\n ans = arr[mid];\n } else if(abs(arr[mid]-target) == abs(ans-target)) {\n ans = max(ans, arr[mid]);\n }\n\n if(arr[mid] == target) {\n return arr[mid];\n } else if (arr[mid] < target) {\n start = mid+1;\n } else {\n end = mid-1;\n }\n }\n\n return ans;\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> pos, z1, z2;\n string t1, t2;\n for (int i = 0; i < a.size(); i++)\n t1.push_back(a[i]);\n t1.push_back('$');\n for (int i = 0; i < s.size(); i++)\n t1.push_back(s[i]);\n\n for (int i = 0; i < b.size(); i++)\n t2.push_back(b[i]);\n t2.push_back('$');\n for (int i = 0; i < s.size(); i++)\n t2.push_back(s[i]);\n\n // t1 = a + \"$\" + s;\n // t2 = b + \"$\" + s;\n\n z1 = zfunc(t1);\n z2 = zfunc(t2);\n\n for (int i = b.size(); i < z2.size(); i++) {\n if (z2[i] == b.size()) {\n pos.push_back(i - b.size() - 1);\n }\n }\n\n if (!pos.size())\n return {};\n\n vector<int> ans;\n for (int i = a.size(); i < z1.size(); i++) {\n if (z1[i] == a.size()) {\n int idx = i - a.size() - 1;\n int x = find(pos, idx);\n if (abs(x - idx) <= k) ans.push_back(idx);\n }\n }\n\n return ans;\n }\n};",
"memory": "133881"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void FillLPS(vector<int>&LPS, string& needle)\n {\n LPS[0] = 0;\n int i = 0;\n int j = 1;\n while(j<needle.length())\n {\n if(needle[j] == needle[i])\n {\n i++;\n LPS[j] = i;\n j++;\n }\n else\n {\n if(i == 0)\n {\n LPS[j] = 0;\n j++;\n } \n else\n {\n i = LPS[i-1];\n }\n }\n }\n }\n void KMP(vector<int>&result , string& haystack, string& needle)\n {\n int n = haystack.length();\n int m = needle.length();\n\n vector<int>LPS(m);\n FillLPS(LPS , needle);\n\n int i = 0 , j = 0;\n while(i<n)\n {\n if(haystack[i] == needle[j])\n {\n i++;\n j++;\n }\n if(j==m)\n {\n result.push_back(i - m);\n j = LPS[j-1];\n }\n else if(haystack[i] != needle[j])\n {\n if(j == 0) \n i++;\n else \n j = LPS[j-1];\n }\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n \n vector<int>a_idx;\n KMP(a_idx , s , a);\n\n vector<int>j_idx;\n KMP(j_idx , s , b);\n\n set<int>st(j_idx.begin() , j_idx.end());\n vector<int>ans;\n for(int &i : a_idx)\n {\n int left_limit = max(0 , i - k);\n int right_limit = min(n-1 , i+k);\n\n auto it = st.lower_bound(left_limit);\n if(it != st.end() and *it <= right_limit)\n {\n ans.push_back(i);\n }\n }\n \n return ans;\n }\n};",
"memory": "135383"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void FillLPS(vector<int>&LPS, string& needle)\n {\n LPS[0] = 0;\n int i = 0;\n int j = 1;\n while(j<needle.length())\n {\n if(needle[j] == needle[i])\n {\n i++;\n LPS[j] = i;\n j++;\n }\n else\n {\n if(i == 0)\n {\n LPS[j] = 0;\n j++;\n } \n else\n {\n i = LPS[i-1];\n }\n }\n }\n }\n void KMP(vector<int>&result , string& haystack, string& needle)\n {\n int n = haystack.length();\n int m = needle.length();\n\n vector<int>LPS(m);\n FillLPS(LPS , needle);\n\n int i = 0 , j = 0;\n while(i<n)\n {\n if(haystack[i] == needle[j])\n {\n i++;\n j++;\n }\n if(j==m)\n {\n result.push_back(i - m);\n j = LPS[j-1];\n }\n else if(haystack[i] != needle[j])\n {\n if(j == 0) \n i++;\n else \n j = LPS[j-1];\n }\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n \n vector<int>a_idx;\n KMP(a_idx , s , a);\n\n vector<int>j_idx;\n KMP(j_idx , s , b);\n\n set<int>st(j_idx.begin() , j_idx.end());\n vector<int>ans;\n for(int &i : a_idx)\n {\n int left_limit = max(0 , i - k);\n int right_limit = min(n-1 , i+k);\n\n auto it = st.lower_bound(left_limit);\n if(it != st.end() and *it <= right_limit)\n {\n ans.push_back(i);\n }\n }\n \n return ans;\n }\n};",
"memory": "136886"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void computeLps(vector<int>& lps, string& a) {\n int m = a.size();\n int len = 0;\n int i = 1;\n while (i < m) {\n if (a[i] == a[len]) {\n len ++;\n lps[i] = len;\n i ++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i ++;\n }\n }\n }\n }\n void kmpComputeIndex(vector<int>& index, string& s, string& a) {\n int m = a.size();\n vector<int> lps(m, 0);\n computeLps(lps, a);\n int n = s.size();\n int i = 0;\n int j = 0;\n while (i < n) {\n if (s[i] == a[j]) {\n i ++;\n j ++;\n }\n if (j == m) {\n index.push_back(i - m);\n j = lps[j - 1];\n } else if (i < n && s[i] != a[j]) {\n if (j != 0) {\n j = lps[j - 1];\n } else {\n i ++;\n }\n }\n\n }\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> aIndex, bIndex;\n kmpComputeIndex(aIndex, s, a);\n kmpComputeIndex(bIndex, s, b);\n vector<int> ans;\n set<int> set;\n for (auto i : aIndex) {\n int lb = lower_bound(bIndex.begin(), bIndex.end(), i - k) - bIndex.begin();\n if (lb < bIndex.size() && abs(bIndex[lb] - i) <= k) {\n set.insert(i);\n }\n lb = lower_bound(bIndex.begin(), bIndex.end(), i + k) - bIndex.begin();\n if (lb < bIndex.size() && abs(bIndex[lb] - i) <= k) {\n set.insert(i);\n }\n }\n for (auto ele : set) {\n ans.push_back(ele);\n }\n return ans;\n }\n};",
"memory": "138388"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "int mod = (1e9) + 7;\nvector<int> p;\nvector<int> q;\nvoid calcPower()\n{\n p.push_back(1);\n long long temp = 1;\n for (int i = 1; i <= 500000; i++)\n {\n temp = (temp * 31) % mod;\n p.push_back(temp);\n }\n q.push_back(1);\n temp = 1;\n for (int i = 1; i <= 500000; i++)\n {\n temp = (temp * 41) % mod;\n q.push_back(temp);\n }\n}\n\nint add(long long a, long long b)\n{\n if (a + b < 0)\n {\n return a + b + mod;\n }\n return (a + b) % mod;\n}\n\nint pow(int a, int b)\n{\n if (b == 0)\n {\n return 1;\n }\n int x = pow(a, b / 2);\n x = (1LL * x * x) % mod;\n if (b % 2 != 0)\n {\n x = (1LL * a * x) % mod;\n }\n return x;\n}\nint inverse(int a)\n{\n\n return pow(p[a], mod - 2);\n}\nint inverseQ(int a)\n{\n\n return pow(q[a], mod - 2);\n}\nvector<int> calHash(string s)\n{\n vector<int> pre(s.size());\n int c = 0;\n for (int i = 0; i < s.size(); i++)\n {\n long long temp = (1LL * p[i] * (s[i] - 'a' + 1)) % mod;\n c = (temp + c) % mod;\n pre[i] = c;\n }\n return pre;\n}\nvector<int> calHashQ(string s)\n{\n vector<int> pre(s.size());\n int c = 0;\n for (int i = 0; i < s.size(); i++)\n {\n long long temp = (1LL * q[i] * (s[i] - 'a' + 1)) % mod;\n c = (temp + c) % mod;\n pre[i] = c;\n }\n return pre;\n}\nvector<int> matchingIndices(vector<int> &a, vector<int> &b,vector<int> &Qa, vector<int> &Qb)\n{\n\n int n = a.size();\n int m = b.size();\n vector<int> ans;\n int hashB = b[m - 1];\n int hashBQ = Qb[m - 1];\n for (int i = 0; i <= n - m; i++)\n {\n int reqHash = (i == 0 ? a[i + m - 1] : add(a[i + m - 1], -1*a[i - 1]));\n reqHash = (1LL * inverse(i) * reqHash) % mod;\n int reqHashQ = (i == 0 ? Qa[i + m - 1] : add(Qa[i + m - 1], -1*Qa[i - 1]));\n reqHashQ = (1LL * inverseQ(i) * reqHashQ) % mod;\n if (reqHash == hashB && reqHashQ== hashBQ )\n ans.push_back(i);\n }\n return ans;\n}\n\nclass Solution\n{\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k)\n {\n if (p.size() == 0)\n {\n calcPower();\n }\n vector<int> preS = calHash(s);\n vector<int> preA = calHash(a);\n vector<int> preB = calHash(b);\n vector<int> preSQ = calHashQ(s);\n vector<int> preAQ = calHashQ(a);\n vector<int> preBQ = calHashQ(b);\n vector<int> aIndices;\n vector<int> bIndices;\n aIndices = matchingIndices(preS, preA,preSQ, preAQ);\n bIndices = matchingIndices(preS, preB,preSQ, preBQ);\n vector<int> ans;\n int m = bIndices.size();\n int l = 0;\n int r = 0;\n for (int i = 0; i < aIndices.size(); i++)\n {\n \n while (l < m && (aIndices[i] - k > bIndices[l]))\n {\n l++;\n }\n while (r < m && aIndices[i] > bIndices[r])\n {\n r++;\n }\n if ((l != m && bIndices[l] - aIndices[i] <= k) || (r != m && bIndices[r] - aIndices[i] <= k))\n {\n ans.push_back(aIndices[i]);\n }\n }\n return ans;\n }\n};\n\n",
"memory": "139891"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "void hashing(string &s,vector<long long int>&h,vector<long long int>&pp){\n int n = s.length();\n h[0] = s[0]-96;\n long long int mod = 1e9+9;\n long long int prime = 31;\n for(int i=1;i<n;i++){\n long long int t = s[i]-96;\n h[i] = (h[i-1] + (t*pp[i])%mod)%mod;\n }\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n = s.length();\n int na = a.length();\n int nb = b.length();\n vector<long long int>hs(n);\n vector<long long int>ha(na);\n vector<long long int>hb(nb);\n int n1 = max(max(na,nb),n);\n vector<long long int>pp(n1);\n pp[0] = 1;\n long long int prime = 31;\n long long int mod = 1e9+9;\n for(int i=1;i<n1;i++){\n pp[i] = (pp[i-1]*prime)%mod;\n }\n hashing(s,hs,pp); hashing(a,ha,pp); hashing(b,hb,pp);\n\n vector<int>v1,v2;\n for(int i=0;i<n;i++){\n long long int b1=-1;\n long long int b2=-1;\n if(i+na-1 < n) {b1 = hs[i+na-1];\n if(i>0) b1 = (b1-hs[i-1]+mod)%mod;}\n if(i+nb-1 < n) { b2 = hs[i+nb-1];\n if(i>0) b2 = (b2-hs[i-1]+mod)%mod;}\n\n if(b1 == (ha[na-1]*pp[i])%mod) v1.push_back(i);\n if(b2 == (hb[nb-1]*pp[i])%mod) v2.push_back(i);\n }\n\n // for(int i=0;i<v1.size();i++) cout << v1[i] << \" \"; cout << endl;\n // for(int i=0;i<v2.size();i++) cout << v2[i] << \" \"; cout << endl;\n\n vector<int>ans;\n for(int i=0;i<n;i++){\n if(i>n-na) break;\n \n if(!binary_search(v1.begin(),v1.end(),i)) continue;\n\n int in = lower_bound(v2.begin(),v2.end(),i-k) - v2.begin();\n // in = v2[in];\n if(in==v2.size() || v2[in] > i+k) continue;\n ans.push_back(i);\n }\n\n return ans;\n\n }\n};",
"memory": "141393"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> kmp(string& s, string& a)\n {\n string sa=a;\n sa+='#';\n sa+=s;\n vector<int>lps(sa.size(),0);\n for(int i=1;i<sa.size();i++)\n {\n int prev_ind=lps[i-1];//go to max prefix match of previous element and try to extend it\n while(prev_ind>0 && sa[i]!=sa[prev_ind])\n {\n prev_ind=lps[prev_ind-1];//go to next max prefix match of previous element and try to extend it\n }\n lps[i]=prev_ind+(sa[i]==sa[prev_ind]?1:0);\n }\n return lps;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int szs=s.size();\n int sza=a.size();\n int szb=b.size();\n vector<int>lpsa=kmp(s,a);\n vector<int>lpsb=kmp(s,b);\n vector<int>vala(szs,0);\n for(int i=sza+1;i<lpsa.size();i++)\n {\n //cout<<lpsa[i]<<endl;\n if(lpsa[i]==sza)\n {\n int x=i-sza-1;//this is the end index in main string\n x=x-sza+1;//this is the starting point in main string\n vala[x]=1;\n }\n }\n vector<int>valb(szs,0);\n for(int i=szb+1;i<lpsb.size();i++)\n {\n //cout<<lpsb[i]<<endl;\n if(lpsb[i]==szb)\n {\n int x=i-szb-1;//this is the end index in main string\n x=x-szb+1;//this is the starting point in main string\n valb[x]=1;\n }\n }\n //now got our required index with TC O(N+M) using kmp\n //now we need to find for one index in vala is there a index in valb with in the range \n //this can be done with binary search with nlogn \n ///but lets do this in O(n) using prefix sum\n vector<int>prefix(szs,0);\n for(int i=0;i<szs;i++)\n {\n prefix[i]=valb[i];\n if(i!=0) prefix[i]+=prefix[i-1];\n }\n vector<int>res;\n for(int i=0;i<szs;i++)\n {\n if(vala[i])\n {\n int ub=(i+k<szs?i+k:szs-1);\n int lb=(i-k>=0?i-k:0);\n if(lb==0)\n {\n if(prefix[ub]>0)res.push_back(i);\n }\n else\n {\n if(prefix[ub]-prefix[lb-1]>0) res.push_back(i);\n }\n }\n }\n return res;\n }\n};",
"memory": "142896"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> KMPSearch(string pat, string txt) {\n int M = pat.size();\n int N = txt.size();\n vector<int> a;\n\n vector<int> lps(M);\n\n auto computeLPSArray = [&](string pat, int M, vector<int>& lps) {\n int len = 0;\n lps[0] = 0; \n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n };\n\n computeLPSArray(pat, M, lps);\n\n int i = 0;\n int j = 0;\n while (i < N) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n a.push_back(i - j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return a;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a1 = KMPSearch(a, s);\n vector<int> a2 = KMPSearch(b, s);\n\n int i = 0, j = 0;\n set<int> ans;\n for(auto it : a1){\n auto it2 = upper_bound(a2.begin(), a2.end(), it + k);\n if(it2 != a2.begin()) {\n it2--;\n }\n \n if(it2 != a2.end() && abs(*it2 - it) <= k) {\n ans.insert(it);\n }\n }\n \n vector<int> m;\n for(auto it : ans){\n m.push_back(it);\n }\n \n return m;\n }\n};",
"memory": "144398"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> KMPSearch(string pat, string txt) {\n int M = pat.size();\n int N = txt.size();\n vector<int> a;\n\n vector<int> lps(M);\n\n auto computeLPSArray = [&](string pat, int M, vector<int>& lps) {\n int len = 0;\n lps[0] = 0; \n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n };\n\n computeLPSArray(pat, M, lps);\n\n int i = 0;\n int j = 0;\n while (i < N) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n a.push_back(i - j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return a;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a1 = KMPSearch(a, s);\n vector<int> a2 = KMPSearch(b, s);\n\n int i = 0, j = 0;\n set<int> ans;\n for(auto it : a1){\n auto it2 = upper_bound(a2.begin(), a2.end(), it + k);\n if(it2 != a2.begin()) {\n it2--;\n }\n \n if(it2 != a2.end() && abs(*it2 - it) <= k) {\n ans.insert(it);\n }\n }\n \n vector<int> m;\n for(auto it : ans){\n m.push_back(it);\n }\n \n return m;\n }\n};",
"memory": "144398"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> KMPSearch(string pat, string txt) {\n int M = pat.size();\n int N = txt.size();\n vector<int> a;\n\n vector<int> lps(M);\n\n auto computeLPSArray = [&](string pat, int M, vector<int>& lps) {\n int len = 0;\n lps[0] = 0; \n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n };\n\n computeLPSArray(pat, M, lps);\n\n int i = 0;\n int j = 0;\n while (i < N) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n a.push_back(i - j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return a;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a1 = KMPSearch(a, s);\n vector<int> a2 = KMPSearch(b, s);\n\n int i = 0, j = 0;\n set<int> ans;\n for(auto it : a1){\n auto it2 = upper_bound(a2.begin(), a2.end(), it + k);\n if(it2 != a2.begin()) {\n it2--;\n }\n \n if(it2 != a2.end() && abs(*it2 - it) <= k) {\n ans.insert(it);\n }\n }\n \n vector<int> m;\n for(auto it : ans){\n m.push_back(it);\n }\n \n return m;\n }\n};",
"memory": "145901"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> KMPSearch(string pat, string txt) {\n int M = pat.size();\n int N = txt.size();\n vector<int> a;\n\n vector<int> lps(M);\n\n auto computeLPSArray = [&](string pat, int M, vector<int>& lps) {\n int len = 0;\n lps[0] = 0; \n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n };\n\n computeLPSArray(pat, M, lps);\n\n int i = 0;\n int j = 0;\n while (i < N) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n a.push_back(i - j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return a;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a1 = KMPSearch(a, s);\n vector<int> a2 = KMPSearch(b, s);\n\n int i = 0, j = 0;\n set<int> ans;\n for(auto it : a1){\n auto it2 = upper_bound(a2.begin(), a2.end(), it + k);\n if(it2 != a2.begin()) {\n it2--;\n }\n \n if(it2 != a2.end() && abs(*it2 - it) <= k) {\n ans.insert(it);\n }\n }\n \n vector<int> m;\n for(auto it : ans){\n m.push_back(it);\n }\n \n return m;\n }\n};",
"memory": "147403"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> KMPSearch(string pat, string txt) {\n int M = pat.size();\n int N = txt.size();\n vector<int> a;\n\n vector<int> lps(M);\n\n auto computeLPSArray = [&](string pat, int M, vector<int>& lps) {\n int len = 0;\n lps[0] = 0; \n int i = 1;\n while (i < M) {\n if (pat[i] == pat[len]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len != 0) {\n len = lps[len - 1];\n } else {\n lps[i] = 0;\n i++;\n }\n }\n }\n };\n\n computeLPSArray(pat, M, lps);\n\n int i = 0;\n int j = 0;\n while (i < N) {\n if (pat[j] == txt[i]) {\n j++;\n i++;\n }\n\n if (j == M) {\n a.push_back(i - j);\n j = lps[j - 1];\n } else if (i < N && pat[j] != txt[i]) {\n if (j != 0)\n j = lps[j - 1];\n else\n i = i + 1;\n }\n }\n\n return a;\n }\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a1 = KMPSearch(a, s);\n vector<int> a2 = KMPSearch(b, s);\n\n int i = 0, j = 0;\n set<int> ans;\n for(auto it : a1){\n auto it2 = upper_bound(a2.begin(), a2.end(), it + k);\n if(it2 != a2.begin()) {\n it2--;\n }\n \n if(it2 != a2.end() && abs(*it2 - it) <= k) {\n ans.insert(it);\n }\n }\n \n vector<int> m;\n for(auto it : ans){\n m.push_back(it);\n }\n \n return m;\n }\n};",
"memory": "148906"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string sa = a + \"#\" + s;\n string sb = b + \"#\" + s;\n\n int len = s.length();\n\n int lena = sa.length();\n int lenb = sb.length();\n\n vector<int> kmpa(lena+1, 0);\n vector<int> kmpb(lenb+1, 0);\n\n vector<int> A(len+1);\n vector<int> B = A;\n\n int i = 0;\n int j = -1;\n\n int alen = a.length();\n int blen = b.length();\n kmpa[0] = -1;\n // cout<<sa<<\" \"<<sb<<\" HHHHHHH\\n\";\n\n while(i < lena){\n // cout<<\"A\\n\";\n while(j != -1 && sa[j] != sa[i]){\n j = kmpa[j];\n // cout<<\"E\\n\";\n // cout<<j<<\" \"<<sa[i]<<\" \"<<sa[j]<<\" BRUH\\n\";\n }\n i++;\n j++;\n kmpa[i] = j;\n // A[i-]\n // cout<<i<<\" \"<<kmpa[i]<<\" \"<<sa[i]<<\" AAAAAAA\\n\";\n if(i > alen + 1){\n // cout<<sa[i-1]<<\" OK\\n\";\n A[i-alen-2] = kmpa[i];\n // if(A[i-alen-2] == alen){\n // cout<<(i-alen-2)<<\" OOOOOOoo\\n\";\n // }\n }\n\n }\n\n i = 0;\n j = -1;\n kmpb[i] = -1;\n\n while(i < lenb){\n // cout<<\"BB\\n\";\n while(j != -1 && sb[i] != sb[j]) j = kmpb[j];\n i++;\n j++;\n kmpb[i] = j;\n\n if(i > blen + 1) B[i-blen-2] = kmpb[i];\n }\n int last = -1e9;\n vector<int> P(len, 0);\n vector<int> N(len, 0);\n\n // for(int i = 0; i < len; i++) cout<<A[i];\n // cout<<endl;\n // for(int i = 0; i < len; i++) cout<<B[i];\n // cout<<endl;\n for(int i = 0; i < len; i++){\n if(B[i] == blen) last = i-blen+1;\n if(A[i] == alen){\n P[i] = last;\n }\n }\n\n last = 1e9;\n\n for(int i = len-1; i >= 0; i--){\n if(B[i] == blen) last = i-blen+1;\n if(A[i] == alen){\n N[i] = last;\n }\n }\n\n int cnt = 0;\n vector<int> F;\n\n for(int i = 0; i < len; i++){\n if(A[i] != alen) continue;\n // cout<<i<<\" IDL\\n\";\n int cur = (abs)(P[i] - (i-alen+1));\n int cur2 = (abs)(N[i] - (i-alen+1));\n\n if(i < alen - 1) continue;\n\n\n cur = min(cur, cur2);\n if(cur <= k) F.push_back(i-alen+1);\n }\n\n return F;\n // return cnt;\n\n \n }\n};",
"memory": "150408"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> kmp(string t,string p){\n int m=p.size();\n p+='#';\n p+=t;\n int n=p.size();\n vector<int> pre(n);\n for(int i=1;i<n;i++){\n int j=pre[i-1];\n while(j && p[i]!=p[j])\n j=pre[j-1];\n if(p[i]==p[j])\n j++;\n pre[i]=j;\n }\n vector<int> r(pre.begin()+m+1,pre.end());\n return r;\n }\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> x,y; \n x=kmp(s,a);\n y=kmp(s,b);\n int n=s.size();\n vector<int> ind1,ind2;\n for(int i=0;i<n;i++){\n // cout<<x[i]<<\" \"<<y[i]<<\"\\n\";\n if(x[i]==a.size())\n ind1.push_back(i-a.size()+1);\n if(y[i]==b.size())\n ind2.push_back(i-b.size()+1);\n }\n // cout<<ind1.size()<<\" \"<<ind2.size()<<\" \";\n vector<int> ans;\n for(auto &val:ind1){\n auto it=upper_bound(ind2.begin(),ind2.end(),val-1);\n if(it!=ind2.end()){\n if(*it-val<=k)\n ans.push_back(val);\n else{\n if(it!=ind2.begin())\n {\n it--;\n if(abs(*it-val)<=k)\n ans.push_back(val);\n }\n }\n }\n else{\n if(it!=ind2.begin()){\n it--;\n if(abs(*it-val)<=k)\n ans.push_back(val);\n }\n }\n }\n return ans;\n }\n};",
"memory": "151911"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <algorithm>\n#include <cmath>\n\nusing namespace std;\n\nvector<int> rk(const string &t, const string &s) {\n const uint64_t p = 31;\n vector<uint64_t> p_pow(max(s.size(), t.size()));\n p_pow[0] = 1;\n for (size_t i = 1; i < p_pow.size(); ++i) {\n p_pow[i] = p_pow[i - 1] * p;\n }\n \n vector<uint64_t> h(t.size());\n for (size_t i = 0; i < t.size(); ++i) {\n h[i] = (t[i] - 'a' + 1) * p_pow[i];\n if (i > 0) {\n h[i] += h[i - 1];\n }\n }\n \n uint64_t hash_s = 0;\n for (size_t i = 0; i < s.size(); ++i) {\n hash_s += (s[i] - 'a' + 1) * p_pow[i];\n }\n\n vector<int> res;\n for (size_t i = 0; i + s.size() - 1 < t.size(); ++i) {\n uint64_t curr_h = h[i + s.size() - 1];\n if (i > 0) {\n curr_h -= h[i - 1];\n }\n if (curr_h == hash_s * p_pow[i]) {\n res.push_back(i);\n }\n }\n \n return res;\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(const string &s, const string &a, const string &b, int k) {\n vector<int> sa = rk(s, a);\n vector<int> sb = rk(s, b);\n \n if (sb.empty()) {\n return {};\n }\n\n vector<int> an;\n for (int i : sa) {\n auto le = lower_bound(sb.begin(), sb.end(), i);\n auto re = upper_bound(sb.begin(), sb.end(), i);\n \n if (le != sb.end() && abs(*le - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (le != sb.begin() && abs(*(le - 1) - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (re != sb.end() && abs(*re - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (re == sb.end() && abs(*(re - 1) - i) <= k) {\n an.push_back(i);\n continue;\n }\n }\n \n return an;\n }\n};\n",
"memory": "153413"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <string>\n#include <algorithm>\n#include <cmath>\n\nusing namespace std;\n\nvector<int> rk(const string &t, const string &s) {\n const uint64_t p = 31;\n vector<uint64_t> p_pow(max(s.size(), t.size()));\n p_pow[0] = 1;\n for (size_t i = 1; i < p_pow.size(); ++i) {\n p_pow[i] = p_pow[i - 1] * p;\n }\n \n vector<uint64_t> h(t.size());\n for (size_t i = 0; i < t.size(); ++i) {\n h[i] = (t[i] - 'a' + 1) * p_pow[i];\n if (i > 0) {\n h[i] += h[i - 1];\n }\n }\n \n uint64_t hash_s = 0;\n for (size_t i = 0; i < s.size(); ++i) {\n hash_s += (s[i] - 'a' + 1) * p_pow[i];\n }\n\n vector<int> res;\n for (size_t i = 0; i + s.size() - 1 < t.size(); ++i) {\n uint64_t curr_h = h[i + s.size() - 1];\n if (i > 0) {\n curr_h -= h[i - 1];\n }\n if (curr_h == hash_s * p_pow[i]) {\n res.push_back(i);\n }\n }\n \n return res;\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(const string &s, const string &a, const string &b, int k) {\n vector<int> sa = rk(s, a);\n vector<int> sb = rk(s, b);\n \n if (sb.empty()) {\n return {};\n }\n\n vector<int> an;\n for (int i : sa) {\n auto le = lower_bound(sb.begin(), sb.end(), i);\n auto re = upper_bound(sb.begin(), sb.end(), i);\n \n if (le != sb.end() && abs(*le - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (le != sb.begin() && abs(*(le - 1) - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (re != sb.end() && abs(*re - i) <= k) {\n an.push_back(i);\n continue;\n }\n if (re == sb.end() && abs(*(re - 1) - i) <= k) {\n an.push_back(i);\n continue;\n }\n }\n \n return an;\n }\n};\n",
"memory": "154916"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int> zalgo(string &s){\n int n=s.length();\n vector<int> v(n,0);\n int l=0,r=0;\n for(int i=1;i<n;i++){\n if(i<r){\n v[i]=min(r-i,v[i-l]);\n }\n while(i+v[i]<n && s[v[i]]==s[v[i]+i]) v[i]++;\n if(i+v[i]>r){\n l=i;\n r=v[i]+i;\n }\n }\n return v;\n }\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n=a.length(),m=b.length();\n a=a+'#'+s;\n b=b+'#'+s;\n // cout<<a<<endl;\n // cout<<b<<endl;\n vector<int> az=zalgo(a);\n vector<int> bz=zalgo(b);\n vector<int> aind;\n for(int i=0;i<az.size();i++){\n // cout<<az[i]<<\" \";\n if(az[i]==n){\n aind.push_back(i-(n+1));\n }\n }\n //zalgo finds the longest string matching to prefix\n vector<int> ans;\n set<int> sb;\n for(int i=1;i<bz.size();i++){\n // cout<<bz[i]<<\" \";\n if(bz[i]==m){\n \n sb.insert(i-(m+1));\n }\n }\n // cout<<aind.size()<<\" \"<<s.size()<<endl;\n for(int i=0;i<aind.size();i++){\n //for every starting index of a(ai) in s find a nearest starting index of b towards left(lb) and right(rb) if difference between any of these i.e abs(ai-lb) <=k or abs(ai-rb)<=k then ai is a beautiful index\n cout<<aind[i]<<\" \";\n auto it=sb.lower_bound(aind[i]);\n int isBeautiful=0;\n // checking to right\n if(it!=sb.end()){\n // cout<<*it<<endl;\n if(abs(*it-aind[i])<=k)\n isBeautiful=1;\n }\n \n if(it!=sb.begin()){\n it--;\n // cout<<*it<<endl;\n // cout<<i<<\" \"<<*it<<endl;\n if(abs(*it-aind[i])<=k)\n isBeautiful=1;\n }\n if(isBeautiful){\n ans.push_back(aind[i]);\n }\n }\n return ans;\n }\n};",
"memory": "160926"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<int> zalgo(string &s){\n int n=s.length();\n vector<int> v(n,0);\n int l=0,r=0;\n for(int i=1;i<n;i++){\n if(i<r){\n v[i]=min(r-i,v[i-l]);\n }\n while(i+v[i]<n && s[v[i]]==s[v[i]+i]) v[i]++;\n if(i+v[i]>r){\n l=i;\n r=v[i]+i;\n }\n }\n return v;\n }\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n int n=a.length(),m=b.length();\n a=a+'#'+s;\n b=b+'#'+s;\n vector<int> az=zalgo(a);\n vector<int> bz=zalgo(b);\n vector<int> aind;\n for(int i=0;i<az.size();i++){\n if(az[i]==n){\n aind.push_back(i-(n+1));\n }\n }\n //zalgo finds the longest string matching to prefix\n vector<int> ans;\n set<int> sb;\n for(int i=1;i<bz.size();i++){\n if(bz[i]==m){\n \n sb.insert(i-(m+1));\n }\n }\n for(int i=0;i<aind.size();i++){\n //for every starting index of a(ai) in s find a nearest starting index of b towards left(lb) and right(rb) if difference between any of these i.e abs(ai-lb) <=k or abs(ai-rb)<=k then ai is a beautiful index\n auto it=sb.lower_bound(aind[i]);\n int isBeautiful=0;\n // checking to right\n if(it!=sb.end()){\n if(abs(*it-aind[i])<=k)\n isBeautiful=1;\n }\n \n if(it!=sb.begin()){\n it--;\n if(abs(*it-aind[i])<=k)\n isBeautiful=1;\n }\n if(isBeautiful){\n ans.push_back(aind[i]);\n }\n }\n return ans;\n }\n};",
"memory": "160926"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "typedef long long int ll;\n\nconst int MOD = 1e9+9;\nconst int P = 31;\n\n// Pattern = 's', Text = 't'\nvector<int> RabinKarp(const string& s, const string& t) {\n int S = s.size(), T = t.size();\n\n vector<ll> p_pow(max(S, T)); \n p_pow[0] = 1; \n for (int i = 1; i < p_pow.size(); i++) \n p_pow[i] = (p_pow[i-1] * P) % MOD;\n\n vector<ll> h(T + 1, 0); \n for (int i = 0; i < T; i++)\n h[i+1] = (h[i] + (t[i] - 'a' + 1) * p_pow[i]) % MOD; \n \n ll h_s = 0; \n for (int i = 0; i < S; i++) \n h_s = (h_s + (s[i] - 'a' + 1) * p_pow[i]) % MOD; \n\n vector<int> occurrences;\n for (int i = 0; i + S - 1 < T; i++) {\n ll cur_h = (h[i+S] + MOD - h[i]) % MOD;\n if (cur_h == h_s * p_pow[i] % MOD)\n occurrences.push_back(i);\n }\n return occurrences;\n}\n\nclass Solution {\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> a_indexes = RabinKarp (a, s);\n vector<int> b_indexes = RabinKarp (b, s);\n \n vector<int> result;\n \n for (int a_ind : a_indexes) {\n auto l = lower_bound(b_indexes.begin(), b_indexes.end(), a_ind-k) - b_indexes.begin();\n auto r = upper_bound(b_indexes.begin(), b_indexes.end(), a_ind+k) - b_indexes.begin() - 1;\n \n if ((r - l + 1) > 0) result.push_back(a_ind);\n }\n \n return result;\n }\n};",
"memory": "162428"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n using ll = long long;\n long long BASE = 101;\n long long mod = 1e9 + 7;\n \n vector<int> rabinKarp(const string& s, const string& p) {\n \n vector<int> ans;\n\n int n = s.size();\n int pn = p.size();\n \n if (pn > n)\n return ans;\n\n \n // pre-compute powers\n vector<long long> power(n);\n power[0] = 1LL;\n for (int i = 1; i < n; i++) \n power[i] = (power[i - 1] * BASE) % mod;\n \n // calculate the hash value for the string `p`\n long long phash = 0LL;\n for (int i = 0; i < pn; i++) \n phash = (phash + (p[i] - 'a') * power[i]) % mod;\n \n // calculate the prefix hash values for the string `s`\n vector<long long> shash(n + 1, 0);\n for (int i = 0; i < n; i++) \n shash[i + 1] = (shash[i] + (s[i] - 'a') * power[i]) % mod;\n \n // find the indices in `s` that contain string `p`\n for (int i = 0; (i + pn) <= n; i++) {\n long long curhash = (shash[i + pn] + mod - shash[i]) % mod;\n if (curhash == ((phash * power[i]) % mod)) \n ans.push_back(i);\n }\n return ans;\n }\n\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> is = rabinKarp(s, a);\n vector<int> js = rabinKarp(s, b);\n \n vector<int> result;\n for (auto& i : is) {\n auto it = lower_bound(js.begin(), js.end(), i - k);\n if ((it != js.end()) && (abs(*it - i) <= k)) result.push_back(i);\n }\n return result;\n }\n};\n",
"memory": "163931"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n using ll = long long;\n long long BASE = 101;\n long long mod = 1e9 + 7;\n \n vector<int> rabinKarp(const string& s, const string& p) {\n \n vector<int> ans;\n\n int n = s.size();\n int pn = p.size();\n \n if (pn > n)\n return ans;\n\n \n // pre-compute powers\n vector<long long> power(n);\n power[0] = 1LL;\n for (int i = 1; i < n; i++) \n power[i] = (power[i - 1] * BASE) % mod;\n \n // calculate the hash value for the string `p`\n long long phash = 0LL;\n for (int i = 0; i < pn; i++) \n phash = (phash + (p[i] - 'a') * power[i]) % mod;\n \n // calculate the prefix hash values for the string `s`\n vector<long long> shash(n + 1, 0);\n for (int i = 0; i < n; i++) \n shash[i + 1] = (shash[i] + (s[i] - 'a') * power[i]) % mod;\n \n // find the indices in `s` that contain string `p`\n for (int i = 0; (i + pn) <= n; i++) {\n long long curhash = (shash[i + pn] + mod - shash[i]) % mod;\n if (curhash == ((phash * power[i]) % mod)) \n ans.push_back(i);\n }\n return ans;\n }\n\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> is = rabinKarp(s, a);\n vector<int> js = rabinKarp(s, b);\n \n vector<int> beaut;\n \n int i = 0, j = 0;\n while (i < is.size() && j < js.size()) {\n if (abs(js[j] - is[i]) <= k) {\n beaut.push_back(is[i]); \n i++; \n } else if (is[i] < js[j]) {\n i++;\n } else {\n j++;\n }\n }\n\n return beaut;\n }\n};\n",
"memory": "163931"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst ll BASE = 31;\nconst ll MOD = 1e9+7;\n\nclass Solution {\npublic:\n vector<ll> computePowers(int n) {\n vector<ll> powers(n);\n powers[0] = 1;\n for(int i = 1; i < n; i++) {\n powers[i] = (powers[i - 1] * BASE) % MOD;\n }\n return powers;\n }\n\n vector<ll> computeHash(const string& s) {\n vector<ll> h(s.size());\n h[0] = s[0] - 'a' + 1; // Convert 'a' to 1, 'b' to 2, etc.\n for(int i = 1; i < s.size(); i++) {\n h[i] = (h[i-1] * BASE + (s[i] - 'a' + 1)) % MOD;\n }\n return h;\n }\n\n ll getHash(int i, int j, const vector<ll>& h, const vector<ll>& powers) {\n if (i == 0) return h[j];\n return (h[j] - (h[i - 1] * powers[j - i + 1] % MOD) + MOD) % MOD;\n }\n\n ll computeHashString(const string& a) {\n vector<ll> p = computePowers(a.size());\n vector<ll> h = computeHash(a);\n return getHash(0, a.size() - 1, h, p);\n }\n\n vector<int> findAll(const string& s, const string& a, const vector<ll>& h, const vector<ll>& p) {\n vector<int> result;\n int size = a.size();\n int n = s.size();\n ll hasha = computeHashString(a);\n for(int i = 0; i + size <= n; i++) { // Ensure index does not go out of bounds\n ll hashi = getHash(i, i + size - 1, h, p); // Adjusted to match substring length\n if(hashi == hasha) result.push_back(i);\n }\n return result;\n }\n\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<ll> h = computeHash(s);\n vector<ll> p = computePowers(s.size());\n vector<int> index_a = findAll(s, a, h, p);\n vector<int> index_b = findAll(s, b, h, p);\n int a_ptr = 0;\n int b_ptr = 0;\n vector<int> ans;\n while(a_ptr < index_a.size() && b_ptr < index_b.size()) {\n if(abs(index_a[a_ptr] - index_b[b_ptr]) <= k) {\n ans.push_back(index_a[a_ptr]);\n a_ptr++;\n } else if (index_a[a_ptr] < index_b[b_ptr]) {\n a_ptr++;\n } \n else {\n b_ptr++;\n }\n }\n return ans;\n }\n};",
"memory": "165433"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst ll BASE = 31;\nconst ll MOD = 1e9+7;\n\nclass Solution {\npublic:\n vector<ll> computePowers(int n) {\n vector<ll> powers(n);\n powers[0] = 1;\n for(int i = 1; i < n; i++) {\n powers[i] = (powers[i - 1] * BASE) % MOD;\n }\n return powers;\n }\n\n vector<ll> computeHash(const string& s) {\n vector<ll> h(s.size());\n h[0] = s[0] - 'a' + 1; // Convert 'a' to 1, 'b' to 2, etc.\n for(int i = 1; i < s.size(); i++) {\n h[i] = (h[i-1] * BASE + (s[i] - 'a' + 1)) % MOD;\n }\n return h;\n }\n\n ll getHash(int i, int j, const vector<ll>& h, const vector<ll>& powers) {\n if (i == 0) return h[j];\n return (h[j] - (h[i - 1] * powers[j - i + 1] % MOD) + MOD) % MOD;\n }\n\n ll computeHashString(const string& a) {\n vector<ll> p = computePowers(a.size());\n vector<ll> h = computeHash(a);\n return getHash(0, a.size() - 1, h, p);\n }\n\n vector<int> findAll(const string& s, const string& a, const vector<ll>& h, const vector<ll>& p) {\n vector<int> result;\n int size = a.size();\n int n = s.size();\n ll hasha = computeHashString(a);\n for(int i = 0; i + size <= n; i++) { // Ensure index does not go out of bounds\n ll hashi = getHash(i, i + size - 1, h, p); // Adjusted to match substring length\n if(hashi == hasha) result.push_back(i);\n }\n return result;\n }\n\n\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<ll> h = computeHash(s);\n vector<ll> p = computePowers(s.size());\n vector<int> index_a = findAll(s, a, h, p);\n vector<int> index_b = findAll(s, b, h, p);\n int a_ptr = 0;\n int b_ptr = 0;\n vector<int> ans;\n for(auto i : index_a) {\n cout << i << \", \";\n }\n cout << endl;\n for(auto i : index_b) {\n cout << i << \", \";\n }\n cout << endl;\n while(a_ptr < index_a.size() && b_ptr < index_b.size()) {\n if(abs(index_a[a_ptr] - index_b[b_ptr]) <= k) {\n ans.push_back(index_a[a_ptr]);\n a_ptr++;\n } else if (index_a[a_ptr] < index_b[b_ptr]) {\n a_ptr++;\n } \n else {\n b_ptr++;\n }\n }\n return ans;\n }\n};",
"memory": "166936"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int brr[500001]={0};\nvector<int> rabin_karp(string const& s, string const& t) \n{\n const int p = 31; \n const int m = 1e9 + 9;\n int S = s.size(), T = t.size();\n\n vector<long long> p_pow(max(S, T)); \n p_pow[0] = 1; \n for (int i = 1; i < (int)p_pow.size(); i++) \n p_pow[i] = (p_pow[i-1] * p) % m;\n\n vector<long long> h(T + 1, 0); \n for (int i = 0; i < T; i++)\n h[i+1] = (h[i] + (t[i] - 'a' + 1) * p_pow[i]) % m; \n long long h_s = 0; \n for (int i = 0; i < S; i++) \n h_s = (h_s + (s[i] - 'a' + 1) * p_pow[i]) % m; \n\n vector<int> occurrences;\n for (int i = 0; i + S - 1 < T; i++) {\n long long cur_h = (h[i+S] + m - h[i]) % m;\n if (cur_h == h_s * p_pow[i] % m)\n occurrences.push_back(i);\n }\n return occurrences;\n}\n \n vector<int> beautifulIndices(string s, string a, string b, int k) \n {\n int i=0, n=s.size(), an=a.size(), bn=b.size();\n vector<int> foundA=rabin_karp(a, s);\n // while(i<= n-an)\n // {\n // auto ind= s.find(a, i);\n // if(ind==string::npos)\n // break;\n // foundA.push_back(ind);\n // i=ind+1;\n // }\n i=0;\n vector<int> foundB= rabin_karp(b, s);\n // while(i<= n-bn)\n // {\n // auto ind= s.find(b, i);\n // if(ind==string::npos)\n // break;\n // foundB.push_back(ind);\n // i=ind+1;\n // }\n for(auto &x : foundB)\n brr[x]++;\n for(int i=1; i<=500000; i++)\n brr[i]+=brr[i-1];\n \n vector<int> ans;\n for(auto &ind : foundA)\n {\n int mx=min(ind+k, 500000);\n int cnt=0;\n if(ind-k<=0)\n cnt=brr[mx];\n else\n cnt=brr[mx]-brr[ind-k-1];\n if(cnt)\n ans.push_back(ind);\n }\n return ans;\n }\n};",
"memory": "166936"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\n/* Macros {{{ */\n/* A lot of this is from some of Benq's submissions\n [https://codeforces.com/profile/Benq]\n Ugly af to the eyes, but with vim fold its barable\n Hopefully c++20 concepts can make all this stuff must cleaner */\n\n/* Basics {{{ */\nusing ll = long long;\nusing ld = long double;\nusing str = string;\n\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\n#define mp make_pair\n#define fi first\n#define se second\n\n#define arr array\n#define ve vector\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vld = vector<ld>;\n\nusing vpi = vector<pi>;\nusing vpll = vector<pll>;\nusing vpld = vector<pld>;\n\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\nusing vvld = vector<vld>;\n\nusing vvpi = vector<vpi>;\nusing vvpll = vector<vpll>;\nusing vvpld = vector<vpld>;\n\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define sz size()\n#define rsz(a) resize(a)\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\n#define For(i, a, b) for (int i = a; i < b; ++i)\n#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)\n#define rep(a) For(_, 0, a)\n#define each(a, x) for (auto &a : x)\n#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)\n\ntemplate <typename T, typename U>\ninline void cmin(T &x, U y) {\n if (y < x) x = y;\n}\ntemplate <typename T, typename U>\ninline void cmax(T &x, U y) {\n if (x < y) x = y;\n}\n/*}}}*/\n\n/* IO {{{ */\n\n/* Template Macros {{{ */\n#define tcT template <class T\n#define tcTU tcT, class U\n#define tcTUU tcT, class... U\n/*}}}*/\n\ninline namespace Helpers { /*{{{*/\ntcT, class = void > struct is_iterable : false_type {};\ntcT > struct is_iterable<\n T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>\n : true_type {};\ntcT > constexpr bool is_iterable_v = is_iterable<T>::value;\n\ntcT, class = void > struct is_readable : false_type {};\ntcT > struct is_readable<T, typename std::enable_if_t<is_same_v<\n decltype(cin >> declval<T &>()), istream &>>>\n : true_type {};\ntcT > constexpr bool is_readable_v = is_readable<T>::value;\n\ntcT, class = void > struct is_printable : false_type {};\ntcT > struct is_printable<T, typename std::enable_if_t<is_same_v<\n decltype(cout << declval<T>()), ostream &>>>\n : true_type {};\ntcT > constexpr bool is_printable_v = is_printable<T>::value;\n} /* namespace Helpers */\n/*}}}*/\n\ninline namespace Input { /*{{{*/\ntcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;\ntcTUU > void re(T &t, U &...u);\ntcTU > void re(pair<T, U> &p); /* pairs */\n\n/* re: read{{{ */\ntcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {\n cin >> x;\n} /* default */\ntcT > typename enable_if<needs_input_v<T>, void>::type re(\n T &i); // vectors, arrays, etc...\ntcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs\ntcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {\n each(x, i) re(x);\n}\ntcTUU > void re(T &t, U &...u) {\n re(t);\n re(u...);\n} /* read multiple}}} */\n\n/* rv: resize and read vectors{{{ */\nvoid rv(size_t) {}\ntcTUU > void rv(size_t N, ve<T> &t, U &...u);\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u);\ntcTUU > void rv(size_t N, ve<T> &t, U &...u) {\n t.rsz(N);\n re(t);\n rv(N, u...);\n}\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u) {\n rv(N2, u...);\n} /*}}}*/\n\n/* dumb shortcuts to read in ints{{{ */\nvoid decrement() {} /* subtract one from each */\ntcTUU > void decrement(T &t, U &...u) {\n --t;\n decrement(u...);\n}\n#define ints(...) \\\n int __VA_ARGS__; \\\n re(__VA_ARGS__);\n#define int1(...) \\\n ints(__VA_ARGS__); \\\n decrement(__VA_ARGS__); /*}}}*/\n} /* namespace Input */\n/*}}}*/\n\ninline namespace ToString { /*{{{*/\ntcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;\n\n/* ts: string representation to print */\ntcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {\n stringstream ss;\n ss << fixed << setprecision(15) << v;\n return ss.str();\n} /* default */\ntcT > str bit_vec(T t) { /* bit vector to string */\n str res = \"{\";\n For(i, 0, t.sz) res += ts(t[i]);\n res += \"}\";\n return res;\n}\nstr ts(ve<bool> v) { return bit_vec(v); }\ntemplate <size_t SZ>\nstr ts(bitset<SZ> b) {\n return bit_vec(b);\n} /* bit vector */\ntcTU > str ts(pair<T, U> p); /* pairs */\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(\n T v); /* vectors, arrays */\ntcTU > str ts(pair<T, U> p) { return \"(\" + ts(p.fi) + \", \" + ts(p.se) + \")\"; }\ntcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {\n /* convert container to string w/ separator sep */\n bool fst = 1;\n str res = \"\";\n for (const auto &x : v) {\n if (!fst) res += sep;\n fst = 0;\n res += ts(x);\n }\n return res;\n}\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {\n return \"{\" + ts_sep(v, \", \") + \"}\";\n}\n\n/* for nested DS */\ntemplate <int, class T>\ntypename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n return {ts(v)};\n}\ntemplate <int lev, class T>\ntypename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n if (lev == 0 || !v.sz) return {ts(v)};\n ve<str> res;\n for (const auto &t : v) {\n if (res.sz) res.back() += \",\";\n ve<str> tmp = ts_lev<lev - 1>(t);\n res.insert(end(res), all(tmp));\n }\n For(i, 0, res.sz) {\n str bef = \" \";\n if (i == 0) bef = \"{\";\n res[i] = bef + res[i];\n }\n res.back() += \"}\";\n return res;\n}\n} /* namespace ToString */\n/*}}}*/\n\ninline namespace Output { /*{{{*/\ntemplate <class T>\nvoid pr_sep(ostream &os, str, const T &t) {\n os << ts(t);\n}\ntemplate <class T, class... U>\nvoid pr_sep(ostream &os, str sep, const T &t, const U &...u) {\n pr_sep(os, sep, t);\n os << sep;\n pr_sep(os, sep, u...);\n}\n/* print w/ no spaces */\ntemplate <class... T>\nvoid pr(const T &...t) {\n pr_sep(cout, \"\", t...);\n}\n/* print w/ spaces, end with newline */\nvoid ps() { cout << \"\\n\"; }\ntemplate <class... T>\nvoid ps(const T &...t) {\n pr_sep(cout, \" \", t...);\n ps();\n}\n/* debug to cerr */\ntemplate <class... T>\nvoid dbg_out(const T &...t) {\n pr_sep(cerr, \" | \", t...);\n cerr << endl;\n}\nvoid loc_info(int line, str names) {\n cerr << \"Line(\" << line << \") -> [\" << names << \"]: \";\n}\ntemplate <int lev, class T>\nvoid dbgl_out(const T &t) {\n cerr << \"\\n\\n\" << ts_sep(ts_lev<lev>(t), \"\\n\") << \"\\n\" << endl;\n}\n} /* namespace Output */\n/*}}}}}}}}}*/\n\nclass Solution {\nprivate:\n int const A = 51;\n int const Mod = 1e9+7;\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n // i is beautiful if a is a prefix of s[i:]\n // and for some j in the k ball centered at i\n // b is a prefix of s[j:]\n\n ll ahv=0, bhv=0;\n for(int i=a.sz-1; i>=0; --i) ahv = (ahv * A + (a[i]-'a'+1)) % Mod;\n for(int i=b.sz-1; i>=0; --i) bhv = (bhv * A + (b[i]-'a'+1)) % Mod;\n\n vll sfxhsh(s.sz+1, 0);\n transform(\n rall(s),\n sfxhsh.rbegin()+1,\n [&, pv=0ll](auto v) mutable { return (pv=(pv*A+(v-'a'+1))%Mod); }\n );\n\n vll apows(1, 1);\n rep(s.sz) apows.pb(apows.back()*A%Mod);\n\n auto hsh = [&](int l, int r) {\n return ((sfxhsh[l] - sfxhsh[r] * apows[r-l]) % Mod + Mod) % Mod;\n };\n\n // rolling window now\n vi res;\n map<int, int> bvs;\n for(int i=0; i<=k && i+b.sz<=s.sz; ++i) ++bvs[ hsh( i, i+b.sz ) ];\n for(int i=0; i+a.sz<=s.sz; ++i) {\n // remove the prev bad value if it exists\n int j = i-k-1;\n if(0 <= j && j+b.sz<=s.sz) {\n ll hv = hsh(j, j+b.sz);\n if(--bvs[hv] == 0) bvs.erase(hv);\n }\n\n if(hsh(i,i+a.sz) == ahv && bvs.count(bhv)) {\n res.pb(i);\n }\n\n // add the next value\n j = i+k+1;\n if(j+b.sz<=s.sz) {\n ll hv = hsh(j, j+b.sz);\n ++bvs[hv];\n }\n }\n\n return res;\n }\n};\n",
"memory": "168438"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n void fillps(string s, vector<int>& lps) {\n lps[0] = 0;\n int len = 0, i = 1;\n while (i < s.size()) {\n if (s[len] == s[i]) {\n len++;\n lps[i] = len;\n i++;\n } else {\n if (len == 0)\n i++;\n else\n len = lps[len - 1];\n }\n }\n }\npublic:\n vector<int> beautifulIndices(string s, string a, string b, int k) {\n string newa = a + \"#\" + s;\n vector<int> lps(newa.size(), 0);\n string newb = b + \"#\" + s;\n vector<int> lps2(newb.size(), 0);\n \n fillps(newa, lps);\n fillps(newb, lps2);\n \n int n = a.size(), m = b.size();\n vector<int> idx1, ans;\n set<int> idx2;\n\n for (int i = n + 1; i < lps.size(); i++) {\n if (lps[i] == n) {\n idx1.push_back(i - 2 * n); // Corrected index calculation\n }\n }\n\n for (int i = m + 1; i < lps2.size(); i++) {\n if (lps2[i] == m) {\n idx2.insert(i - 2 * m); // Corrected index calculation\n }\n }\n for (auto &x : idx1) {\n int val1 = x - k;\n int val2 = x + k;\n \n // Find the closest `b` index in the range [x - k, x + k]\n auto it = idx2.lower_bound(val1);\n if (it != idx2.end() && *it <= val2) {\n ans.push_back(x);\n }\n }\n return ans;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "169941"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void getPatternMatchingIndex(string& s, string& a, vector<int>& v){\n string t = a + \"@\" + s;\n vector<int> lps(1, 0);\n for(int i = 1; i < t.size(); ++i){\n int ind = lps[i-1]; \n while(ind > 0 && t[ind] != t[i]) { ind = lps[ind-1]; }\n lps.push_back((t[ind] == t[i])?ind + 1 : 0);\n }\n for(int i = 0; i < lps.size(); ++i){\n if(lps[i] == a.size()) v.push_back(i - 2*a.size());\n }\n}\n\nvector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> ans, v1, v2;\n getPatternMatchingIndex(s, a, v1);\n getPatternMatchingIndex(s, b, v2);\n for(int i = 0, j = 0; i < v1.size(); ++i){\n while(j < v2.size() && v1[i] > v2[j] && abs(v1[i] - v2[j]) > k) j++;\n if(j < v2.size() && abs(v1[i] - v2[j]) <= k) ans.push_back(v1[i]);\n }\n return ans;\n}\n};",
"memory": "171443"
} |
3,303 | <p>You are given a <strong>0-indexed</strong> string <code>s</code>, a string <code>a</code>, a string <code>b</code>, and an integer <code>k</code>.</p>
<p>An index <code>i</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>0 <= i <= s.length - a.length</code></li>
<li><code>s[i..(i + a.length - 1)] == a</code></li>
<li>There exists an index <code>j</code> such that:
<ul>
<li><code>0 <= j <= s.length - b.length</code></li>
<li><code>s[j..(j + b.length - 1)] == b</code></li>
<li><code>|j - i| <= k</code></li>
</ul>
</li>
</ul>
<p>Return <em>the array that contains beautiful indices in <strong>sorted order from smallest to largest</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15
<strong>Output:</strong> [16,33]
<strong>Explanation:</strong> There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15.
Thus we return [16,33] as the result.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcd", a = "a", b = "a", k = 4
<strong>Output:</strong> [0]
<strong>Explanation:</strong> There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4.
Thus we return [0] as the result.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code>, <code>a</code>, and <code>b</code> contain only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void getPatternMatchingIndex(string& s, string& a, vector<int>& v){\n string t = a + \"@\" + s;\n vector<int> lps(1, 0);\n for(int i = 1; i < t.size(); ++i){\n int ind = lps[i-1]; \n while(ind > 0 && t[ind] != t[i]) { ind = lps[ind-1]; }\n lps.push_back((t[ind] == t[i])?ind + 1 : 0);\n }\n for(int i = 0; i < lps.size(); ++i){\n if(lps[i] == a.size()) v.push_back(i - 2*a.size());\n }\n}\n\nvector<int> beautifulIndices(string s, string a, string b, int k) {\n vector<int> ans, v1, v2;\n getPatternMatchingIndex(s, a, v1);\n getPatternMatchingIndex(s, b, v2);\n for(int i = 0, j = 0; i < v1.size(); ++i){\n while(j < v2.size() && v1[i] > v2[j] && abs(v1[i] - v2[j]) > k) j++;\n if(j < v2.size() && abs(v1[i] - v2[j]) <= k) ans.push_back(v1[i]);\n }\n return ans;\n}\n}; ",
"memory": "171443"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int mini = 1e9;\n int ind1=0, ind2, ind3;\n for(int i=1; i<nums.size(); i++){\n for(int j=i+1; j<nums.size(); j++){\n int sum = nums[0] + nums[i] + nums[j];\n if(sum<mini){\n mini = sum;\n ind2 = i;\n ind3 = j;\n }\n }\n }\n cout<<ind1<<\" \"<<ind2<<\" \"<<ind3<<endl;\n // if(ind1 == 0){\n // return mini;\n // }\n // else{\n // return mini-nums[ind1]+nums[0];\n // }\n return mini;\n }\n};",
"memory": "29800"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int ans = nums[0];\n int smaller = 100;\n int small = 100;\n for(int i=1;i<nums.size();i++){\n if(nums[i]<small){\n smaller = small;\n small = nums[i];\n }\n else smaller = min(smaller,nums[i]);\n }\n return ans+smaller+small;\n }\n};",
"memory": "29900"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int n = nums.size();\n if (n < 3) return 0; // Not enough elements to split into 3 subarrays\n \n int min_cost = INT_MAX;\n \n \n for (int i = 0; i < n - 2; ++i) {\n for (int j = i + 1; j < n - 1; ++j) {\n \n int cost = nums[0] + nums[i + 1] + nums[j + 1];\n // Update the minimum cost found\n min_cost = min(min_cost, cost);\n }\n }\n \n return min_cost;\n }\n};",
"memory": "29900"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums)\n {\n int min1 = nums[0];\n int min2 = INT_MAX, min3 = INT_MAX;\n for(int i=1;i<nums.size();i++)\n {\n if(nums[i] <= min2)\n {\n min3 = min2;\n min2= nums[i];\n }\n else if(nums[i] <= min3)\n {\n min3 = nums[i];\n }\n }\n return min1 + min2 + min3;\n }\n};",
"memory": "30000"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int n = nums.size();\n int ans = nums[0]; // first elemnt will be taken as we need subarrays\n int min1 = INT_MAX;\n int min2 = INT_MAX;\n for(int i = 1; i < n; i++) {\n int curr = nums[i];\n if(curr < min1) {\n min2 = min1;\n min1 = curr;\n }\n else if(curr < min2) {\n min2 = curr;\n }\n }\n ans += min1 + min2;\n return ans;\n }\n};\n// Simple easy O(N) solution ",
"memory": "30100"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int first = INT_MAX;\n int second = INT_MAX;\n for (int i = 1; i < nums.size(); ++i) {\n if (nums[i] < first) {\n second = first;\n first = nums[i];\n } else if (nums[i] < second) {\n second = nums[i];\n }\n }\n return nums[0] + first + second;\n }\n};",
"memory": "30100"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int n = nums.size();\n if (n == 3) return nums[0] + nums[1] + nums[2];\n int res = INT_MAX;\n for (int i = 1; i < n - 1; ++i) {\n for (int j = i + 1; j < n; ++j) {\n res = min(res, nums[0] + nums[i] + nums[j]);\n }\n }\n return res;\n }\n};",
"memory": "30200"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int n = nums.front();\n sort(nums.begin(), nums.end());\n if(n > nums[2]){\n return nums[0] + nums[1] + n;\n }else{\n return nums[0] + nums[1] + nums[2];\n }\n }\n};",
"memory": "30200"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n sort(nums.begin()+1,nums.end());\n int count=nums[0]+nums[1]+nums[2];\n return count;\n }\n};",
"memory": "30300"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int min_cost = nums[0];\n\n nums.erase(nums.begin());\n sort(nums.begin(), nums.end());\n\n min_cost += nums[0];\n min_cost += nums[1];\n\n return min_cost;\n }\n};",
"memory": "30300"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n std::sort(nums.begin()+1, nums.end());\n return nums[0] + nums[1] + nums[2];\n }\n};",
"memory": "30400"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n int sum=0;\n vector<int> v(nums.size(),0);\n for(int i=1;i<nums.size();i++){\n v[i-1]=nums[i];\n }\n sort(v.begin(),v.end());\n sum=nums[0]+v[1]+v[2];\n return sum;\n }\n};",
"memory": "30500"
} |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need to divide <code>nums</code> into <code>3</code> <strong>disjoint contiguous </strong><span data-keyword="subarray-nonempty">subarrays</span>.</p>
<p>Return <em>the <strong>minimum</strong> possible <strong>sum</strong> of the cost of these subarrays</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,12]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,4,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,3,1,1]
<strong>Output:</strong> 12
<strong>Explanation:</strong> The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= n <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n vector<int>a;\n for(int i=1;i<nums.size();i++){\n a.push_back(nums[i]);\n }\n sort(a.begin(),a.end());\n return a[0]+a[1]+nums[0];\n }\n};",
"memory": "30600"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.