Skip to content

Commit 9472f7b

Browse files
authored
feat: add solutions to lc problem: No.2067 (#2570)
No.2067.Number of Equal Count Substrings
1 parent 1d15cc5 commit 9472f7b

File tree

8 files changed

+276
-261
lines changed

8 files changed

+276
-261
lines changed

solution/2000-2099/2067.Number of Equal Count Substrings/README.md

+92-88
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363

6464
### 方法一:枚举 + 滑动窗口
6565

66-
我们可以在 $[1..26]$ 范围内枚举子串的字母种类数 $x$,那么子串长度就是 $count \times x$。
66+
我们可以在 $[1..26]$ 范围内枚举子串的字母种类数 $i$,那么子串长度就是 $i \times count$。
6767

68-
接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $y$ 中。如果此时 $x = y$,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。
68+
接下来,我们将当前子串长度作为窗口的大小,统计窗口大小中有多少种字母的个数为 $count$,记录在 $t$ 中。如果此时 $i = t$,说明当前窗口中的字母个数都为 $count$,那么就可以将答案加一。
6969

7070
时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字母的种类数,本题中 $C = 26$。
7171

@@ -75,57 +75,46 @@
7575
class Solution:
7676
def equalCountSubstrings(self, s: str, count: int) -> int:
7777
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):
8181
break
8282
cnt = Counter()
83-
y = 0
84-
for i, c in enumerate(s):
83+
t = 0
84+
for j, c in enumerate(s):
8585
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
9493
return ans
9594
```
9695

9796
```java
9897
class Solution {
9998
public int equalCountSubstrings(String s, int count) {
10099
int ans = 0;
100+
int[] cnt = new int[26];
101101
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';
108108
++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';
118113
--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;
128116
}
117+
ans += i == t ? 1 : 0;
129118
}
130119
}
131120
return ans;
@@ -140,23 +129,20 @@ public:
140129
int ans = 0;
141130
int n = s.size();
142131
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;
158144
}
159-
ans += x == y;
145+
ans += i == t;
160146
}
161147
}
162148
return ans;
@@ -167,31 +153,28 @@ public:
167153
```go
168154
func equalCountSubstrings(s string, count int) (ans int) {
169155
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
173158
cnt := [26]int{}
174-
for i, c := range s {
159+
t := 0
160+
for j, c := range s {
175161
a := c - 'a'
176162
cnt[a]++
177163
if cnt[a] == count {
178-
y++
164+
t++
165+
} else if cnt[a] == count+1 {
166+
t--
179167
}
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'
186170
cnt[b]--
187171
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--
192175
}
193176
}
194-
if x == y {
177+
if i == t {
195178
ans++
196179
}
197180
}
@@ -200,32 +183,53 @@ func equalCountSubstrings(s string, count int) (ans int) {
200183
}
201184
```
202185

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+
203210
```js
204211
/**
205212
* @param {string} s
206213
* @param {number} count
207214
* @return {number}
208215
*/
209216
var equalCountSubstrings = function (s, count) {
210-
let ans = 0;
211217
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;
227231
}
228-
ans += x == y;
232+
ans += i === t ? 1 : 0;
229233
}
230234
}
231235
return ans;

0 commit comments

Comments
 (0)