id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
2
{ "code": "class Solution {\npublic:\n /* int numberOfSubarrays(vector<int>& nums, int k) {\n int answer=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n int count=0;\n for(int j=i;j<n;j++){\n if(nums[j]%2==1){\n if(count>k){\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
2
{ "code": "class Solution {\npublic:\n int smallerThanOrEqual(vector<int> nums, int k) {\n\n if ( k==-1 )\n return 0;\n int n = nums.size(), left=0, right=0, cnt=0, odd=0;\n\n while ( right<n )\n {\n if ( nums[right] % 2 == 1 )\n odd++;\n\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
2
{ "code": "class Solution {\npublic:\nint nsub(vector<int>nums,int k){\n int l=0,r=0,n=nums.size(),count=0,cntsub=0;\n while(r<n){\n if(nums[r]%2) count++;\n while(count>k){\n if(nums[l]%2!=0) count--;\n l++;\n }\n cntsub+=(r-l+1);\n r++;\n }\n retu...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "#include <vector>\n#include <iostream>\n\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n std::vector <int>odd_index;\n for(int i=0;i<nums.size();i++){\n if(nums[i]&1){odd_index.push_back(i);}\n }\n \n if(odd_index.size()<k){r...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "#include <vector>\n#include <iostream>\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n std::vector <int>...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int ctOfSub(vector<int>nums,int k){\n int n=nums.size();\n int l=0,r=0,ct=0;\n unordered_map<int,int>mp;\n while(r<n){\n mp[nums[r]%2]+=1;\n while(mp[1]>k){\n mp[nums[l]%2]--;\n l++;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int findNumOfSubArr(vector<int>& nums, int goal) {\n int left = 0, right = 0, sum = 0, res = 0;\n while (right < nums.size()) {\n sum += nums[right];\n while (sum > goal) {\n sum -= nums[left];\n left++;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int lessThanEqualToK(vector<int> &nums, int goal){\n if(goal<0) return 0;\n int l=0,r=0,cnt=0,sum=0,n=nums.size();\n while(r<n){\n sum+=nums[r];\n while(sum>goal){\n sum-= nums[l];\n l++;\n }\...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "long long lessthanequal(const vector<int>& v, int k) {\n int n = v.size();\n long long r = 0, l = 0, ct = 0, sum = 0;\n\n while (r < n) {\n sum += v[r];\n while (sum > k && l <= r) {\n sum -= v[l];\n l++;\n }\n ct += (r - l + 1);\n r++;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int>vis;\n for(int i=0;i<nums.size();i++){\n if (nums[i]%2==0){\n vis.push_back(0);\n\n }\n else vis.push_back(1);\n }\n int l=0;\n in...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& nums, int k){\n if(k<0) return 0;\n int n=nums.size();\n int i=0;\n int j=0;\n int sum=0,count=0;\n while(j<n){\n sum+=nums[j];\n if(sum>k){\n while(sum>k){\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> from_l = {};\n vector<int> from_r = {};\n int idx = -1;\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] % 2 == 1) {\n from_l.push_back(i - idx);\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> map(50000, 0);\n int nice = 0, count = 0;\n for(int i = 0; i < nums.size(); i++){\n if(nums[i] % 2 != 0) nice++;\n if(nice == k) count++;\n if(nice - k >= 0 &...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>v,int k)\n {\n if(k<0) return 0;\n int r=0,l=0,sum=0,c=0,n=v.size();\n while(r<n)\n {\n sum+=v[r];\n while(sum>k)\n {\n sum-=v[l];\n l++;\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\nprivate:\n int count(vector<int> nums,int k) {\n int n=nums.size();\n int l=0,r=0;\n int sum=0,cnt=0;\n while(r<n) {\n sum += nums[r];\n while(sum>k) {\n sum-=nums[l];\n l++;\n }\n if(...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int f(vector<int> v, int k) {\n if(k==-1) return 0;\n int n=v.size();\n int l=0,r=0;\n int cnt=0, sum=0;\n while(r<n) {\n sum+=v[r];\n while(sum>k) {\n sum-=v[l];\n l++;\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int count(vector<int> nums, int k){\n int odd = 0;\n int l = 0, r = 0;\n int cnt = 0;\n\n if (k < 0) return 0;\n\n while (r < nums.size()){\n odd += nums[r];\n\n while (odd > k ){\n odd -= nums[l];\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> nums,int k){\n int l=0,r=0,cnt=0,sum=0;\n while(r<nums.size()){\n sum+=nums[r];\n while(sum>k){\n sum-=nums[l++];\n }\n cnt+=(r-l+1);\n r++;\n }\n return cn...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "\n\nclass Solution {\npublic:\n int numberOfSubarrays(vector<int>& arr, int k) {\n int n = arr.size();\n\n // Convert array elements to 0 if even, 1 if odd\n for(int i = 0; i < n; i++) {\n arr[i] = arr[i] % 2 == 0 ? 0 : 1;\n }\n\n // Initialize prefix sum ar...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int Result=0;\n int n=nums.size();\n int DP[n];\n for(int i=0;i<n;i++)\n {\n if(i==0)\n DP[i]=nums[i]%2;\n else DP[i]=DP[i-1]+nums[i]%2;\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\n\nclass Solution {\npublic:\n int noOfSubArr(vector<int>arr,int target){\n if(target<0)return 0;\n\n int sum=0;\n int i=0...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int func(vector<int> nums,int goal)\n {\n if(goal<0) return 0;\n int i = 0;\n int j = 0;\n int sum = 0;\n int count = 0;\n \n while(j<nums.size())\n {\n sum+=nums[j];\n while(sum>goal)\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n\n int count(vector<int> nums,int goal){\n if(goal<0){\n return 0;\n }\n int l=0;\n int r=0;\n int sum=0;\n int c=0;\n while(r<nums.size()){\n sum+=nums[r];\n while(sum>goal){\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "\n class Solution {\npublic:\nvector<int> transfer(vector<int>& nums1){\n vector<int > nums;\n int n=nums1.size();\n for(int i=0;i<n;i++){\n if(nums1[i]%2==0){\n nums.push_back(0);\n }\n else{\n nums.push_back(1);\n }\n }\n return nums;\n...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "\n class Solution {\npublic:\nvector<int> transfer(vector<int>& nums1){\n vector<int > nums;\n int n=nums1.size();\n for(int i=0;i<n;i++){\n if(nums1[i]%2==0){\n nums.push_back(0);\n }\n else{\n nums.push_back(1);\n }\n }\n return nums;\n...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n for (int i = 0; i < nums.size(); ++i) {\n nums[i] = (nums[i] % 2);\n }\n\n vector<int> map(100001);\n map[0] = 1;\n\n int accum = 0;\n int result = 0;\n\n for (int ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> map(100001);\n map[0] = 1;\n\n int accum = 0;\n int result = 0;\n\n for (int i = 0; i < nums.size(); ++i) {\n accum += (nums[i] % 2);\n\n if (accum - k >= ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int r=0,l=0,n=nums.size();\n unordered_map<int,int>x;\n int ans=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]%2==1){\n x[nums[i]]++;\n }\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int i=0;\n int j=0;\n\n unordered_map<int,int>mp;\n\n for(int i=0; i<nums.size(); i++)\n {\n if(nums[i]%2!=0)\n mp[nums[i]]++;\n }\n\n int cnt=0;\n int ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n for(int i = 0; i < nums.size(); i++) {\n nums[i] %= 2;\n }\n //return helper(nums,k)-helper(nums,k-1);\n unordered_map<int,int> prefixCount(nums.size() + 1);\n prefixCount[0] = 1...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n m[0]=1;\n int odd=0;\n int ans=0;\n for(auto x:nums){\n odd+=(x%2);\n if(m.count(odd-k)){\n ans+=m[odd-k];\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n mp[0]=1;\n int sum=0;\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i]%2;\n if(mp.find(sum-k)!=mp.end()){\n cnt+...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n // Map to store the count of prefix sums\n unordered_map<int, int> prefixCount;\n // Initialize with 0 prefix sum having one count\n prefixCount[0] = 1;\n \n int count = 0; // To sto...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n unordered_map<int, int> prefixCount;\n int count = 0;\n int prefixSum = 0;\n \n \n prefixCount[0] = 1;\n \n for (int num : nums) {\n prefixSum += num % 2;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n unordered_map<int, int> prefixSums;\n int count = 0;\n int curSum = 0;\n prefixSums[0] = 1;\n for(int i = 0; i < nums.size(); i++){\n curSum += nums[i] % 2;\n if(prefi...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "// https://leetcode.com/problems/count-number-of-nice-subarrays/description/\n// https://www.youtube.com/watch?v=JpXlsCAD1kg\n#include <bits/stdc++.h>\nusing namespace std;\n\n// Approach 1 : Brute force O(n^2)\n\n// Approach 2 : Using hash map\n// TC : O(n)\n// SC : O(n)\nclass Solution1\n{\npublic:\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]%2==0)\n nums[i]=0;\n else\n nums[i]=1;\n }\n int count=0;long long sum=0;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> v=nums;\n int n=v.size();\n int ans=0,sum=0;\n map<int,int> m;\n for(int i=0;i<n;i++){\n if(v[i]%2!=0){\n sum++;\n }\n if(sum==k)...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int>newarr(nums.size());\n for(int i = 0 ; i< nums.size();i++){\n if(nums[i]%2==0)\n newarr[i] = 0;\n else\n newarr[i] =1;\n }\n\n int l = 0 , r=...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\r\npublic:\r\n int numberOfSubarrays(vector<int>& nums, int k) {\r\n vector<int> ans(nums.size());\r\n for (int i = 0; i < nums.size(); i++) {\r\n ans[i] = (nums[i] % 2 != 0) ? 1 : 0;\r\n }\r\n int goal=k;\r\n map<int, int> mp;\r\n mp...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int l=0,r=0,cnt=0;\n vector<int>v;\n for(auto i:nums){\n v.push_back(i%2);\n }\n long long sum=0;\n unordered_map<int,int>mp;\n while(r<v.size()){\n sum+...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> oddEvenArr;\n for (int i = 0; i < nums.size(); i++) {\n if(nums[i] & 1) oddEvenArr.push_back(1);\n else oddEvenArr.push_back(0);\n }\n int curSum = 0, size = nums...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int> vec(nums.size());\n for(int i=0;i<nums.size();i++){\n if(nums[i]%2==1){\n vec[i] = 1;\n }\n else{\n vec[i] = 0;\n }\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>odd(n,0),even(n,0),suff(n,0);\n if(nums[0]%2!=0){\n odd[0]=1;\n }\n else if(nums[0]%2==0){\n even[0]=1;\n }\n \n for(int i=1;i<n;i...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n vector<int>a;\n for(int i=0;i<nums.size();i++){\n if(nums[i] & 1) a.push_back(1);\n else a.push_back(0);\n }\n int sm=0;\n int ans=0;\n map<int,int>mp;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n for(int i=0; i<n; i++)\n {\n nums[i] = nums[i]%2;\n }\n int res = 0;\n int start = 0;\n int curr = 0;\n vector<vector<int>> ind;\n ...
1,370
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called <strong>nice</strong> if there are <code>k</code> odd numbers on it.</p> <p>Return <em>the number of <strong>nice</strong> sub-arrays</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
3
{ "code": "class Solution {\npublic:\n int numberOfSubarrays(vector<int>& num, int k) {\n\n\n int count=0;\n int sum=0;\n int n=num.size();\n map<int,int>mp;\n mp[0]=1;\n\n vector<int>nums;\n for(int i=0;i<n;i++){\n if(num[i]%2==0){\n nums...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n const int n = (int)s.length();\n int balance = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == '(')\n balance++;\n else if (s[i] == ')')\n if (balance) balance-...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int depth = 0;\n int p = 0;\n for (char c : s) {\n if (c == '(') {\n ++depth;\n s[p++] = c;\n } else if (c == ')') {\n if (depth > 0) {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n std::string minRemoveToMakeValid(std::string s)\n {\n int open = 0;\n int position = 0;\n\n std::string str(s.size(), '#');\n\n for (char character : s)\n {\n if (character == '(')\n open++;\n else if ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n vector<int> left_pos;\n left_pos.reserve(s.size());\n \n // Traverse string and remove extra \")\". \n int i = 0;\n while (i < s.size()) {\n const auto& c = s[i];\n if (c...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int> st; // indexes of unbalanced opening paranthesis\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '(')\n st.push(i);\n else if (s[i] == ')' && !st.empty())\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n //iterate through the string and mark all those positions as '*' which are not valid\n //at the end remove all the '*' from the string\n\n string minRemoveToMakeValid(string s) {\n stack<int> st;\n for (auto i = 0; i < s.size(); ++i) {\n if (s[i...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n std::string minRemoveToMakeValid(std::string s)\n {\n int open = 0;\n int position = 0;\n\n std::string str(s.size(), '#');\n\n for (char character : s)\n {\n if (character == '(')\n open++;\n else if ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n //iterate through the string and mark all those positions as '*' which are not valid\n //at the end remove all the '*' from the string\n\n string minRemoveToMakeValid(string s) {\n stack<int> st;\n for (auto i = 0; i < s.size(); ++i) {\n if (s[i...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\nstd::string minRemoveToMakeValid(std::string s) \n{\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n std::stack<int> openParentheses; // Stack to store indices of '(' characters\n int size = s.size();\n\n // First pass: Remove invalid ')'\n for(int i = ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\n string make_valid(string s, char open, char close) {\n auto write = s.begin();\n auto read = s.begin();\n int closure = 0;\n\n while (read != s.end()) {\n if (isalpha(*read)) {\n *write++ = *read++;\n continue;\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int> st;\n\n\n for(int i = 0; i < s.length(); i++){\n\n if(s[i] == '('){\n st.push(i);\n }\n else if(s[i] == ')'){\n if (!st.empty() && s[st.top()] ==...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n = s.length();\n stack<int> st;\n\n for(int i=0;i<n;i++){\n if(s[i]=='('){\n st.push(i);\n }else if(s[i]==')'){\n if(!st.empty() && s[st.top()]=='('){\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int openCount = 0;\n int closeCount = 0;\n int n=s.size();\n string str=\"\";\n for(int i=0;i<s.size();++i)\n {\n if(s[i]=='(')\n openCount++;\n if(s[i]=='...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n=s.length();\n string res=\"\";\n int cnt=0;\n\n for(int i=0 ; i<s.length(); i++){\n if(s[i] == '(')\n ++cnt;\n \n else if (s[i] == ')')\n {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int open = 0;\n char ch;\n for(int i = 0; i < s.length(); i++) {\n ch = s[i];\n if(ch == '(') open++;\n else if(ch == ')') {\n if(open == 0) s[i] = '*';\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string str = \"\";\n int num = 0;\n for(int i=0;i<s.size();i++){\n if(s[i] == '('){\n num++;\n }else if(s[i] == ')'){\n if(num == 0){\n contin...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
0
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n if(s.empty()) return s;\n int vp = 0, invp = 0;\n for(int i = 0; i < s.length(); i++){\n if(s[i] == '('){\n vp++; invp++;\n }\n else if(s[i] == ')' && invp > 0){\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n // 14:50 - \n int left_para = 0, right_para = 0;\n int total_left = 0;\n for (auto &c : s) {\n if (c == '(') {\n left_para++;\n total_left++;\n } else if ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int open = 0;\n char ch;\n for(int i = 0; i < s.length(); i++) {\n ch = s[i];\n if(ch == '(') open++;\n else if(ch == ')') {\n if(open == 0) s[i] = '*';\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "#include<stack>\n#include<string>\n#include<unordered_map>\n#include<vector>\n\nusing namespace std;\n\nclass Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n if (s.size() == 0) {\n return s;\n }\n stack<char> p_stack;\n string res = \"\";\n f...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int balance = 0;\n unordered_set<int> invalid_left;\n for (int i = s.size() - 1; i >=0; --i) {\n if (s.at(i) == ')') {\n ++balance;\n } else if (s.at(i) == '(') {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string output = \"\";\n stack<int> st;\n for (int i=0; i<s.size(); ++i) {\n if (s[i] == '(') {\n st.push(i);\n } else if (s[i] == ')') {\n if (!st.empty()) {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string res = \"\";\n int n = s.size();\n stack<int> st; \n for (int i=0 ; i<n ; i++) {\n if (s[i] == '(') {\n st.push(i);\n } else if (s[i] == ')') {\n if...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int> opened;\n string ans;\n for(int i = 0; i < s.size(); ++i) {\n if(s[i] == '(') {\n opened.push(i);\n }\n else if(s[i] == ')' && !opened.empty()) {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n\n string res = \"\";\n stack<int> st;\n vector<int> index;\n int i, j;\n\n for (i = 0; i < s.size(); i++) {\n if (s[i] == '(')\n st.push(i);\n else if(s[i] == ')' && !st.empty())\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string ans;\n stack<int> st;\n vector<bool> toRemove(s.size(), false);\n\n // First pass to identify the invalid parentheses\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '(') {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int> st;\n set<int> del;\n for(int i=0; i<s.size(); i++){\n char c = s[i];\n if(c == '('){\n st.push(i);\n }\n else if(c == ')'){\n i...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<char> st ;\n stack<char> s1 ;\n\n for(int i=0;i<s.length();i++){\n if(s[i]==')' || s[i]=='(' ){\n if( s[i]=='('){\n st.push(s[i]);\n s1.push...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
1
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n \n stack<int> st;\n set<int> toRemove;\n for(int i =0;i < s.size();++i) {\n if (s[i] == '(') {\n st.push(i);\n } else if (s[i] == ')'){\n if (st.empty()) ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n //preParentheses: parentheses to remove\n stack<int> preParentheses;\n\n //Handle parentheses\n for(int i = 0; i<s.length(); i++){\n if(s[i] == '('){\n preParentheses.push(i);\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n //preParentheses: parentheses to remove\n stack<int> preParentheses;\n\n //Handle parentheses\n for(int i = 0; i<s.length(); i++){\n if(s[i] == '('){\n preParentheses.push(i);\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n = s.length();\n string res=\"\";\n int open=0;\n for(int i=0;i<n;i++){\n if(s[i]>='a' && s[i]<='z'){\n res.push_back(s[i]);\n }\n else if(s[i]=='('){\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int>st;\n unordered_set<int>to_remove;\n string result=\"\";\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='(')\n {\n st.push(i);\n }\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n = s.size();\n\n string temp = \"\";\n string ans;\n int open = 0;\n\n for(char ch : s) { // removing unnecessary closed tags\n if(ch == '(') {\n temp += ch;\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string temp=\"\", ans =\"\";\n\n int open_cnt=0;\n for(int i=0;i<s.length();i++) {\n if(s[i] =='(') {\n temp+=s[i];\n open_cnt++;\n }\n else if (s[i] == ')' and open_cnt>0) {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n // use a stack to maintain indices of unpaired \"(\"\n // meanwhile use a set to track indices of unpaired \")\"\n std::stack<int> left; \n std::unordered_set<int> right;\n\n for (int i=0; i<s.size()...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
2
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n //int opened = 0;\n //int closed = 0;\n \n //int remove = 0;\n \n string valid = \"\";\n \n vector<int> opened;\n // need to keep track of all the opened locations in case...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n queue<int> excludedIndices;\n stringstream stream;\n stack<int> openIndices;\n\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == '(') {\n openIndices.push(i);\n } e...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);\n\n string temp=\"\",ans=\"\";\n int open=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='(')temp+=s[i],open++;\n else if...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n\n string isBalanced(string s) {\n int count = 0;\n set<int> r_index;\n stack<int> st;\n \n string answer = \"\";\n\n for(int i = 0; i < s.size(); i++) {\n cout<<count;\n if(s[i] == '(') {\n st.push...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n\n string isBalanced(string s) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n \n set<int> r_index;\n stack<int> st;\n \n string answer = \"\";\n\n for(int i = 0; i < s.size(); i++) {\n if(...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n stack<int> pos;\n for(int i = 0; i < s.size(); i++){\n if(s[i] == '('){\n pos.push(i);\n }\n else if(s[i] == ')'){\n if(!pos.empty() && s[pos.top()] == '('){...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string removeExtraBracks(string s, stack<int>st, unordered_set<int> &cl)\n {\n string res=\"\";\n for(int i=s.size()-1; i>=0;i--)\n {\n if(s[i]=='(' && !st.empty() && st.top()==i)\n {\n st.pop();\n co...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string result = \"\";\n const size_t N = 100001;\n bitset<N> setBits;\n stack<int> inValid;\n stack<int> st;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '(') {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string working;\n stack<int> st;\n int n = s.size();\n\n for(int i=0; i<n; i++) {\n if(s[i] == '(') {\n st.push(i);\n working.push_back(s[i]);\n } else if...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string working;\n stack<int> st;\n int n = s.size();\n\n for(int i=0; i<n; i++) {\n if(s[i] == '(') {\n st.push(i);\n working.push_back(s[i]);\n } else if...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int n=s.size(),c=0;\n vector<int> v(n);\n for(int i=0;i<n;i++){\n if(s[i]=='(') c++;\n else if(s[i]==')') c--;\n if(c<0){\n c=0;\n v[i]=1;\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n int count = 0;\n vector<char> v;\n for (int i = 0; i < s.size(); ++i) {\n if (s[i] == '(') ++count;\n else if (s[i] == ')') {\n if (count == 0) continue;\n --cou...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\n string deleteExtraClosing(string s, bool isRev=false)\n {\n string ans = \"\";\n int i, flag = 0;\n char a = '(', b = ')';\n if(isRev)\n {\n a = ')';\n b = '(';\n }\n for(i=0; i<s.length(); i++)\n {\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string s1 = removeInvalid(s, '(', ')');\n reverse(s1.begin(), s1.end());\n string s2 = removeInvalid(s1, ')', '(');\n reverse(s2.begin(), s2.end());\n\n return s2;\n }\n\n string removeInvalid(...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n string ans = \"\";\n vector<int> index(s.length(),0);\n stack<int> st;\n for(int i=0;i<s.length();i++){\n if(s[i] == '('){\n st.push(i);\n }\n if(s[i] == ')' ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n \n // Time: O(N)\n // Space: O(N)\n \n string minRemoveToMakeValid(string S) {\n int N = S.length();\n vector<bool> marked(N, false);\n \n stack<pair<char,int>> st;\n for (int i = 0; i < N; i++) {\n char c = S[i];\n ...
1,371
<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p> <p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses str...
3
{ "code": "class Solution {\npublic:\n string minRemoveToMakeValid(string s) {\n vector<int> mask(s.size(), 1);\n int left = 0;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '(') {\n left ++;\n } else if (s[i] == ')') {\n if (left > 0)...