-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3325.cpp
More file actions
29 lines (29 loc) · 850 Bytes
/
3325.cpp
File metadata and controls
29 lines (29 loc) · 850 Bytes
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
class Solution {
public:
bool valid(vector<int>& hash, int k) {
for (auto& num : hash) {
if (num >= k) return true;
}
return false;
}
int numberOfSubstrings(string s, int k) {
int n = s.size();
vector<int> hash(26, 0);
int invalidNum = 0;
int left = 0;
for (int right = 0; right < n; ++right) {
hash[s[right] - 'a']++;
while (left < n && valid(hash, k)) {
hash[s[left] - 'a']--;
left++;
}
// [left, right], [left + 1, right] ... are invalid
if (left <= right && !valid(hash, k)) {
invalidNum += (right - left + 1);
}
}
int res = 0;
for (int i = 0; i < n; ++i) res += (i + 1);
return res - invalidNum;
}
};