@@ -75,32 +75,201 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3628.Ma
75
75
76
76
<!-- solution:start -->
77
77
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)$。
79
93
80
94
<!-- tabs:start -->
81
95
82
96
#### Python3
83
97
84
98
``` 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
86
120
```
87
121
88
122
#### Java
89
123
90
124
``` 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
+ }
92
162
```
93
163
94
164
#### C++
95
165
96
166
``` 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
+ };
98
197
```
99
198
100
199
#### Go
101
200
102
201
``` 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
+ ```
103
241
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
+ }
104
273
```
105
274
106
275
<!-- tabs:end -->
0 commit comments