Skip to content

[pull] main from doocs:main #417

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

Merged
merged 2 commits into from
Mar 16, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

## 站点

https://doocs.github.io/leetcode
https://leetcode.doocs.org

## 算法全解

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This project contains solutions for problems from LeetCode, "Coding Interviews (

## Site

https://doocs.github.io/leetcode/en
https://leetcode.doocs.org/en

## Solutions

Expand Down
36 changes: 33 additions & 3 deletions solution/2200-2299/2272.Substring With Largest Variance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值
递推公式如下:

1. 如果当前字符为 $a$,则 $f[0]$ 和 $f[1]$ 都加 $1$;
1. 如果当前字符为 $b$,则 $f[1]=\max(f[1]-1, f[0]-1)$,而 $f[0]=0$;
1. 如果当前字符为 $b$,则 $f[1] = \max(f[1] - 1, f[0] - 1)$,而 $f[0] = 0$;
1. 否则,无需考虑。

注意,初始时将 $f[1]$ 赋值为一个负数最大值,可以保证更新答案时是合法的。

时间复杂度 $O(n\times C^2)$,其中 $n$ 表示字符串 $s$ 的长度,而 $C$ 为字符集大小,本题中 $C=26$。
时间复杂度 $O(n \times |\Sigma|^2)$,其中 $n$ 是字符串长度,而 $|\Sigma|$ 是字符集大小。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -144,7 +144,9 @@ public:
int ans = 0;
for (char a = 'a'; a <= 'z'; ++a) {
for (char b = 'a'; b <= 'z'; ++b) {
if (a == b) continue;
if (a == b) {
continue;
}
int f[2] = {0, -n};
for (char c : s) {
if (c == a) {
Expand Down Expand Up @@ -190,6 +192,34 @@ func largestVariance(s string) int {
}
```

#### TypeScript

```ts
function largestVariance(s: string): number {
const n: number = s.length;
let ans: number = 0;
for (let a = 97; a <= 122; ++a) {
for (let b = 97; b <= 122; ++b) {
if (a === b) {
continue;
}
const f: number[] = [0, -n];
for (let i = 0; i < n; ++i) {
if (s.charCodeAt(i) === a) {
f[0]++;
f[1]++;
} else if (s.charCodeAt(i) === b) {
f[1] = Math.max(f[0] - 1, f[1] - 1);
f[0] = 0;
}
ans = Math.max(ans, f[1]);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,21 @@ No letter occurs more than once in s, so the variance of every substring is 0.

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration + Dynamic Programming

Since the character set only contains lowercase letters, we can consider enumerating the most frequent character $a$ and the least frequent character $b$. For a substring, the difference in the number of occurrences of these two characters is the variance of the substring.

Specifically, we use a double loop to enumerate $a$ and $b$. We use $f[0]$ to record the number of consecutive occurrences of character $a$ ending at the current character, and $f[1]$ to record the variance of the substring ending at the current character and containing both $a$ and $b$. We iterate to find the maximum value of $f[1]$.

The recurrence formula is as follows:

1. If the current character is $a$, then both $f[0]$ and $f[1]$ are incremented by $1$;
2. If the current character is $b$, then $f[1] = \max(f[1] - 1, f[0] - 1)$, and $f[0] = 0$;
3. Otherwise, no need to consider.

Note that initially setting $f[1]$ to a negative maximum value ensures that updating the answer is valid.

The time complexity is $O(n \times |\Sigma|^2)$, where $n$ is the length of the string, and $|\Sigma|$ is the size of the character set. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -128,7 +142,9 @@ public:
int ans = 0;
for (char a = 'a'; a <= 'z'; ++a) {
for (char b = 'a'; b <= 'z'; ++b) {
if (a == b) continue;
if (a == b) {
continue;
}
int f[2] = {0, -n};
for (char c : s) {
if (c == a) {
Expand Down Expand Up @@ -174,6 +190,34 @@ func largestVariance(s string) int {
}
```

#### TypeScript

```ts
function largestVariance(s: string): number {
const n: number = s.length;
let ans: number = 0;
for (let a = 97; a <= 122; ++a) {
for (let b = 97; b <= 122; ++b) {
if (a === b) {
continue;
}
const f: number[] = [0, -n];
for (let i = 0; i < n; ++i) {
if (s.charCodeAt(i) === a) {
f[0]++;
f[1]++;
} else if (s.charCodeAt(i) === b) {
f[1] = Math.max(f[0] - 1, f[1] - 1);
f[0] = 0;
}
ans = Math.max(ans, f[1]);
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Solution {
int ans = 0;
for (char a = 'a'; a <= 'z'; ++a) {
for (char b = 'a'; b <= 'z'; ++b) {
if (a == b) continue;
if (a == b) {
continue;
}
int f[2] = {0, -n};
for (char c : s) {
if (c == a) {
Expand All @@ -21,4 +23,4 @@ class Solution {
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function largestVariance(s: string): number {
const n: number = s.length;
let ans: number = 0;
for (let a = 97; a <= 122; ++a) {
for (let b = 97; b <= 122; ++b) {
if (a === b) {
continue;
}
const f: number[] = [0, -n];
for (let i = 0; i < n; ++i) {
if (s.charCodeAt(i) === a) {
f[0]++;
f[1]++;
} else if (s.charCodeAt(i) === b) {
f[1] = Math.max(f[0] - 1, f[1] - 1);
f[0] = 0;
}
ans = Math.max(ans, f[1]);
}
}
}
return ans;
}