Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2067 #2570

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 92 additions & 88 deletions solution/2000-2099/2067.Number of Equal Count Substrings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@

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

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

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

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

Expand All @@ -75,57 +75,46 @@
class Solution:
def equalCountSubstrings(self, s: str, count: int) -> int:
ans = 0
for x in range(1, 27):
m = count * x
if m > len(s):
for i in range(1, 27):
k = i * count
if k > len(s):
break
cnt = Counter()
y = 0
for i, c in enumerate(s):
t = 0
for j, c in enumerate(s):
cnt[c] += 1
y += cnt[c] == count
y -= cnt[c] == count + 1
j = i - m
if j >= 0:
cnt[s[j]] -= 1
y += cnt[s[j]] == count
y -= cnt[s[j]] == count - 1
ans += x == y
t += cnt[c] == count
t -= cnt[c] == count + 1
if j >= k:
cnt[s[j - k]] -= 1
t += cnt[s[j - k]] == count
t -= cnt[s[j - k]] == count - 1
ans += i == t
return ans
```

```java
class Solution {
public int equalCountSubstrings(String s, int count) {
int ans = 0;
int[] cnt = new int[26];
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';
for (int i = 1; i < 27 && i * count <= n; ++i) {
int k = i * count;
Arrays.fill(cnt, 0);
int t = 0;
for (int j = 0; j < n; ++j) {
int a = s.charAt(j) - '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';
t += cnt[a] == count ? 1 : 0;
t -= cnt[a] == count + 1 ? 1 : 0;
if (j - k >= 0) {
int b = s.charAt(j - k) - 'a';
--cnt[b];
if (cnt[b] == count) {
++y;
}
if (cnt[b] == count - 1) {
--y;
}
}
if (x == y) {
++ans;
t += cnt[b] == count ? 1 : 0;
t -= cnt[b] == count - 1 ? 1 : 0;
}
ans += i == t ? 1 : 0;
}
}
return ans;
Expand All @@ -140,23 +129,20 @@ public:
int ans = 0;
int n = s.size();
int cnt[26];
for (int x = 1; x < 27 && count * x <= n; ++x) {
int m = count * x;
memset(cnt, 0, sizeof cnt);
int y = 0;
for (int i = 0; i < n; ++i) {
int a = s[i] - 'a';
++cnt[a];
y += cnt[a] == count;
y -= cnt[a] == count + 1;
int j = i - m;
if (j >= 0) {
int b = s[j] - 'a';
--cnt[b];
y += cnt[b] == count;
y -= cnt[b] == count - 1;
for (int i = 1; i < 27 && i * count <= n; ++i) {
int k = i * count;
memset(cnt, 0, sizeof(cnt));
int t = 0;
for (int j = 0; j < n; ++j) {
int a = s[j] - 'a';
t += ++cnt[a] == count;
t -= cnt[a] == count + 1;
if (j >= k) {
int b = s[j - k] - 'a';
t += --cnt[b] == count;
t -= cnt[b] == count - 1;
}
ans += x == y;
ans += i == t;
}
}
return ans;
Expand All @@ -167,31 +153,28 @@ public:
```go
func equalCountSubstrings(s string, count int) (ans int) {
n := len(s)
for x := 1; x < 27 && x*count <= n; x++ {
m := x * count
y := 0
for i := 1; i < 27 && i*count <= n; i++ {
k := i * count
cnt := [26]int{}
for i, c := range s {
t := 0
for j, c := range s {
a := c - 'a'
cnt[a]++
if cnt[a] == count {
y++
t++
} else if cnt[a] == count+1 {
t--
}
if cnt[a] == count+1 {
y--
}
j := i - m
if j >= 0 {
b := s[j] - 'a'
if j >= k {
b := s[j-k] - 'a'
cnt[b]--
if cnt[b] == count {
y++
}
if cnt[b] == count-1 {
y--
t++
} else if cnt[b] == count-1 {
t--
}
}
if x == y {
if i == t {
ans++
}
}
Expand All @@ -200,32 +183,53 @@ func equalCountSubstrings(s string, count int) (ans int) {
}
```

```ts
function equalCountSubstrings(s: string, count: number): number {
const n = s.length;
let ans = 0;
for (let i = 1; i < 27 && i * count <= n; ++i) {
const k = i * count;
const cnt: number[] = Array(26).fill(0);
let t = 0;
for (let j = 0; j < n; ++j) {
const a = s.charCodeAt(j) - 97;
t += ++cnt[a] === count ? 1 : 0;
t -= cnt[a] === count + 1 ? 1 : 0;
if (j >= k) {
const b = s.charCodeAt(j - k) - 97;
t += --cnt[b] === count ? 1 : 0;
t -= cnt[b] === count - 1 ? 1 : 0;
}
ans += i === t ? 1 : 0;
}
}
return ans;
}
```

```js
/**
* @param {string} s
* @param {number} count
* @return {number}
*/
var equalCountSubstrings = function (s, count) {
let ans = 0;
const n = s.length;
for (let x = 1; x <= 26 && x * count <= n; ++x) {
const m = x * count;
const cnt = new Array(26).fill(0);
let y = 0;
for (let i = 0; i < n; ++i) {
const a = s.charCodeAt(i) - 'a'.charCodeAt(0);
++cnt[a];
y += cnt[a] == count;
y -= cnt[a] == count + 1;
const j = i - m;
if (j >= 0) {
const b = s.charCodeAt(j) - 'a'.charCodeAt(0);
--cnt[b];
y += cnt[b] == count;
y -= cnt[b] == count - 1;
let ans = 0;
for (let i = 1; i < 27 && i * count <= n; ++i) {
const k = i * count;
const cnt = Array(26).fill(0);
let t = 0;
for (let j = 0; j < n; ++j) {
const a = s.charCodeAt(j) - 97;
t += ++cnt[a] === count ? 1 : 0;
t -= cnt[a] === count + 1 ? 1 : 0;
if (j >= k) {
const b = s.charCodeAt(j - k) - 97;
t += --cnt[b] === count ? 1 : 0;
t -= cnt[b] === count - 1 ? 1 : 0;
}
ans += x == y;
ans += i === t ? 1 : 0;
}
}
return ans;
Expand Down
Loading
Loading