-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-substrings-that-satisfy-k-constraint-ii.cpp
36 lines (34 loc) · 1.27 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
// Time: O(n)
// Space: O(n)
// two pointers, sliding window, prefix sum, hash table
class Solution {
public:
vector<long long> countKConstraintSubstrings(string s, int k, vector<vector<int>>& queries) {
const auto& count = [](int64_t l) {
return (l + 1) * l / 2;
};
vector<int64_t> prefix(size(s) + 1);
vector<int> lookup(size(s), -1);
for (int right = 0, left = 0, cnt = 0; right < size(s); ++right) {
cnt += s[right] == '1' ? 1 : 0;
while (!(cnt <= k || (right - left + 1) - cnt <= k)) {
cnt -= s[left++] == '1' ? 1 : 0;
}
prefix[right + 1] = prefix[right] + (right - left + 1);
lookup[left] = right;
}
assert(lookup[0] != -1);
for (int i = 0; i + 1 < size(s); ++i) {
if (lookup[i + 1] == -1) {
lookup[i + 1] = lookup[i];
}
}
vector<long long> result(size(queries));
for (int i = 0; i < size(queries); ++i) {
const int left = queries[i][0], right = queries[i][1];
const int new_right = min(lookup[left], right);
result[i] = count(new_right - left + 1) + (prefix[right + 1] - prefix[new_right + 1]);
}
return result;
}
};