id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_set<int> s;\n int n1=nums1.size();\n int n2=nums2.size();\n for(int i=0;i<n1;i++){\n s.insert(nums1[i]);\n }\n int use1=s.size();\n s.clear();\...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n unordered_set<int> s1(nums1.begin(),nums1.end());\n unordered_set<int> s2(nums2.begin(),nums2.end());\n int cnt = 0,cn = 0;\n set<int> ans;\n for(a...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int >a,b,c;\n for(auto x:nums1)a.insert(x);\n for(auto x:nums2)b.insert(x);\n int x=max((int)0,(int)(a.size()-nums1.size()/2));\n x+=max((int)0,(int)(b.size()-nums2.size()/2)...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n set<int>s1,s2;\n for(int i=0;i<nums1.size();i++) s1.insert(nums1[i]);\n for(int j=0;j<nums2.size();j++) s2.insert(nums2[j]);\n int a=s1.size(),b=s2.size();\...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int setDiff(set<int> &a, set<int> &b) {\n int res = 0;\n for (auto e: a) {\n if (b.find(e) == b.end()) res++;\n }\n return res;\n }\n int un(set<int> &a, set<int> &b) {\n for (auto e: b) a.insert(e);\n return a.size()...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n unordered_set<int> s1, s2, common;\n\n for(auto i: nums1){\n s1.insert(i);\n }\n int cnt2=0;\n for(auto i: nums2){\n s2.insert(...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int>Nums2Element,Nums1Element;\n for(auto&v:nums2) Nums2Element.insert(v);\n int n=nums1.size()/2;\n int cnt=n,i;\n for(i=0;i<nums1.size();i++){\n if(Nums1Elemen...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n unordered_map<int,int>m;\n for(int i=0;i<n;i++){\n m[nums1[i]]++;\n }\n unordered_map<int,int>m1;\n for(int i=0;i<n;i++){\n m1[...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_set<int>s1, s2, res;\n for(auto num : nums1) {\n s1.insert(num);\n }\n for(auto num : nums2) {\n s2.insert(num);\n }\n int c = nums1.size()...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1;\n set<int> s2;\n for(auto i:nums1) s1.insert(i);\n for(auto i:nums2) s2.insert(i);\n int n = nums1.size() / 2;\n set<int> st;\n int j = n;\n int...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1;\n set<int> s2;\n for(auto i:nums1) s1.insert(i);\n for(auto i:nums2) s2.insert(i);\n int n = nums1.size() / 2;\n set<int> st;\n int j = n;\n int...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n \n set<int>s;\n for(auto it:nums1)\n {\n s.insert(it);\n }\n\n set<int>s2;\n set<int>common;\n for(auto it:nums2)\n {\n if(s.cou...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> st;\n unordered_set<int> st1,st2;\n for(auto it:nums1){\n st1.insert(it);\n }\n for(auto it:nums2){\n st2.insert(it);\n }\n int n=num...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int,int>c1,c2;\n int n=nums1.size();\n set<int>set,s1,s2;\n for(auto j:nums1){\n c1[j]++;\n }\n for(auto j:nums2){\n c2[j]++;\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_set<int> s1, s2, s;\n for(auto n : nums1) {\n s1.insert(n);\n s.insert(n);\n }\n for(auto n : nums2) {\n s2.insert(n);\n s.insert(n...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_set<int> s1, s2, u;\n\n int n = nums1.size() / 2; \n for(auto x:nums1){\n s1.insert(x);\n u.insert(x);\n }\n for(auto x:nums2){\n s2.i...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n size_t n = nums1.size();\n\n unordered_set<int> s1, s2, s;\n for (int num : nums1) {\n s1.insert(num);\n s.insert(num);\n }\n for (int num : nums2) {\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& A, vector<int>& B) {\n set<int>s1(A.begin(),A.end());\n set<int>s2(B.begin(),B.end());\n int a = 0;\n int n = A.size();\n int b = 0;\n a+=A.size() - s1.size();\n b+=B.size() - s2.size();\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
2
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& A, vector<int>& B) {\n set<int>s1(A.begin(),A.end());\n set<int>s2(B.begin(),B.end());\n int a = 0;\n int n = A.size();\n int b = 0;\n a+=A.size() - s1.size();\n b+=B.size() - s2.size();\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "#define pii pair<int, int>\nclass Solution {\npublic:\n int n;\n void removeEle(vector<pii>& nums, unordered_map<int, int>& f1, unordered_map<int, int>& f2) {\n int cnt = 0;\n for (auto& [fr, num] : nums) {\n if (fr > 1) {\n cnt += fr - 1;\n fr =...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n \n// int solve(unordered_map<int,int> u1, unordered_map<int,int> u2, int c1, int c2)\n// {\n// int s1,s2,s3;\n// s1=s2=s3=0;\n// int x=u1.begin().first;\n// if(c1>=u1[x])\n// {\n \n// }\n// if(c2>=u2[x...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int,int> f,s,visited;\n int ans=0;\n for(auto i:nums1)f[i]+=1;\n for(auto i:nums2)s[i]+=1;\n int r1=nums1.size()/2,r2=nums2.size()/2;\n for(int i=0;i<nums1.s...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& A, vector<int>& B) {\n unordered_set<int> st;\n for(auto it: A){\n st.insert(it);\n }\n for(auto it : B){\n st.insert(it);\n }\n int sz = st.size();\n int n = A.size();\n set<int> uA(A.begin(), A.end());...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int,int> b1,b2;\n int n=nums1.size();\n for (int i=0;i<n;i++){\n b1[nums1[i]]++;\n b2[nums2[i]]++;\n }\n unordered_set<int> c;\n deque<...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n unordered_map<int,int> b1,b2;\n int n=nums1.size();\n for (int i=0;i<n;i++){\n b1[n...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n unordered_map<int,int> b1,b2;\n int n=nums1.size();\n for (int i=0;i<n;i++){\n b1[n...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n set<int>s;\n\n for(int i=0;i<n;i++)\n {\n s.insert(nums1[i]);\n }\n int s1=s.size();\n s.clear();\n\n for(int i=0;i<n;i++)\n...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n set<int> st1, st2,st;\n for(auto i : nums1){\n st1.insert(i);\n st.insert(i);\n\n } \n for(auto i : nums2){\n st2.insert(i);\n st.insert(i); \...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1, s2, s;\n int n = nums1.size();\n for (auto num : nums1) {\n s1.insert(num);\n s.insert(num);\n }\n for (auto num : nums2) {\n s2.ins...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1, s2, s;\n int n = nums1.size();\n for (int x : nums1) {\n s1.insert(x);\n s.insert(x);\n }\n for (int x : nums2) {\n s2.insert(x);\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1, s2, u;\n int n = nums1.size() / 2;\n for(auto x:nums1){\n s1.insert(x);\n u.insert(x);\n }\n for(auto x:nums2){\n s2.insert(x);\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n set<int> s1, s2, s;\n int n = nums1.size();\n for (int x : nums1) {\n s1.insert(x);\n s.insert(x);\n }\n for (int x : nums2) {\n s2.insert(x);\n ...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_map<int,int>mp1,mp2,mp,mpp;\n for(int i=0;i<nums1.size();i++)mp1[nums1[i]]++;\n for(int i=0;i<nums2.size();i++)mp2[nums2[i]]++;\n vector<pair<int,int>>v1,v2;\n for(auto...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n\nint maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n map<int, int> mpp1;\n map<int, int> mpp2;\n int count1 = 0;\n int count2 = 0;\n\n // Count occurrences and duplicates in nums1\n for (int i = 0; i < n; i++) {\n if...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n unordered_set<int> res;\n unordered_map<int,int> f1;\n unordered_map<int,int> f2;\n\n for(auto i: nums1) f1[i]++;\n for(auto i: nums2) f2[i]++;\n\n int td2 = nums1.size()/...
3,228
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of even length <code>n</code>.</p> <p>You must remove <code>n / 2</code> elements from <code>nums1</code> and <code>n / 2</code> elements from <code>nums2</code>. After the removals, you insert the remaining elemen...
3
{ "code": "class Solution {\npublic:\n int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n map<int,int>mp1;\n map<int,int>mp2;\n int k1=nums1.size()/2;\n int k2=nums2.size()/2;\n for(auto x:nums1){\n mp1[x]++;\n }\n for(auto x:nums2){\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "// Time: O(n)\n// Space: O(n)\n\n// prefix sum, greedy\nclass Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n vector<int> left(size(s) + 1);\n vector<int> left_mask(size(s) + 1);\n for (int i = 0, cnt = 0, mask = 0; i < size(s); ++i) {\n mask ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n auto count = [](unsigned int bits) -> int { return std::popcount(bits); };\n if (k == 26) return 1;\n const int n = static_cast<int>(s.size());\n s = '@' + s + '@';\n std::vector<int> pre...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n if (k==26) return 1;\n int n = s.length();\n s = '$' + s + '$';\n vector<int> pre(n+2),suff(n+2);\n vector<int> pbit(n+2),sbit(n+2);\n int prefix = 0; int suffix = 0;\n int ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int count(int n){\n return __builtin_popcount(n);\n }\n int maxPartitionsAfterOperations(string s, int k) {\n if(k==26){\n return 1;\n }\n int n=s.size();\n s='@'+s+'@';\n vector<int> pref(n+2),pval(n+2);\n int...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n if (k == 26) return 1;\n \n int n = s.size();\n vector<int> dp(n + 1), pos_k_last(n);\n int cnt[26];\n memset(cnt, 0, sizeof(cnt));\n for (int i = n - 1, j = n, distinct = 0...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\nint dp[(int) 1e4 + 3];\nint desc[(int) 1e4 + 3];\nint psum[(int) 1e4 + 3][26];\n\nint BSit(int l, int r, int k, int sp, int h, int old){\n int i = l;\n while ((r-l) > 1){\n int m = (r+l)>>1;\n int cnt = 0;\n for (int j = 0; j < 26 && cnt <= k; ++j) {\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "typedef long long ll;\n\nnamespace internal{\n\t\tint ceil_pow2(int n) {\n\t\t\tint x = 0;\n\t\t\twhile ((1U << x) < (unsigned int)(n)) x++;\n\t\t\treturn x;\n\t\t}\n\t}\n\n\ttemplate <class S, S (*op)(S, S), S (*e)()> struct segtree {\n\t\tpublic:\n\t\t\tsegtree() : segtree(0) {}\n\t\t\texplicit segtree(i...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n vector<array<int, 4>> getPrefs(int p, const string &s, int k, char first) {\n vector<array<int, 4>> res;\n int mask = 0, cnt = 0, mid = -1, sz = s.size(), l = 0;\n for (int i = p; i < sz; ++i) {\n int bt = i == p ? first - 'a' : s[i] - 'a';\n if ((mask &...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\nprivate:\n int getCharMask(vector<vector<int>>& suffCount, int l, int r){\n int mask = 0;\n for(char c = 'a'; c <= 'z'; ++c){\n if(suffCount[c - 'a'][l] - suffCount[c - 'a'][r + 1] >= 1){\n mask |= (1 << (c - 'a'));\n }\n }\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\nprivate:\n int getCharMask(vector<vector<int>>& suffCount, int l, int r){\n int mask = 0;\n for(char c = 'a'; c <= 'z'; ++c){\n if(suffCount[c - 'a'][l] - suffCount[c - 'a'][r + 1] >= 1){\n mask |= (1 << (c - 'a'));\n }\n }\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\nvector<vector<int>> spr;\nvoid bldspr(int n,string s){\n for(int i=0;i<n;i++)spr[i][0]=(1<<(s[i]-'a'));\n for(int j = 1;(1<<j)<=n;j++)\n for(int i = 0;i+(1<<j)-1<n;i++)spr[i][j]=(spr[i][j-1])|(spr[i+(1<<(j-1))][j-1]);\n}\nint quespr(int l,int r){\n if(r<l)return 0...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\n\n void print(vector<int> &v){\n for(auto &c : v) cout<<c<<\" \";\n cout<<endl;\n }\n\n int calc(vector<vector<int>> &fre,int l,int r,int org,int rep) {\n int t = 0;\n for(int i = 0;i<26;i++) {\n int cnt = fre[r][i] - (l > 0 ? fre[l-1][i] : ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "#define set_bits __builtin_popcountll\nclass Solution {\npublic:\n int dp[10008][26][2];\n int func(int i,bool check,string &s,int k){\n int n=s.size();\n if(i>=n){\n return 0;\n }\n if(dp[i][s[i]-'a'][check]!=-1)\n return dp[i][s[i]-'a'][check];\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n int n = s.length();\n // ve[n][26] - 1st time to the right ; jth character kokhon asche\n vector<vector<int>>ve(n,vector<int>(26,-1));\n ve[n-1][s[n-1]-97]=n-1;\n for(int i=n-2;i>=0;i--){...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int dp[100001][26][2];\n string s;\n int k,n;\n int solve(int i, int flag){\n if(i>=n) return 0;\n if(dp[i][s[i]-'a'][flag]!=-1) return dp[i][s[i]-'a'][flag];\n\n set<char>st;\n int j=i,chk=0;\n while(j<n && (st.count(s[j]) || st.si...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n void Update(unordered_map<int,int> &m,int key,int val)\n {\n auto it=m.find(key);\n if(it==m.end()) m[key]=val;\n else it->second=max(it->second,val);\n }\n int maxPartitionsAfterOperations(string s, int k) {\n int n=s.size();\n uno...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int off = 0;\n int f(int i, int state, string &s, int k, map<pair<int,int>, int> &dp) {\n if(i == s.size()) return 1;\n if(dp.count({i, state}) != 0) return dp[{i, state}];\n int cur_k = 0;\n int chr = s[i] - 'a';\n for(int j = 0;j<26; j+...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n\n vector<vector<map<int,int>>>dp;\n\n int Calc(int i, int mask, int j, int cnt, int k, int n, string &s) {\n // cout<<i<<\" \"<<j<<\" \"<<mask<<\" \"<<cnt<<endl;\n if(i==n) return 0;\n if(dp[i][j].find(mask)!=dp[i][j].end()) {\n if(dp[i][j][...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n\n vector<vector<map<int,int>>>dp;\n\n int Calc(int i, int mask, int j, int cnt, int k, int n, string &s) {\n // cout<<i<<\" \"<<j<<\" \"<<mask<<\" \"<<cnt<<endl;\n if(i==n) return 0;\n if(dp[i][j].find(mask)!=dp[i][j].end()) {\n if(dp[i][j][...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "// Idea: DP or Greedy. since we can change at most one char,\n// there're two states w.r.t. this aspect, while we scan s at [i]:\n// * changed a char before [i]\n// * haven't changed any char before [i].\n// other aspects of the states:\n// the distinct count of chars after the last partition\n//\n// Speci...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "// Idea: DP or Greedy. since we can change at most one char,\n// there're two states w.r.t. this aspect, while we scan s at [i]:\n// * changed a char before [i]\n// * haven't changed any char before [i].\n// other aspects of the states:\n// the distinct count of chars after the last partition\n//\n// Speci...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n if (k == 26) {\n return 1;\n }\n return Findpartition(0, 0, s, true, k) + 1; \n }\nprivate:\n unordered_map<long long, int> _memo;\n long long Findpartition(long long mask, i...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "int numdivs(string s, int start, unordered_set<char> &st, int k, vector<unordered_map<char,int>> &v, vector<unordered_set<char>> &v1){\n int ans = 0;\n for(int i=0;i<26;i++){\n if(st.find('a'+i)==st.end()){\n unordered_map<char,int> t = v[start];\n unordered_set<char> t1 ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n int n = s.length();\n\n auto countOne = [&] (int mask) -> int {\n int cntOne = 0;\n for(int j = 0 ; j < 26 ; j++) {\n if((mask & (1<<j)) != 0) {\n cntOn...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "typedef long long ll;\n\nclass Solution {\npublic:\n unordered_map<ll, int> dp;\n int k;\n string s;\n\n ll getKey(ll ind, ll mask, ll canChange) {\n return (ind << 27) | (mask << 1) | canChange;\n }\n\n int solve(int ind, ll mask, bool canChange) {\n int n = s.size();\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\n vector<vector<unordered_map<int,int>>>dp;\n int solve(string &s,int idx,int mask,int left,int k){ \n if(left<0) return -1e5;\n if(idx>=dp.size()) return bool(mask);\n if(dp[idx][left].find(mask)!=dp[idx][left].end()) return dp[idx][left][mask];\n int &an...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n map<long long,int>mp;\n int f(int i,int unique,bool change,string &s, int k){\n \n if(i==s.size())return 1;\n long long num=unique;\n if(change){\n num|=(1<<27);\n }\n else{\n num|=(0<<27);\n }\n long long p=i;\n int j=28;\n whil...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n map<long long,int> mp;\n long long solve(long long idx,long long uc,int can_change,int k,string& s)\n {\n\n long long key = (idx << 27) | (uc << 1) | can_change;\n\n\n if(mp.find(key) != mp.end())\n {\n return mp[key];\n }\n\n\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int dfs(int i, int m, int c, string& s, int k, map<long long, int>& memo) {\n if (i == s.size()) return 1; \n long long key = (long long) i<<27 | m<<1 | c; \n if (!memo.contains(key)) {\n int v = s[i] - 'a', mm = m | 1<<v; \n int ans...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution {\npublic:\n int dfs(int i, int m, int c, string& s, int k, map<long long, int>& memo) {\n if (i == s.size()) return 1; \n long long key = (long long) i<<27 | m<<1 | c; \n if (!memo.contains(key)) {\n int ans = 0, v = s[i] - 'a', mm = m | 1<<v; \n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
0
{ "code": "class Solution\n{\npublic:\n#define ll long long\n int rec(ll lvl, ll mask, ll canchange, string &s, int k, map<ll, int> &mp)\n {\n if (lvl >= s.size())\n {\n return 0;\n }\n ll key = (lvl << 27 | mask << 1 | canchange); // storing the state of dp in a single in...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n string S;\n int K;\n map<long long,int>mp;\n int solve(long long i,bool c,int unique) {\n if(i>=S.size()) return 1;\n long long key = i<<27 | unique<<1 | c;\n if(mp[key]) return mp[key];\n int newUnique = unique | 1<<S[i]-'a';\n int...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n map<long long, int> dp;\n int dfs(string& s, int& k, long long idx, int canChange, long long mask, vector<int>& shift){\n if(idx==s.size()){\n return 0;\n }\n\n long long key = (idx<<27) | (mask<<1) | (canChange);\n if(dp.count(key)){...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "const int NOT_ALLOW = 0, ALLOW = 1;\nclass Solution {\npublic:\n int dfs(int i, string &s, int curr_status, int allow_change, vector<vector<unordered_map<int, int>>>& memo, int k){\n if (i >= s.size()) \n return curr_status > 0;\n int c = s[i] - 'a';\n if (memo[i][allow_c...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n int dfs(int i, string &s, int curr_status, int allow_change, vector<vector<unordered_map<int, int>>>& memo, int k){\n if (i >= s.size()) \n return curr_status > 0;\n int c = s[i] - 'a';\n if (memo[i][allow_change].find(curr_status) != memo[i][a...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> mp;\n\nint solve(long long i, int uniquechar, bool canChange, string& s, int k) {\n // Create a unique key for memoization using bitwise operations\n long long key = (i << 27 | uniquechar << 1 | canChange);\n //here we are having 0th bit...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string s, int k) {\n if (k == 26) {\n return 1;\n }\n return Findpartition(0, 0, s, true, k) + 1; \n }\nprivate:\n unordered_map<long long, int> _memo;\n long long Findpartition(long long mask, i...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "#define ll long long\n \nclass Solution {\npublic:\n unordered_map< ll , int >mp; // memoization using map\n\n int solve(string &s, int n, ll i, ll changed, ll vis, int k) {\n if (i == n) return 1;\n ll key= (i<<27) | (vis<<1 ) | changed;\n if( mp.find(key)!= mp.end() ) return ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\nprivate:\n int computeMaxPartitions(int i, int mask, bool canChange, string& s, int k, unordered_map<long long, int>& memo){\n const int N = s.length();\n\n if(i == N){\n return 1;\n }\n\n long long state = i | ((long long)canChange << 15) | ((lon...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> dp;\n string s;\n int k;\n\n int maxPartitionsAfterOperations(string s, int k) {\n this->s = s;\n this->k = k;\n return solve(0, 0, true) + 1;\n }\n\nprivate:\n int solve(long long i, long long currentSet, bool...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\n public:\n int maxPartitionsAfterOperations(string s, int k) {\n unordered_map<long, int> mem;\n return maxPartitionsAfterOperations(s, 0, true, 0, k, mem) + 1;\n }\n\n private:\n // Returns the maximum number of partitions of s[i..n), where `canChange` is\n // true if we can st...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\n public:\n int maxPartitionsAfterOperations(string s, int k) {\n unordered_map<long, int> mem;\n return maxPartitionsAfterOperations(s, 0, true, 0, k, mem) + 1;\n }\n\n private:\n // Returns the maximum number of partitions of s[i..n), where `canChange` is\n // true if we can st...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> mp;\n string S;\n int K;\n \n int solve(long long i, long long uniqueChars, bool canChange) {\n long long key = (i << 27) | (uniqueChars << 1) | (canChange);\n /*\n i << 27: Left-shifting i by 27 bits. This i...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\nunordered_map<long long ,int> mp;\nint solve(long long i, long long unique, bool can, string &s, int k)\n{\n if(i>=s.size())\n return 0;\n long long key = (i << 27) | (unique << 1) | (can);\n if(mp.count(key))\n {\n return mp[key];\n }\n int newCha...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
1
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> cache;\n string s;\n int k;\n\n int maxPartitionsAfterOperations(string s, int k) {\n this->s = s;\n this->k = k;\n return dp(0, 0, true) + 1;\n }\n\nprivate:\n int dp(long long index, long long currentSet, boo...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
2
{ "code": "class Solution {\npublic:\n string S;\n int K;\n unordered_map<long long , int> dp;\n int solve(long long unique_chars, long long i, bool canChange){\n \n long long key = (i << 27) | (unique_chars << 1) | (canChange);\n if(dp.count(key)){\n return dp[key];\n }...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
2
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> cache;\n string s;\n int k;\n\n int maxPartitionsAfterOperations(string s, int k) {\n this->s = s;\n this->k = k;\n return dp(0, 0, true) + 1;\n }\n\nprivate:\n int dp(long long index, long long currentSet, boo...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n unordered_map<ll, int> mp;\n\n int solve(string &s, ll i, ll mask, ll flag, ll k) {\n if(i >= s.length()) return 1;\n\n ll x = (i<<27) | (mask<<1) | flag;\n if(mp.find(x) != mp.end()) return mp[x];\n\n int charIndex =...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> mp;\n string S;\n int K;\n \n int solve(long long i, long long uniqueChars, bool canChange) {\n long long key = (i << 27) | (uniqueChars << 1) | (canChange);\n\n if (mp.count(key)) { \n return mp[key];\n ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\npublic:\n unordered_map<long long, int> mp;\n\n long long solve(long long ind, long long unique_char, bool canchange, int n, string& s, int k) {\n long long key = (ind << 27) | (unique_char << 1) | canchange;\n \n // Check memoization\n if (mp.find(key) !...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\npublic:\n //MEMOIZATION\n unordered_map<long long, int> dp;\n /*\n i: index used for traversing\n option: It denotes whether we changed a character or not\n mask: It basically acts as a 26 length long integer where ith bit represents whether we encountered ith character ...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\npublic:\n\n using item = pair<pair<int, int>, pair<bool, int>>;\n\n inline int start(item& i) {return i.first.first;}\n inline int tot(item& i) {return i.first.second;}\n inline bool chance(item& i) {return i.second.first;}\n inline int hash(item& i) {return i.second.second...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\n map<pair<int, pair<int, long long> >, int> dp;\n int solve(int i, bool f, string& s, int k, long long mask, int n){\n if(i == n) return 1;\n if(dp.find({i, {f, mask}}) != dp.end()) return dp[{i, {f, mask}}];\n\n int ans = 0;\n\n if(__builtin_popcount(mas...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "\nclass Solution {\n unordered_map<long long ,int >dp;\n int solve(long long i,int cnt,bool change,int mask,string &s, int k){\n int n = s.size();\n if(i>=n)return 1;\n // if(dp[i][change][cnt]!=-1)return dp[i][change][cnt];\n long long key = ((i<<27)|(mask<<1)|(change));\n i...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "// for every index either do no change and calculate as per the conditions\n// or try out all the a->z replacement for it \nclass Solution {\n unordered_map<long long ,int >dp;\n int solve(long long i,int cnt,bool change,int mask,string &s, int k){\n int n = s.size();\n if(i>=n)return 1;\n...
3,233
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>First, you are allowed to change <strong>at most</strong> <strong>one</strong> index in <code>s</code> to another lowercase English letter.</p> <p>After that, do the following partitioning operation until <code>s</code> is <strong>empty</s...
3
{ "code": "class Solution {\npublic:\n int maxPartitionsAfterOperations(string S, int K) {\n int N = S.size();\n vector<int> A(N);\n for (int i = 0; i < N; ++i) {\n A[i] = 1 << (S[i] - 'a');\n }\n\n unordered_map<int, unordered_map<int, unordered_map<int, int>>> memo;\...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n int maxPossibleScore(vector<int>& start, int d) {\n long long a[500005], n;\n for(int i = 0; i < start.size(); i++){\n a[i + 1] = start[i];\n }\n n = start.size();\n sort(a + 1, a + n + 1);\n long long l, r;\n r = a[...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n long long maxPossibleScore(vector<int>& s, int d) {\n shuffleAndProcess(s); \n sort(s.begin(), s.end()); \n long long low = 0, high = (static_cast<long long>(s.back()) + d) - s.front();\n long long result = 0;\n \n while (low <= high)...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n int maxPossibleScore(vector<int>& start, int d) {\n vector<int> A = start;\n int n = A.size();\n sort(A.begin(), A.end());\n int low = 0, high = A.back() - A[0] + d;\n int ans = 0;\n while(low<=high) {\n // cout<<low<<\" \"...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n int maxPossibleScore(vector<int>& start, int d) {\n int n = (int)start.size();\n vector<int> a = start;\n sort(a.begin(), a.end());\n\n long long l = 0, r = 2e9;\n while (l < r) {\n long long m = (l + r + 1) / 2;\n\n lo...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "// Approach 1 - Optimization\n// Since all number's are +Ve, MinScore = 0;\n// #PATTERN - Min(Max) or Max(Min) - Binary Search\n\n// T.C - O(N log N + N log N)\n// S.C - O(N)\n\nclass Solution {\nprivate:\n bool isMinScorePossible(vector<int> &start, vector<int> &end, int score)\n {\n long lon...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int maxPossibleScore(vector<int>& start, int d) {\n int size = start.size();\n\n vector<int> sortedStart = start;\n sort(sortedStart.begin(), sortedStart.end());\n\n ll left = 0;\n ll right = d + sortedStart[size-1] + 2...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n // Check if we can pick numbers from intervals while maintaining at least 'minGap' between them\n bool canChooseWithMinDiff(const vector<long long>& intervals, long long intervalSpan, long long minGap) {\n long long lastPicked = intervals[0]; // Start with the left...
3,485
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
3
{ "code": "class Solution {\npublic:\n \n bool canChooseWithMinDiff(const vector<long long>& intervals, long long intervalSpan, long long minGap) {\n long long lastPicked = intervals[0]; \n for (int i = 1; i < intervals.size(); ++i) {\n long long nextPick = max(intervals[i], lastPicked...