Skip to content

Commit 1455c9a

Browse files
authored
feat: add solutions to lc problem: No.3628 (#4601)
No.3628.Maximum Number of Subsequences After One Inserting
1 parent 02c8054 commit 1455c9a

File tree

7 files changed

+500
-8
lines changed

7 files changed

+500
-8
lines changed

solution/3600-3699/3628.Maximum Number of Subsequences After One Inserting/README.md

Lines changed: 173 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,201 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3628.Ma
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:枚举
79+
80+
我们可以先计算出原字符串中 "LCT" 的子序列数量,然后考虑插入一个字母的情况。
81+
82+
计算 "LCT" 子序列的数量可以通过遍历字符串来实现。我们可以枚举中间的 "C",用两个变量 $l$ 和 $r$ 分别维护左右两侧的 "L" 和 "T" 的数量。对于每个 "C",我们可以计算出它左侧的 "L" 的数量和右侧的 "T" 的数量,从而得到以该 "C" 为中间的 "LCT" 子序列数量为 $l \times r$,累加到总数中。
83+
84+
接下来,我们需要考虑插入一个字母的情况。考虑到插入一个 "L" 或 "C" 或 "T" 的情况:
85+
86+
- 插入一个 "L",那么我们只需要统计原字符串中 "CT" 的子序列数量。
87+
- 插入一个 "T",那么我们只需要统计原字符串中 "LC" 的子序列数量。
88+
- 插入一个 "C",那么我们只需要统计原字符串中 "LT" 的子序列数量,这种情况下,我们可以在前面枚举的过程中,维护一个变量 $\textit{mx}$,表示当前最大的 $l \times r$ 的值。
89+
90+
最后,我们将原字符串中 "LCT" 的子序列数量加上插入一个字母后的最大子序列数量,得到最终结果。
91+
92+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
7993

8094
<!-- tabs:start -->
8195

8296
#### Python3
8397

8498
```python
85-
99+
class Solution:
100+
def numOfSubsequences(self, s: str) -> int:
101+
def calc(t: str) -> int:
102+
cnt = a = 0
103+
for c in s:
104+
if c == t[1]:
105+
cnt += a
106+
a += int(c == t[0])
107+
return cnt
108+
109+
l, r = 0, s.count("T")
110+
ans = mx = 0
111+
for c in s:
112+
r -= int(c == "T")
113+
if c == "C":
114+
ans += l * r
115+
l += int(c == "L")
116+
mx = max(mx, l * r)
117+
mx = max(mx, calc("LC"), calc("CT"))
118+
ans += mx
119+
return ans
86120
```
87121

88122
#### Java
89123

90124
```java
91-
125+
class Solution {
126+
private char[] s;
127+
128+
public long numOfSubsequences(String S) {
129+
s = S.toCharArray();
130+
int l = 0, r = 0;
131+
for (char c : s) {
132+
if (c == 'T') {
133+
++r;
134+
}
135+
}
136+
long ans = 0, mx = 0;
137+
for (char c : s) {
138+
r -= c == 'T' ? 1 : 0;
139+
if (c == 'C') {
140+
ans += 1L * l * r;
141+
}
142+
l += c == 'L' ? 1 : 0;
143+
mx = Math.max(mx, 1L * l * r);
144+
}
145+
mx = Math.max(mx, Math.max(calc("LC"), calc("CT")));
146+
ans += mx;
147+
return ans;
148+
}
149+
150+
private long calc(String t) {
151+
long cnt = 0;
152+
int a = 0;
153+
for (char c : s) {
154+
if (c == t.charAt(1)) {
155+
cnt += a;
156+
}
157+
a += c == t.charAt(0) ? 1 : 0;
158+
}
159+
return cnt;
160+
}
161+
}
92162
```
93163

94164
#### C++
95165

96166
```cpp
97-
167+
class Solution {
168+
public:
169+
long long numOfSubsequences(string s) {
170+
auto calc = [&](string t) {
171+
long long cnt = 0, a = 0;
172+
for (char c : s) {
173+
if (c == t[1]) {
174+
cnt += a;
175+
}
176+
a += (c == t[0]);
177+
}
178+
return cnt;
179+
};
180+
181+
long long l = 0, r = count(s.begin(), s.end(), 'T');
182+
long long ans = 0, mx = 0;
183+
for (char c : s) {
184+
r -= (c == 'T');
185+
if (c == 'C') {
186+
ans += l * r;
187+
}
188+
l += (c == 'L');
189+
mx = max(mx, l * r);
190+
}
191+
mx = max(mx, calc("LC"));
192+
mx = max(mx, calc("CT"));
193+
ans += mx;
194+
return ans;
195+
}
196+
};
98197
```
99198

100199
#### Go
101200

102201
```go
202+
func numOfSubsequences(s string) int64 {
203+
calc := func(t string) int64 {
204+
cnt, a := int64(0), int64(0)
205+
for _, c := range s {
206+
if c == rune(t[1]) {
207+
cnt += a
208+
}
209+
if c == rune(t[0]) {
210+
a++
211+
}
212+
}
213+
return cnt
214+
}
215+
216+
l, r := int64(0), int64(0)
217+
for _, c := range s {
218+
if c == 'T' {
219+
r++
220+
}
221+
}
222+
223+
ans, mx := int64(0), int64(0)
224+
for _, c := range s {
225+
if c == 'T' {
226+
r--
227+
}
228+
if c == 'C' {
229+
ans += l * r
230+
}
231+
if c == 'L' {
232+
l++
233+
}
234+
mx = max(mx, l*r)
235+
}
236+
mx = max(mx, calc("LC"), calc("CT"))
237+
ans += mx
238+
return ans
239+
}
240+
```
103241

242+
#### TypeScript
243+
244+
```ts
245+
function numOfSubsequences(s: string): number {
246+
const calc = (t: string): number => {
247+
let [cnt, a] = [0, 0];
248+
for (const c of s) {
249+
if (c === t[1]) cnt += a;
250+
if (c === t[0]) a++;
251+
}
252+
return cnt;
253+
};
254+
255+
let [l, r] = [0, 0];
256+
for (const c of s) {
257+
if (c === 'T') r++;
258+
}
259+
260+
let [ans, mx] = [0, 0];
261+
for (const c of s) {
262+
if (c === 'T') r--;
263+
if (c === 'C') ans += l * r;
264+
if (c === 'L') l++;
265+
mx = Math.max(mx, l * r);
266+
}
267+
268+
mx = Math.max(mx, calc('LC'));
269+
mx = Math.max(mx, calc('CT'));
270+
ans += mx;
271+
return ans;
272+
}
104273
```
105274

106275
<!-- tabs:end -->

0 commit comments

Comments
 (0)