File tree 4 files changed +224
-0
lines changed
solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I
4 files changed +224
-0
lines changed Original file line number Diff line number Diff line change @@ -269,4 +269,86 @@ function maximumLength(s: string): number {
269
269
270
270
<!-- solution: end -->
271
271
272
+ <!-- solution: start -->
273
+
274
+ ### 方法二:计数
275
+
276
+ 时间复杂度 $O(n)$。
277
+
278
+ <!-- tabs: start -->
279
+
280
+ #### TypeScript
281
+
282
+ ``` ts
283
+ function maximumLength(s : string ): number {
284
+ const cnt = new Map <string , number >();
285
+ const n = s .length ;
286
+ let [c, ch] = [0 , ' ' ];
287
+
288
+ for (let i = 0 ; i < n + 1 ; i ++ ) {
289
+ if (ch === s [i ]) {
290
+ c ++ ;
291
+ } else {
292
+ let j = 1 ;
293
+ while (c ) {
294
+ const char = ch .repeat (j ++ );
295
+ cnt .set (char , (cnt .get (char ) ?? 0 ) + c );
296
+ c -- ;
297
+ }
298
+
299
+ ch = s [i ];
300
+ c = 1 ;
301
+ }
302
+ }
303
+
304
+ let res = - 1 ;
305
+ for (const [x, c] of cnt ) {
306
+ if (c >= 3 ) {
307
+ res = Math .max (res , x .length );
308
+ }
309
+ }
310
+
311
+ return res ;
312
+ }
313
+ ```
314
+
315
+ ### JavaScript
316
+
317
+ ``` js
318
+ function maximumLength (s ) {
319
+ const cnt = new Map ();
320
+ const n = s .length ;
321
+ let [c, ch] = [0 , ' ' ];
322
+
323
+ for (let i = 0 ; i < n + 1 ; i++ ) {
324
+ if (ch === s[i]) {
325
+ c++ ;
326
+ } else {
327
+ let j = 1 ;
328
+ while (c) {
329
+ const char = ch .repeat (j++ );
330
+ cnt .set (char, (cnt .get (char) ?? 0 ) + c);
331
+ c-- ;
332
+ }
333
+
334
+ ch = s[i];
335
+ c = 1 ;
336
+ }
337
+ }
338
+
339
+ let res = - 1 ;
340
+ for (const [x , c ] of cnt) {
341
+ if (c >= 3 ) {
342
+ res = Math .max (res, x .length );
343
+ }
344
+ }
345
+
346
+ return res;
347
+ }
348
+ ` ` `
349
+
350
+ <!-- tabs:end -->
351
+
352
+ <!-- solution:end -->
353
+
272
354
<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -267,4 +267,86 @@ function maximumLength(s: string): number {
267
267
268
268
<!-- solution: end -->
269
269
270
+ <!-- solution: start -->
271
+
272
+ ### Solution 2: Counting
273
+
274
+ The time complexity is $O(n)$
275
+
276
+ <!-- tabs: start -->
277
+
278
+ #### TypeScript
279
+
280
+ ``` ts
281
+ function maximumLength(s : string ): number {
282
+ const cnt = new Map <string , number >();
283
+ const n = s .length ;
284
+ let [c, ch] = [0 , ' ' ];
285
+
286
+ for (let i = 0 ; i < n + 1 ; i ++ ) {
287
+ if (ch === s [i ]) {
288
+ c ++ ;
289
+ } else {
290
+ let j = 1 ;
291
+ while (c ) {
292
+ const char = ch .repeat (j ++ );
293
+ cnt .set (char , (cnt .get (char ) ?? 0 ) + c );
294
+ c -- ;
295
+ }
296
+
297
+ ch = s [i ];
298
+ c = 1 ;
299
+ }
300
+ }
301
+
302
+ let res = - 1 ;
303
+ for (const [x, c] of cnt ) {
304
+ if (c >= 3 ) {
305
+ res = Math .max (res , x .length );
306
+ }
307
+ }
308
+
309
+ return res ;
310
+ }
311
+ ```
312
+
313
+ ### JavaScript
314
+
315
+ ``` js
316
+ function maximumLength (s ) {
317
+ const cnt = new Map ();
318
+ const n = s .length ;
319
+ let [c, ch] = [0 , ' ' ];
320
+
321
+ for (let i = 0 ; i < n + 1 ; i++ ) {
322
+ if (ch === s[i]) {
323
+ c++ ;
324
+ } else {
325
+ let j = 1 ;
326
+ while (c) {
327
+ const char = ch .repeat (j++ );
328
+ cnt .set (char, (cnt .get (char) ?? 0 ) + c);
329
+ c-- ;
330
+ }
331
+
332
+ ch = s[i];
333
+ c = 1 ;
334
+ }
335
+ }
336
+
337
+ let res = - 1 ;
338
+ for (const [x , c ] of cnt) {
339
+ if (c >= 3 ) {
340
+ res = Math .max (res, x .length );
341
+ }
342
+ }
343
+
344
+ return res;
345
+ }
346
+ ` ` `
347
+
348
+ <!-- tabs:end -->
349
+
350
+ <!-- solution:end -->
351
+
270
352
<!-- problem:end -->
Original file line number Diff line number Diff line change
1
+ function maximumLength ( s ) {
2
+ const cnt = new Map ( ) ;
3
+ const n = s . length ;
4
+ let [ c , ch ] = [ 0 , '' ] ;
5
+
6
+ for ( let i = 0 ; i < n + 1 ; i ++ ) {
7
+ if ( ch === s [ i ] ) {
8
+ c ++ ;
9
+ } else {
10
+ let j = 1 ;
11
+ while ( c ) {
12
+ const char = ch . repeat ( j ++ ) ;
13
+ cnt . set ( char , ( cnt . get ( char ) ?? 0 ) + c ) ;
14
+ c -- ;
15
+ }
16
+
17
+ ch = s [ i ] ;
18
+ c = 1 ;
19
+ }
20
+ }
21
+
22
+ let res = - 1 ;
23
+ for ( const [ x , c ] of cnt ) {
24
+ if ( c >= 3 ) {
25
+ res = Math . max ( res , x . length ) ;
26
+ }
27
+ }
28
+
29
+ return res ;
30
+ }
Original file line number Diff line number Diff line change
1
+ function maximumLength ( s : string ) : number {
2
+ const cnt = new Map < string , number > ( ) ;
3
+ const n = s . length ;
4
+ let [ c , ch ] = [ 0 , '' ] ;
5
+
6
+ for ( let i = 0 ; i < n + 1 ; i ++ ) {
7
+ if ( ch === s [ i ] ) {
8
+ c ++ ;
9
+ } else {
10
+ let j = 1 ;
11
+ while ( c ) {
12
+ const char = ch . repeat ( j ++ ) ;
13
+ cnt . set ( char , ( cnt . get ( char ) ?? 0 ) + c ) ;
14
+ c -- ;
15
+ }
16
+
17
+ ch = s [ i ] ;
18
+ c = 1 ;
19
+ }
20
+ }
21
+
22
+ let res = - 1 ;
23
+ for ( const [ x , c ] of cnt ) {
24
+ if ( c >= 3 ) {
25
+ res = Math . max ( res , x . length ) ;
26
+ }
27
+ }
28
+
29
+ return res ;
30
+ }
You can’t perform that action at this time.
0 commit comments