Skip to content

Commit 3314bae

Browse files
authored
Create number-of-equal-count-substrings.cpp
1 parent ff60ffb commit 3314bae

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(26 * n) = O(n)
2+
// Space: O(26) = O(1)
3+
4+
class Solution {
5+
public:
6+
int equalCountSubstrings(string s, int count) {
7+
int result = 0;
8+
const int max_l = min(size(unordered_set<char>(cbegin(s), cend(s))), size(s) / count);
9+
for (int l = 1; l <= max_l; ++l) {
10+
unordered_map<char, int> cnt;
11+
for (int i = 0, equal_cnt = 0; i < size(s); ++i) {
12+
equal_cnt += static_cast<int>(++cnt[s[i]] == count);
13+
if (i >= count * l) {
14+
equal_cnt -= static_cast<int>(cnt[s[i - count * l]]-- == count);
15+
}
16+
result += static_cast<int>(equal_cnt == l);
17+
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)