-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Substrings That Satisfy K-Constraint II.cpp
75 lines (52 loc) · 1.53 KB
/
Count Substrings That Satisfy K-Constraint II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution {
public:
vector<long long> countKConstraintSubstrings(string s, int k, vector<vector<int>>& queries) {
vector<long long>result;
int n = s.length();
vector<int> leftMost(n,0);
vector<int> rightMost(n,0);
int i = 0;
int j = 0;
unordered_map<char,int>mapping;
while(j<n){
mapping[s[j]]++;
while(mapping['0'] > k && mapping['1'] > k){
mapping[s[i]]--;
i++;
}
leftMost[j] = i;
j++;
}
mapping.clear();
i = n-1;
j = n-1;
while( j>= 0){
mapping[s[j]]++;
while(mapping['0'] > k && mapping['1'] > k) {
mapping[s[i]]--;
i--;
}
rightMost[j] = i;
j--;
}
vector<int>temp(n,0);
for(int j = 0; j < n; j++){
temp[j] = j - leftMost[j] + 1;
}
vector<long long >cumSum(n,0);
cumSum[0] = temp[0];
for(int i = 1; i < n; i++){
cumSum[i] = cumSum[i-1] + temp[i];
}
for(vector<int>& query : queries){
int low = query[0];
int high = query[1];
int valid = min(high,rightMost[low]);
long long length = valid - low + 1;
long long total = length * (length +1 ) /2;
total += cumSum[high] - cumSum[valid];
result.push_back(total);
}
return result ;
}
};