diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md index 65db32d48661..fcccfc564ad1 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README.md @@ -67,7 +67,18 @@ tags: -### 方法一 +### 方法一:遍历 + 计数 + +我们可以使用两个变量 $x$ 和 $y$ 分别记录当前字符串中 $\textit{pattern}[0]$ 和 $\textit{pattern}[1]$ 出现的次数。 + +然后遍历字符串 $\textit{text}$,对于当前遍历到的字符 $c$: + +- 如果 $c$ 等于 $\textit{pattern}[1]$,我们将 $y$ 加一,此时之前出现过的所有 $\textit{pattern}[0]$ 都可以和当前的 $c$ 组成一个 $\textit{pattern}$ 子序列,因此答案加上 $x$; +- 如果 $c$ 等于 $\textit{pattern}[0]$,我们将 $x$ 加一。 + +遍历结束后,由于我们可以插入一个字符,因此,如果我们在字符串开头加上 $\textit{pattern}[0]$,那么可以得到 $y$ 个 $\textit{pattern}$ 子序列;如果我们在字符串结尾加上 $\textit{pattern}[1]$,那么可以得到 $x$ 个 $\textit{pattern}$ 子序列。因此,我们将答案加上 $x$ 和 $y$ 中的较大值即可。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{text}$ 的长度。空间复杂度 $O(1)$。 @@ -76,13 +87,14 @@ tags: ```python class Solution: def maximumSubsequenceCount(self, text: str, pattern: str) -> int: - ans = 0 - cnt = Counter() + ans = x = y = 0 for c in text: if c == pattern[1]: - ans += cnt[pattern[0]] - cnt[c] += 1 - ans += max(cnt[pattern[0]], cnt[pattern[1]]) + y += 1 + ans += x + if c == pattern[0]: + x += 1 + ans += max(x, y) return ans ``` @@ -91,17 +103,18 @@ class Solution: ```java class Solution { public long maximumSubsequenceCount(String text, String pattern) { - int[] cnt = new int[26]; - char a = pattern.charAt(0); - char b = pattern.charAt(1); long ans = 0; - for (char c : text.toCharArray()) { - if (c == b) { - ans += cnt[a - 'a']; + int x = 0, y = 0; + for (int i = 0; i < text.length(); ++i) { + if (text.charAt(i) == pattern.charAt(1)) { + ++y; + ans += x; + } + if (text.charAt(i) == pattern.charAt(0)) { + ++x; } - cnt[c - 'a']++; } - ans += Math.max(cnt[a - 'a'], cnt[b - 'a']); + ans += Math.max(x, y); return ans; } } @@ -114,13 +127,17 @@ class Solution { public: long long maximumSubsequenceCount(string text, string pattern) { long long ans = 0; - char a = pattern[0], b = pattern[1]; - vector cnt(26); + int x = 0, y = 0; for (char& c : text) { - if (c == b) ans += cnt[a - 'a']; - cnt[c - 'a']++; + if (c == pattern[1]) { + ++y; + ans += x; + } + if (c == pattern[0]) { + ++x; + } } - ans += max(cnt[a - 'a'], cnt[b - 'a']); + ans += max(x, y); return ans; } }; @@ -129,19 +146,39 @@ public: #### Go ```go -func maximumSubsequenceCount(text string, pattern string) int64 { - ans := 0 - cnt := make([]int, 26) - a, b := pattern[0], pattern[1] - for i := range text { - c := text[i] - if c == b { - ans += cnt[a-'a'] +func maximumSubsequenceCount(text string, pattern string) (ans int64) { + x, y := 0, 0 + for _, c := range text { + if byte(c) == pattern[1] { + y++ + ans += int64(x) + } + if byte(c) == pattern[0] { + x++ } - cnt[c-'a']++ } - ans += max(cnt[a-'a'], cnt[b-'a']) - return int64(ans) + ans += int64(max(x, y)) + return +} +``` + +#### TypeScript + +```ts +function maximumSubsequenceCount(text: string, pattern: string): number { + let ans = 0; + let [x, y] = [0, 0]; + for (const c of text) { + if (c === pattern[1]) { + ++y; + ans += x; + } + if (c === pattern[0]) { + ++x; + } + } + ans += Math.max(x, y); + return ans; } ``` diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md index 4665b50fca99..e8c6f8e4a2db 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/README_EN.md @@ -65,7 +65,18 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo -### Solution 1 +### Solution 1: Traversal + Counting + +We can use two variables $x$ and $y$ to record the current counts of $\textit{pattern}[0]$ and $\textit{pattern}[1]$ in the string, respectively. + +Then, traverse the string $\textit{text}$. For the current character $c$: + +- If $c$ equals $\textit{pattern}[1]$, increment $y$ by one. At this point, all previously encountered $\textit{pattern}[0]$ can form a $\textit{pattern}$ subsequence with the current $c$, so add $x$ to the answer. +- If $c$ equals $\textit{pattern}[0]$, increment $x$ by one. + +After the traversal, since we can insert one character, if we add $\textit{pattern}[0]$ at the beginning of the string, we can get $y$ $\textit{pattern}$ subsequences. If we add $\textit{pattern}[1]$ at the end of the string, we can get $x$ $\textit{pattern}$ subsequences. Therefore, we add the larger value of $x$ and $y$ to the answer. + +The time complexity is $O(n)$, where $n$ is the length of the string $\textit{text}$. The space complexity is $O(1)$. @@ -74,13 +85,14 @@ Some of the strings which can be obtained from text and have 6 subsequences &quo ```python class Solution: def maximumSubsequenceCount(self, text: str, pattern: str) -> int: - ans = 0 - cnt = Counter() + ans = x = y = 0 for c in text: if c == pattern[1]: - ans += cnt[pattern[0]] - cnt[c] += 1 - ans += max(cnt[pattern[0]], cnt[pattern[1]]) + y += 1 + ans += x + if c == pattern[0]: + x += 1 + ans += max(x, y) return ans ``` @@ -89,17 +101,18 @@ class Solution: ```java class Solution { public long maximumSubsequenceCount(String text, String pattern) { - int[] cnt = new int[26]; - char a = pattern.charAt(0); - char b = pattern.charAt(1); long ans = 0; - for (char c : text.toCharArray()) { - if (c == b) { - ans += cnt[a - 'a']; + int x = 0, y = 0; + for (int i = 0; i < text.length(); ++i) { + if (text.charAt(i) == pattern.charAt(1)) { + ++y; + ans += x; + } + if (text.charAt(i) == pattern.charAt(0)) { + ++x; } - cnt[c - 'a']++; } - ans += Math.max(cnt[a - 'a'], cnt[b - 'a']); + ans += Math.max(x, y); return ans; } } @@ -112,13 +125,17 @@ class Solution { public: long long maximumSubsequenceCount(string text, string pattern) { long long ans = 0; - char a = pattern[0], b = pattern[1]; - vector cnt(26); + int x = 0, y = 0; for (char& c : text) { - if (c == b) ans += cnt[a - 'a']; - cnt[c - 'a']++; + if (c == pattern[1]) { + ++y; + ans += x; + } + if (c == pattern[0]) { + ++x; + } } - ans += max(cnt[a - 'a'], cnt[b - 'a']); + ans += max(x, y); return ans; } }; @@ -127,19 +144,39 @@ public: #### Go ```go -func maximumSubsequenceCount(text string, pattern string) int64 { - ans := 0 - cnt := make([]int, 26) - a, b := pattern[0], pattern[1] - for i := range text { - c := text[i] - if c == b { - ans += cnt[a-'a'] +func maximumSubsequenceCount(text string, pattern string) (ans int64) { + x, y := 0, 0 + for _, c := range text { + if byte(c) == pattern[1] { + y++ + ans += int64(x) + } + if byte(c) == pattern[0] { + x++ } - cnt[c-'a']++ } - ans += max(cnt[a-'a'], cnt[b-'a']) - return int64(ans) + ans += int64(max(x, y)) + return +} +``` + +#### TypeScript + +```ts +function maximumSubsequenceCount(text: string, pattern: string): number { + let ans = 0; + let [x, y] = [0, 0]; + for (const c of text) { + if (c === pattern[1]) { + ++y; + ans += x; + } + if (c === pattern[0]) { + ++x; + } + } + ans += Math.max(x, y); + return ans; } ``` diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp index fcd9989338aa..aa788ca1cf81 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp @@ -2,13 +2,17 @@ class Solution { public: long long maximumSubsequenceCount(string text, string pattern) { long long ans = 0; - char a = pattern[0], b = pattern[1]; - vector cnt(26); + int x = 0, y = 0; for (char& c : text) { - if (c == b) ans += cnt[a - 'a']; - cnt[c - 'a']++; + if (c == pattern[1]) { + ++y; + ans += x; + } + if (c == pattern[0]) { + ++x; + } } - ans += max(cnt[a - 'a'], cnt[b - 'a']); + ans += max(x, y); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.go b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.go index 1a419f303fd8..640c0f8a3337 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.go +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.go @@ -1,14 +1,14 @@ -func maximumSubsequenceCount(text string, pattern string) int64 { - ans := 0 - cnt := make([]int, 26) - a, b := pattern[0], pattern[1] - for i := range text { - c := text[i] - if c == b { - ans += cnt[a-'a'] +func maximumSubsequenceCount(text string, pattern string) (ans int64) { + x, y := 0, 0 + for _, c := range text { + if byte(c) == pattern[1] { + y++ + ans += int64(x) + } + if byte(c) == pattern[0] { + x++ } - cnt[c-'a']++ } - ans += max(cnt[a-'a'], cnt[b-'a']) - return int64(ans) -} \ No newline at end of file + ans += int64(max(x, y)) + return +} diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.java b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.java index 1a1dcd5db2d5..8e2d507c0842 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.java +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.java @@ -1,16 +1,17 @@ class Solution { public long maximumSubsequenceCount(String text, String pattern) { - int[] cnt = new int[26]; - char a = pattern.charAt(0); - char b = pattern.charAt(1); long ans = 0; - for (char c : text.toCharArray()) { - if (c == b) { - ans += cnt[a - 'a']; + int x = 0, y = 0; + for (int i = 0; i < text.length(); ++i) { + if (text.charAt(i) == pattern.charAt(1)) { + ++y; + ans += x; + } + if (text.charAt(i) == pattern.charAt(0)) { + ++x; } - cnt[c - 'a']++; } - ans += Math.max(cnt[a - 'a'], cnt[b - 'a']); + ans += Math.max(x, y); return ans; } -} \ No newline at end of file +} diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.py b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.py index f745bf71c61b..d6be38300786 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.py +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.py @@ -1,10 +1,11 @@ class Solution: def maximumSubsequenceCount(self, text: str, pattern: str) -> int: - ans = 0 - cnt = Counter() + ans = x = y = 0 for c in text: if c == pattern[1]: - ans += cnt[pattern[0]] - cnt[c] += 1 - ans += max(cnt[pattern[0]], cnt[pattern[1]]) + y += 1 + ans += x + if c == pattern[0]: + x += 1 + ans += max(x, y) return ans diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.ts b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.ts new file mode 100644 index 000000000000..9c2186fe785f --- /dev/null +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.ts @@ -0,0 +1,15 @@ +function maximumSubsequenceCount(text: string, pattern: string): number { + let ans = 0; + let [x, y] = [0, 0]; + for (const c of text) { + if (c === pattern[1]) { + ++y; + ans += x; + } + if (c === pattern[0]) { + ++x; + } + } + ans += Math.max(x, y); + return ans; +}