diff --git a/README.md b/README.md index fac8b5005ec39..308efcb8b4cd7 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ ## 站点 -https://doocs.github.io/leetcode +https://leetcode.doocs.org ## 算法全解 diff --git a/README_EN.md b/README_EN.md index ec460d3984fa4..4161ac3fb40da 100644 --- a/README_EN.md +++ b/README_EN.md @@ -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 diff --git a/solution/2200-2299/2272.Substring With Largest Variance/README.md b/solution/2200-2299/2272.Substring With Largest Variance/README.md index e66f437d2f9d4..5163d465375b0 100644 --- a/solution/2200-2299/2272.Substring With Largest Variance/README.md +++ b/solution/2200-2299/2272.Substring With Largest Variance/README.md @@ -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)$。 @@ -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) { @@ -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; +} +``` + diff --git a/solution/2200-2299/2272.Substring With Largest Variance/README_EN.md b/solution/2200-2299/2272.Substring With Largest Variance/README_EN.md index 74b82d61a8b8d..16f280421f49d 100644 --- a/solution/2200-2299/2272.Substring With Largest Variance/README_EN.md +++ b/solution/2200-2299/2272.Substring With Largest Variance/README_EN.md @@ -63,7 +63,21 @@ No letter occurs more than once in s, so the variance of every substring is 0. -### 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)$. @@ -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) { @@ -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; +} +``` + diff --git a/solution/2200-2299/2272.Substring With Largest Variance/Solution.cpp b/solution/2200-2299/2272.Substring With Largest Variance/Solution.cpp index e1d3382bf6ae9..01a28127ad610 100644 --- a/solution/2200-2299/2272.Substring With Largest Variance/Solution.cpp +++ b/solution/2200-2299/2272.Substring With Largest Variance/Solution.cpp @@ -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) { @@ -21,4 +23,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2272.Substring With Largest Variance/Solution.ts b/solution/2200-2299/2272.Substring With Largest Variance/Solution.ts new file mode 100644 index 0000000000000..ea37a77c22d97 --- /dev/null +++ b/solution/2200-2299/2272.Substring With Largest Variance/Solution.ts @@ -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; +}