Skip to content

Commit bc2b184

Browse files
authored
feat: add solutions to lc problem: No.2272 (#4165)
No.2272.Substring With Largest Variance
1 parent 66f296d commit bc2b184

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

solution/2200-2299/2272.Substring With Largest Variance/README.md

+33-3
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值
7474
递推公式如下:
7575

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

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

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

8484
<!-- tabs:start -->
8585

@@ -144,7 +144,9 @@ public:
144144
int ans = 0;
145145
for (char a = 'a'; a <= 'z'; ++a) {
146146
for (char b = 'a'; b <= 'z'; ++b) {
147-
if (a == b) continue;
147+
if (a == b) {
148+
continue;
149+
}
148150
int f[2] = {0, -n};
149151
for (char c : s) {
150152
if (c == a) {
@@ -190,6 +192,34 @@ func largestVariance(s string) int {
190192
}
191193
```
192194

195+
#### TypeScript
196+
197+
```ts
198+
function largestVariance(s: string): number {
199+
const n: number = s.length;
200+
let ans: number = 0;
201+
for (let a = 97; a <= 122; ++a) {
202+
for (let b = 97; b <= 122; ++b) {
203+
if (a === b) {
204+
continue;
205+
}
206+
const f: number[] = [0, -n];
207+
for (let i = 0; i < n; ++i) {
208+
if (s.charCodeAt(i) === a) {
209+
f[0]++;
210+
f[1]++;
211+
} else if (s.charCodeAt(i) === b) {
212+
f[1] = Math.max(f[0] - 1, f[1] - 1);
213+
f[0] = 0;
214+
}
215+
ans = Math.max(ans, f[1]);
216+
}
217+
}
218+
}
219+
return ans;
220+
}
221+
```
222+
193223
<!-- tabs:end -->
194224

195225
<!-- solution:end -->

solution/2200-2299/2272.Substring With Largest Variance/README_EN.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,21 @@ No letter occurs more than once in s, so the variance of every substring is 0.
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Enumeration + Dynamic Programming
67+
68+
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.
69+
70+
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]$.
71+
72+
The recurrence formula is as follows:
73+
74+
1. If the current character is $a$, then both $f[0]$ and $f[1]$ are incremented by $1$;
75+
2. If the current character is $b$, then $f[1] = \max(f[1] - 1, f[0] - 1)$, and $f[0] = 0$;
76+
3. Otherwise, no need to consider.
77+
78+
Note that initially setting $f[1]$ to a negative maximum value ensures that updating the answer is valid.
79+
80+
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)$.
6781

6882
<!-- tabs:start -->
6983

@@ -128,7 +142,9 @@ public:
128142
int ans = 0;
129143
for (char a = 'a'; a <= 'z'; ++a) {
130144
for (char b = 'a'; b <= 'z'; ++b) {
131-
if (a == b) continue;
145+
if (a == b) {
146+
continue;
147+
}
132148
int f[2] = {0, -n};
133149
for (char c : s) {
134150
if (c == a) {
@@ -174,6 +190,34 @@ func largestVariance(s string) int {
174190
}
175191
```
176192

193+
#### TypeScript
194+
195+
```ts
196+
function largestVariance(s: string): number {
197+
const n: number = s.length;
198+
let ans: number = 0;
199+
for (let a = 97; a <= 122; ++a) {
200+
for (let b = 97; b <= 122; ++b) {
201+
if (a === b) {
202+
continue;
203+
}
204+
const f: number[] = [0, -n];
205+
for (let i = 0; i < n; ++i) {
206+
if (s.charCodeAt(i) === a) {
207+
f[0]++;
208+
f[1]++;
209+
} else if (s.charCodeAt(i) === b) {
210+
f[1] = Math.max(f[0] - 1, f[1] - 1);
211+
f[0] = 0;
212+
}
213+
ans = Math.max(ans, f[1]);
214+
}
215+
}
216+
}
217+
return ans;
218+
}
219+
```
220+
177221
<!-- tabs:end -->
178222

179223
<!-- solution:end -->

solution/2200-2299/2272.Substring With Largest Variance/Solution.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class Solution {
55
int ans = 0;
66
for (char a = 'a'; a <= 'z'; ++a) {
77
for (char b = 'a'; b <= 'z'; ++b) {
8-
if (a == b) continue;
8+
if (a == b) {
9+
continue;
10+
}
911
int f[2] = {0, -n};
1012
for (char c : s) {
1113
if (c == a) {
@@ -21,4 +23,4 @@ class Solution {
2123
}
2224
return ans;
2325
}
24-
};
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function largestVariance(s: string): number {
2+
const n: number = s.length;
3+
let ans: number = 0;
4+
for (let a = 97; a <= 122; ++a) {
5+
for (let b = 97; b <= 122; ++b) {
6+
if (a === b) {
7+
continue;
8+
}
9+
const f: number[] = [0, -n];
10+
for (let i = 0; i < n; ++i) {
11+
if (s.charCodeAt(i) === a) {
12+
f[0]++;
13+
f[1]++;
14+
} else if (s.charCodeAt(i) === b) {
15+
f[1] = Math.max(f[0] - 1, f[1] - 1);
16+
f[0] = 0;
17+
}
18+
ans = Math.max(ans, f[1]);
19+
}
20+
}
21+
}
22+
return ans;
23+
}

0 commit comments

Comments
 (0)