diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README.md b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README.md index af66ac7057c2a..4123e55191dbb 100644 --- a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README.md +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README.md @@ -75,32 +75,201 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3628.Ma -### 方法一 +### 方法一:枚举 + +我们可以先计算出原字符串中 "LCT" 的子序列数量,然后考虑插入一个字母的情况。 + +计算 "LCT" 子序列的数量可以通过遍历字符串来实现。我们可以枚举中间的 "C",用两个变量 $l$ 和 $r$ 分别维护左右两侧的 "L" 和 "T" 的数量。对于每个 "C",我们可以计算出它左侧的 "L" 的数量和右侧的 "T" 的数量,从而得到以该 "C" 为中间的 "LCT" 子序列数量为 $l \times r$,累加到总数中。 + +接下来,我们需要考虑插入一个字母的情况。考虑到插入一个 "L" 或 "C" 或 "T" 的情况: + +- 插入一个 "L",那么我们只需要统计原字符串中 "CT" 的子序列数量。 +- 插入一个 "T",那么我们只需要统计原字符串中 "LC" 的子序列数量。 +- 插入一个 "C",那么我们只需要统计原字符串中 "LT" 的子序列数量,这种情况下,我们可以在前面枚举的过程中,维护一个变量 $\textit{mx}$,表示当前最大的 $l \times r$ 的值。 + +最后,我们将原字符串中 "LCT" 的子序列数量加上插入一个字母后的最大子序列数量,得到最终结果。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def numOfSubsequences(self, s: str) -> int: + def calc(t: str) -> int: + cnt = a = 0 + for c in s: + if c == t[1]: + cnt += a + a += int(c == t[0]) + return cnt + + l, r = 0, s.count("T") + ans = mx = 0 + for c in s: + r -= int(c == "T") + if c == "C": + ans += l * r + l += int(c == "L") + mx = max(mx, l * r) + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans ``` #### Java ```java - +class Solution { + private char[] s; + + public long numOfSubsequences(String S) { + s = S.toCharArray(); + int l = 0, r = 0; + for (char c : s) { + if (c == 'T') { + ++r; + } + } + long ans = 0, mx = 0; + for (char c : s) { + r -= c == 'T' ? 1 : 0; + if (c == 'C') { + ans += 1L * l * r; + } + l += c == 'L' ? 1 : 0; + mx = Math.max(mx, 1L * l * r); + } + mx = Math.max(mx, Math.max(calc("LC"), calc("CT"))); + ans += mx; + return ans; + } + + private long calc(String t) { + long cnt = 0; + int a = 0; + for (char c : s) { + if (c == t.charAt(1)) { + cnt += a; + } + a += c == t.charAt(0) ? 1 : 0; + } + return cnt; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long numOfSubsequences(string s) { + auto calc = [&](string t) { + long long cnt = 0, a = 0; + for (char c : s) { + if (c == t[1]) { + cnt += a; + } + a += (c == t[0]); + } + return cnt; + }; + + long long l = 0, r = count(s.begin(), s.end(), 'T'); + long long ans = 0, mx = 0; + for (char c : s) { + r -= (c == 'T'); + if (c == 'C') { + ans += l * r; + } + l += (c == 'L'); + mx = max(mx, l * r); + } + mx = max(mx, calc("LC")); + mx = max(mx, calc("CT")); + ans += mx; + return ans; + } +}; ``` #### Go ```go +func numOfSubsequences(s string) int64 { + calc := func(t string) int64 { + cnt, a := int64(0), int64(0) + for _, c := range s { + if c == rune(t[1]) { + cnt += a + } + if c == rune(t[0]) { + a++ + } + } + return cnt + } + + l, r := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r++ + } + } + + ans, mx := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r-- + } + if c == 'C' { + ans += l * r + } + if c == 'L' { + l++ + } + mx = max(mx, l*r) + } + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans +} +``` +#### TypeScript + +```ts +function numOfSubsequences(s: string): number { + const calc = (t: string): number => { + let [cnt, a] = [0, 0]; + for (const c of s) { + if (c === t[1]) cnt += a; + if (c === t[0]) a++; + } + return cnt; + }; + + let [l, r] = [0, 0]; + for (const c of s) { + if (c === 'T') r++; + } + + let [ans, mx] = [0, 0]; + for (const c of s) { + if (c === 'T') r--; + if (c === 'C') ans += l * r; + if (c === 'L') l++; + mx = Math.max(mx, l * r); + } + + mx = Math.max(mx, calc('LC')); + mx = Math.max(mx, calc('CT')); + ans += mx; + return ans; +} ``` diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README_EN.md b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README_EN.md index 2804876ae2896..8fed5e0a2150e 100644 --- a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README_EN.md +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README_EN.md @@ -73,32 +73,201 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3628.Ma -### Solution 1 +### Solution 1: Enumeration + +We can first calculate the number of "LCT" subsequences in the original string, then consider the case of inserting one letter. + +The number of "LCT" subsequences can be calculated by traversing the string. We can enumerate the middle "C" and use two variables $l$ and $r$ to maintain the counts of "L" on the left and "T" on the right respectively. For each "C", we can calculate the number of "L"s on its left and the number of "T"s on its right, thus obtaining the number of "LCT" subsequences with this "C" as the middle character as $l \times r$, and accumulate it to the total count. + +Next, we need to consider the case of inserting one letter. Consider inserting an "L", "C", or "T": + +- Insert an "L": we only need to count the number of "CT" subsequences in the original string. +- Insert a "T": we only need to count the number of "LC" subsequences in the original string. +- Insert a "C": we only need to count the number of "LT" subsequences in the original string. In this case, during the enumeration process above, we can maintain a variable $\textit{mx}$ representing the current maximum value of $l \times r$. + +Finally, we add the number of "LCT" subsequences in the original string to the maximum number of subsequences after inserting one letter to get the final result. + +The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def numOfSubsequences(self, s: str) -> int: + def calc(t: str) -> int: + cnt = a = 0 + for c in s: + if c == t[1]: + cnt += a + a += int(c == t[0]) + return cnt + + l, r = 0, s.count("T") + ans = mx = 0 + for c in s: + r -= int(c == "T") + if c == "C": + ans += l * r + l += int(c == "L") + mx = max(mx, l * r) + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans ``` #### Java ```java - +class Solution { + private char[] s; + + public long numOfSubsequences(String S) { + s = S.toCharArray(); + int l = 0, r = 0; + for (char c : s) { + if (c == 'T') { + ++r; + } + } + long ans = 0, mx = 0; + for (char c : s) { + r -= c == 'T' ? 1 : 0; + if (c == 'C') { + ans += 1L * l * r; + } + l += c == 'L' ? 1 : 0; + mx = Math.max(mx, 1L * l * r); + } + mx = Math.max(mx, Math.max(calc("LC"), calc("CT"))); + ans += mx; + return ans; + } + + private long calc(String t) { + long cnt = 0; + int a = 0; + for (char c : s) { + if (c == t.charAt(1)) { + cnt += a; + } + a += c == t.charAt(0) ? 1 : 0; + } + return cnt; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long numOfSubsequences(string s) { + auto calc = [&](string t) { + long long cnt = 0, a = 0; + for (char c : s) { + if (c == t[1]) { + cnt += a; + } + a += (c == t[0]); + } + return cnt; + }; + + long long l = 0, r = count(s.begin(), s.end(), 'T'); + long long ans = 0, mx = 0; + for (char c : s) { + r -= (c == 'T'); + if (c == 'C') { + ans += l * r; + } + l += (c == 'L'); + mx = max(mx, l * r); + } + mx = max(mx, calc("LC")); + mx = max(mx, calc("CT")); + ans += mx; + return ans; + } +}; ``` #### Go ```go +func numOfSubsequences(s string) int64 { + calc := func(t string) int64 { + cnt, a := int64(0), int64(0) + for _, c := range s { + if c == rune(t[1]) { + cnt += a + } + if c == rune(t[0]) { + a++ + } + } + return cnt + } + + l, r := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r++ + } + } + + ans, mx := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r-- + } + if c == 'C' { + ans += l * r + } + if c == 'L' { + l++ + } + mx = max(mx, l*r) + } + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans +} +``` +#### TypeScript + +```ts +function numOfSubsequences(s: string): number { + const calc = (t: string): number => { + let [cnt, a] = [0, 0]; + for (const c of s) { + if (c === t[1]) cnt += a; + if (c === t[0]) a++; + } + return cnt; + }; + + let [l, r] = [0, 0]; + for (const c of s) { + if (c === 'T') r++; + } + + let [ans, mx] = [0, 0]; + for (const c of s) { + if (c === 'T') r--; + if (c === 'C') ans += l * r; + if (c === 'L') l++; + mx = Math.max(mx, l * r); + } + + mx = Math.max(mx, calc('LC')); + mx = Math.max(mx, calc('CT')); + ans += mx; + return ans; +} ``` diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.cpp b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.cpp new file mode 100644 index 0000000000000..2c9f31c80a17c --- /dev/null +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + long long numOfSubsequences(string s) { + auto calc = [&](string t) { + long long cnt = 0, a = 0; + for (char c : s) { + if (c == t[1]) { + cnt += a; + } + a += (c == t[0]); + } + return cnt; + }; + + long long l = 0, r = count(s.begin(), s.end(), 'T'); + long long ans = 0, mx = 0; + for (char c : s) { + r -= (c == 'T'); + if (c == 'C') { + ans += l * r; + } + l += (c == 'L'); + mx = max(mx, l * r); + } + mx = max(mx, calc("LC")); + mx = max(mx, calc("CT")); + ans += mx; + return ans; + } +}; diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.go b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.go new file mode 100644 index 0000000000000..851040600877b --- /dev/null +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.go @@ -0,0 +1,38 @@ +func numOfSubsequences(s string) int64 { + calc := func(t string) int64 { + cnt, a := int64(0), int64(0) + for _, c := range s { + if c == rune(t[1]) { + cnt += a + } + if c == rune(t[0]) { + a++ + } + } + return cnt + } + + l, r := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r++ + } + } + + ans, mx := int64(0), int64(0) + for _, c := range s { + if c == 'T' { + r-- + } + if c == 'C' { + ans += l * r + } + if c == 'L' { + l++ + } + mx = max(mx, l*r) + } + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans +} \ No newline at end of file diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.java b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.java new file mode 100644 index 0000000000000..254d72d48452c --- /dev/null +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.java @@ -0,0 +1,37 @@ +class Solution { + private char[] s; + + public long numOfSubsequences(String S) { + s = S.toCharArray(); + int l = 0, r = 0; + for (char c : s) { + if (c == 'T') { + ++r; + } + } + long ans = 0, mx = 0; + for (char c : s) { + r -= c == 'T' ? 1 : 0; + if (c == 'C') { + ans += 1L * l * r; + } + l += c == 'L' ? 1 : 0; + mx = Math.max(mx, 1L * l * r); + } + mx = Math.max(mx, Math.max(calc("LC"), calc("CT"))); + ans += mx; + return ans; + } + + private long calc(String t) { + long cnt = 0; + int a = 0; + for (char c : s) { + if (c == t.charAt(1)) { + cnt += a; + } + a += c == t.charAt(0) ? 1 : 0; + } + return cnt; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.py b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.py new file mode 100644 index 0000000000000..1cb81d12f3554 --- /dev/null +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def numOfSubsequences(self, s: str) -> int: + def calc(t: str) -> int: + cnt = a = 0 + for c in s: + if c == t[1]: + cnt += a + a += int(c == t[0]) + return cnt + + l, r = 0, s.count("T") + ans = mx = 0 + for c in s: + r -= int(c == "T") + if c == "C": + ans += l * r + l += int(c == "L") + mx = max(mx, l * r) + mx = max(mx, calc("LC"), calc("CT")) + ans += mx + return ans diff --git a/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.ts b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.ts new file mode 100644 index 0000000000000..7c3fe6ecd3007 --- /dev/null +++ b/solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/Solution.ts @@ -0,0 +1,28 @@ +function numOfSubsequences(s: string): number { + const calc = (t: string): number => { + let [cnt, a] = [0, 0]; + for (const c of s) { + if (c === t[1]) cnt += a; + if (c === t[0]) a++; + } + return cnt; + }; + + let [l, r] = [0, 0]; + for (const c of s) { + if (c === 'T') r++; + } + + let [ans, mx] = [0, 0]; + for (const c of s) { + if (c === 'T') r--; + if (c === 'C') ans += l * r; + if (c === 'L') l++; + mx = Math.max(mx, l * r); + } + + mx = Math.max(mx, calc('LC')); + mx = Math.max(mx, calc('CT')); + ans += mx; + return ans; +}