-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
36 lines (36 loc) · 1 KB
/
Solution.java
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
class Solution {
public int equalCountSubstrings(String s, int count) {
int ans = 0;
int n = s.length();
for (int x = 1; x < 27 && count * x <= n; ++x) {
int m = count * x;
int[] cnt = new int[26];
int y = 0;
for (int i = 0; i < n; ++i) {
int a = s.charAt(i) - 'a';
++cnt[a];
if (cnt[a] == count) {
++y;
}
if (cnt[a] == count + 1) {
--y;
}
int j = i - m;
if (j >= 0) {
int b = s.charAt(j) - 'a';
--cnt[b];
if (cnt[b] == count) {
++y;
}
if (cnt[b] == count - 1) {
--y;
}
}
if (x == y) {
++ans;
}
}
}
return ans;
}
}