File tree Expand file tree Collapse file tree 4 files changed +196
-0
lines changed
solution/2900-2999/2981.Find Longest Special Substring That Occurs Thrice I Expand file tree Collapse file tree 4 files changed +196
-0
lines changed Original file line number Diff line number Diff line change @@ -269,4 +269,76 @@ function maximumLength(s: string): number {
269269
270270<!-- solution: end -->
271271
272+ <!-- solution: start -->
273+
274+ ### Solution 2: Counting
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+ const vals = [... cnt .entries ()].filter (([, c ]) => c >= 3 ).map (([x ]) => x .length );
303+ const res = Math .max (... vals );
304+
305+ return res === Number .NEGATIVE_INFINITY ? - 1 : res ;
306+ }
307+ ```
308+
309+ ### JavaScript
310+
311+ ``` js
312+ function maximumLength (s ) {
313+ const cnt = new Map ();
314+ const n = s .length ;
315+ let [c, ch] = [0 , ' ' ];
316+
317+ for (let i = 0 ; i < n + 1 ; i++ ) {
318+ if (ch === s[i]) {
319+ c++ ;
320+ } else {
321+ let j = 1 ;
322+ while (c) {
323+ const char = ch .repeat (j++ );
324+ cnt .set (char, (cnt .get (char) ?? 0 ) + c);
325+ c-- ;
326+ }
327+
328+ ch = s[i];
329+ c = 1 ;
330+ }
331+ }
332+
333+ const vals = [... cnt .entries ()].filter (([, c ]) => c >= 3 ).map (([x ]) => x .length );
334+ const res = Math .max (... vals);
335+
336+ return res === Number .NEGATIVE_INFINITY ? - 1 : res;
337+ }
338+ ` ` `
339+
340+ <!-- tabs:end -->
341+
342+ <!-- solution:end -->
343+
272344<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -267,4 +267,76 @@ function maximumLength(s: string): number {
267267
268268<!-- solution: end -->
269269
270+ <!-- solution: start -->
271+
272+ ### Solution 2: Counting
273+
274+ <!-- tabs: start -->
275+
276+ #### TypeScript
277+
278+ ``` ts
279+ function maximumLength(s : string ): number {
280+ const cnt = new Map <string , number >();
281+ const n = s .length ;
282+ let [c, ch] = [0 , ' ' ];
283+
284+ for (let i = 0 ; i < n + 1 ; i ++ ) {
285+ if (ch === s [i ]) {
286+ c ++ ;
287+ } else {
288+ let j = 1 ;
289+ while (c ) {
290+ const char = ch .repeat (j ++ );
291+ cnt .set (char , (cnt .get (char ) ?? 0 ) + c );
292+ c -- ;
293+ }
294+
295+ ch = s [i ];
296+ c = 1 ;
297+ }
298+ }
299+
300+ const vals = [... cnt .entries ()].filter (([, c ]) => c >= 3 ).map (([x ]) => x .length );
301+ const res = Math .max (... vals );
302+
303+ return res === Number .NEGATIVE_INFINITY ? - 1 : res ;
304+ }
305+ ```
306+
307+ ### JavaScript
308+
309+ ``` js
310+ function maximumLength (s ) {
311+ const cnt = new Map ();
312+ const n = s .length ;
313+ let [c, ch] = [0 , ' ' ];
314+
315+ for (let i = 0 ; i < n + 1 ; i++ ) {
316+ if (ch === s[i]) {
317+ c++ ;
318+ } else {
319+ let j = 1 ;
320+ while (c) {
321+ const char = ch .repeat (j++ );
322+ cnt .set (char, (cnt .get (char) ?? 0 ) + c);
323+ c-- ;
324+ }
325+
326+ ch = s[i];
327+ c = 1 ;
328+ }
329+ }
330+
331+ const vals = [... cnt .entries ()].filter (([, c ]) => c >= 3 ).map (([x ]) => x .length );
332+ const res = Math .max (... vals);
333+
334+ return res === Number .NEGATIVE_INFINITY ? - 1 : res;
335+ }
336+ ` ` `
337+
338+ <!-- tabs:end -->
339+
340+ <!-- solution:end -->
341+
270342<!-- 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+ const vals = [ ...cnt . entries ( ) ] . filter ( ( [ , c ] ) => c >= 3 ) . map ( ( [ x ] ) => x . length ) ;
23+ const res = Math . max ( ...vals ) ;
24+
25+ return res === Number . NEGATIVE_INFINITY ? - 1 : res ;
26+ }
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+ const vals = [ ...cnt . entries ( ) ] . filter ( ( [ , c ] ) => c >= 3 ) . map ( ( [ x ] ) => x . length ) ;
23+ const res = Math . max ( ...vals ) ;
24+
25+ return res === Number . NEGATIVE_INFINITY ? - 1 : res ;
26+ }
You can’t perform that action at this time.
0 commit comments