63
63
64
64
### 方法一:枚举 + 滑动窗口
65
65
66
- 我们可以在 $[ 1..26] $ 范围内枚举子串的字母种类数 $x $,那么子串长度就是 $count \times x $。
66
+ 我们可以在 $[ 1..26] $ 范围内枚举子串的字母种类数 $i $,那么子串长度就是 $i \times count $。
67
67
68
- 接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $y $ 中。如果此时 $x = y $,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。
68
+ 接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $t $ 中。如果此时 $i = t $,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。
69
69
70
70
时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字母的种类数,本题中 $C = 26$。
71
71
75
75
class Solution :
76
76
def equalCountSubstrings (self , s : str , count : int ) -> int :
77
77
ans = 0
78
- for x in range (1 , 27 ):
79
- m = count * x
80
- if m > len (s):
78
+ for i in range (1 , 27 ):
79
+ k = i * count
80
+ if k > len (s):
81
81
break
82
82
cnt = Counter()
83
- y = 0
84
- for i , c in enumerate (s):
83
+ t = 0
84
+ for j , c in enumerate (s):
85
85
cnt[c] += 1
86
- y += cnt[c] == count
87
- y -= cnt[c] == count + 1
88
- j = i - m
89
- if j >= 0 :
90
- cnt[s[j]] -= 1
91
- y += cnt[s[j]] == count
92
- y -= cnt[s[j]] == count - 1
93
- ans += x == y
86
+ t += cnt[c] == count
87
+ t -= cnt[c] == count + 1
88
+ if j >= k:
89
+ cnt[s[j - k]] -= 1
90
+ t += cnt[s[j - k]] == count
91
+ t -= cnt[s[j - k]] == count - 1
92
+ ans += i == t
94
93
return ans
95
94
```
96
95
97
96
``` java
98
97
class Solution {
99
98
public int equalCountSubstrings (String s , int count ) {
100
99
int ans = 0 ;
100
+ int [] cnt = new int [26 ];
101
101
int n = s. length();
102
- for (int x = 1 ; x < 27 && count * x <= n; ++ x ) {
103
- int m = count * x ;
104
- int [] cnt = new int [ 26 ] ;
105
- int y = 0 ;
106
- for (int i = 0 ; i < n; ++ i ) {
107
- int a = s. charAt(i ) - ' a' ;
102
+ for (int i = 1 ; i < 27 && i * count <= n; ++ i ) {
103
+ int k = i * count ;
104
+ Arrays . fill( cnt, 0 ) ;
105
+ int t = 0 ;
106
+ for (int j = 0 ; j < n; ++ j ) {
107
+ int a = s. charAt(j ) - ' a' ;
108
108
++ cnt[a];
109
- if (cnt[a] == count) {
110
- ++ y;
111
- }
112
- if (cnt[a] == count + 1 ) {
113
- -- y;
114
- }
115
- int j = i - m;
116
- if (j >= 0 ) {
117
- int b = s. charAt(j) - ' a' ;
109
+ t += cnt[a] == count ? 1 : 0 ;
110
+ t -= cnt[a] == count + 1 ? 1 : 0 ;
111
+ if (j - k >= 0 ) {
112
+ int b = s. charAt(j - k) - ' a' ;
118
113
-- cnt[b];
119
- if (cnt[b] == count) {
120
- ++ y;
121
- }
122
- if (cnt[b] == count - 1 ) {
123
- -- y;
124
- }
125
- }
126
- if (x == y) {
127
- ++ ans;
114
+ t += cnt[b] == count ? 1 : 0 ;
115
+ t -= cnt[b] == count - 1 ? 1 : 0 ;
128
116
}
117
+ ans += i == t ? 1 : 0 ;
129
118
}
130
119
}
131
120
return ans;
@@ -140,23 +129,20 @@ public:
140
129
int ans = 0;
141
130
int n = s.size();
142
131
int cnt[ 26] ;
143
- for (int x = 1; x < 27 && count * x <= n; ++x) {
144
- int m = count * x;
145
- memset(cnt, 0, sizeof cnt);
146
- int y = 0;
147
- for (int i = 0; i < n; ++i) {
148
- int a = s[ i] - 'a';
149
- ++cnt[ a] ;
150
- y += cnt[ a] == count;
151
- y -= cnt[ a] == count + 1;
152
- int j = i - m;
153
- if (j >= 0) {
154
- int b = s[ j] - 'a';
155
- --cnt[ b] ;
156
- y += cnt[ b] == count;
157
- y -= cnt[ b] == count - 1;
132
+ for (int i = 1; i < 27 && i * count <= n; ++i) {
133
+ int k = i * count;
134
+ memset(cnt, 0, sizeof(cnt));
135
+ int t = 0;
136
+ for (int j = 0; j < n; ++j) {
137
+ int a = s[ j] - 'a';
138
+ t += ++cnt[ a] == count;
139
+ t -= cnt[ a] == count + 1;
140
+ if (j >= k) {
141
+ int b = s[ j - k] - 'a';
142
+ t += --cnt[ b] == count;
143
+ t -= cnt[ b] == count - 1;
158
144
}
159
- ans += x == y ;
145
+ ans += i == t ;
160
146
}
161
147
}
162
148
return ans;
@@ -167,31 +153,28 @@ public:
167
153
```go
168
154
func equalCountSubstrings(s string, count int) (ans int) {
169
155
n := len(s)
170
- for x := 1; x < 27 && x*count <= n; x++ {
171
- m := x * count
172
- y := 0
156
+ for i := 1; i < 27 && i*count <= n; i++ {
157
+ k := i * count
173
158
cnt := [26]int{}
174
- for i, c := range s {
159
+ t := 0
160
+ for j, c := range s {
175
161
a := c - 'a'
176
162
cnt[a]++
177
163
if cnt[a] == count {
178
- y++
164
+ t++
165
+ } else if cnt[a] == count+1 {
166
+ t--
179
167
}
180
- if cnt[a] == count+1 {
181
- y--
182
- }
183
- j := i - m
184
- if j >= 0 {
185
- b := s[j] - 'a'
168
+ if j >= k {
169
+ b := s[j-k] - 'a'
186
170
cnt[b]--
187
171
if cnt[b] == count {
188
- y++
189
- }
190
- if cnt[b] == count-1 {
191
- y--
172
+ t++
173
+ } else if cnt[b] == count-1 {
174
+ t--
192
175
}
193
176
}
194
- if x == y {
177
+ if i == t {
195
178
ans++
196
179
}
197
180
}
@@ -200,32 +183,53 @@ func equalCountSubstrings(s string, count int) (ans int) {
200
183
}
201
184
```
202
185
186
+ ``` ts
187
+ function equalCountSubstrings(s : string , count : number ): number {
188
+ const n = s .length ;
189
+ let ans = 0 ;
190
+ for (let i = 1 ; i < 27 && i * count <= n ; ++ i ) {
191
+ const k = i * count ;
192
+ const cnt: number [] = Array (26 ).fill (0 );
193
+ let t = 0 ;
194
+ for (let j = 0 ; j < n ; ++ j ) {
195
+ const a = s .charCodeAt (j ) - 97 ;
196
+ t += ++ cnt [a ] === count ? 1 : 0 ;
197
+ t -= cnt [a ] === count + 1 ? 1 : 0 ;
198
+ if (j >= k ) {
199
+ const b = s .charCodeAt (j - k ) - 97 ;
200
+ t += -- cnt [b ] === count ? 1 : 0 ;
201
+ t -= cnt [b ] === count - 1 ? 1 : 0 ;
202
+ }
203
+ ans += i === t ? 1 : 0 ;
204
+ }
205
+ }
206
+ return ans ;
207
+ }
208
+ ```
209
+
203
210
``` js
204
211
/**
205
212
* @param {string} s
206
213
* @param {number} count
207
214
* @return {number}
208
215
*/
209
216
var equalCountSubstrings = function (s , count ) {
210
- let ans = 0 ;
211
217
const n = s .length ;
212
- for (let x = 1 ; x <= 26 && x * count <= n; ++ x) {
213
- const m = x * count;
214
- const cnt = new Array (26 ).fill (0 );
215
- let y = 0 ;
216
- for (let i = 0 ; i < n; ++ i) {
217
- const a = s .charCodeAt (i) - ' a' .charCodeAt (0 );
218
- ++ cnt[a];
219
- y += cnt[a] == count;
220
- y -= cnt[a] == count + 1 ;
221
- const j = i - m;
222
- if (j >= 0 ) {
223
- const b = s .charCodeAt (j) - ' a' .charCodeAt (0 );
224
- -- cnt[b];
225
- y += cnt[b] == count;
226
- y -= cnt[b] == count - 1 ;
218
+ let ans = 0 ;
219
+ for (let i = 1 ; i < 27 && i * count <= n; ++ i) {
220
+ const k = i * count;
221
+ const cnt = Array (26 ).fill (0 );
222
+ let t = 0 ;
223
+ for (let j = 0 ; j < n; ++ j) {
224
+ const a = s .charCodeAt (j) - 97 ;
225
+ t += ++ cnt[a] === count ? 1 : 0 ;
226
+ t -= cnt[a] === count + 1 ? 1 : 0 ;
227
+ if (j >= k) {
228
+ const b = s .charCodeAt (j - k) - 97 ;
229
+ t += -- cnt[b] === count ? 1 : 0 ;
230
+ t -= cnt[b] === count - 1 ? 1 : 0 ;
227
231
}
228
- ans += x == y ;
232
+ ans += i === t ? 1 : 0 ;
229
233
}
230
234
}
231
235
return ans;
0 commit comments