File tree Expand file tree Collapse file tree 3 files changed +109
-0
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses Expand file tree Collapse file tree 3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -395,6 +395,44 @@ function reverseParentheses(s: string): string {
395
395
}
396
396
```
397
397
398
+ #### TypeScript
399
+
400
+ ``` ts
401
+ function reverseParentheses(s : string ): string {
402
+ const res: string [] = [];
403
+ const n = s .length ;
404
+ const pairs = Array (n ).fill (- 1 );
405
+ const stack: number [] = [];
406
+
407
+ for (let i = 0 ; i < n ; i ++ ) {
408
+ if (s [i ] === ' (' ) stack .push (i );
409
+ else if (s [i ] === ' )' ) {
410
+ const j = stack .pop ()! ;
411
+ pairs [i ] = j ;
412
+ pairs [j ] = i ;
413
+ }
414
+ }
415
+
416
+ for (let i = 0 , forward = true ; i < n ; ) {
417
+ const ch = s [i ];
418
+
419
+ switch (s [i ]) {
420
+ case ' (' :
421
+ case ' )' :
422
+ i = forward ? pairs [i ] - 1 : pairs [i ] + 1 ;
423
+ forward = ! forward ;
424
+ break ;
425
+
426
+ default :
427
+ res .push (ch );
428
+ i = forward ? i + 1 : i - 1 ;
429
+ }
430
+ }
431
+
432
+ return res .join (' ' );
433
+ }
434
+ ```
435
+
398
436
<!-- tabs: end -->
399
437
400
438
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -388,6 +388,44 @@ function reverseParentheses(s: string): string {
388
388
}
389
389
```
390
390
391
+ #### TypeScript
392
+
393
+ ``` ts
394
+ function reverseParentheses(s : string ): string {
395
+ const res: string [] = [];
396
+ const n = s .length ;
397
+ const pairs = Array (n ).fill (- 1 );
398
+ const stack: number [] = [];
399
+
400
+ for (let i = 0 ; i < n ; i ++ ) {
401
+ if (s [i ] === ' (' ) stack .push (i );
402
+ else if (s [i ] === ' )' ) {
403
+ const j = stack .pop ()! ;
404
+ pairs [i ] = j ;
405
+ pairs [j ] = i ;
406
+ }
407
+ }
408
+
409
+ for (let i = 0 , forward = true ; i < n ; ) {
410
+ const ch = s [i ];
411
+
412
+ switch (s [i ]) {
413
+ case ' (' :
414
+ case ' )' :
415
+ i = forward ? pairs [i ] - 1 : pairs [i ] + 1 ;
416
+ forward = ! forward ;
417
+ break ;
418
+
419
+ default :
420
+ res .push (ch );
421
+ i = forward ? i + 1 : i - 1 ;
422
+ }
423
+ }
424
+
425
+ return res .join (' ' );
426
+ }
427
+ ```
428
+
391
429
<!-- tabs: end -->
392
430
393
431
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function reverseParentheses ( s : string ) : string {
2
+ const res : string [ ] = [ ] ;
3
+ const n = s . length ;
4
+ const pairs = Array ( n ) . fill ( - 1 ) ;
5
+ const stack : number [ ] = [ ] ;
6
+
7
+ for ( let i = 0 ; i < n ; i ++ ) {
8
+ if ( s [ i ] === '(' ) stack . push ( i ) ;
9
+ else if ( s [ i ] === ')' ) {
10
+ const j = stack . pop ( ) ! ;
11
+ pairs [ i ] = j ;
12
+ pairs [ j ] = i ;
13
+ }
14
+ }
15
+
16
+ for ( let i = 0 , forward = true ; i < n ; ) {
17
+ const ch = s [ i ] ;
18
+
19
+ switch ( s [ i ] ) {
20
+ case '(' :
21
+ case ')' :
22
+ i = forward ? pairs [ i ] - 1 : pairs [ i ] + 1 ;
23
+ forward = ! forward ;
24
+ break ;
25
+
26
+ default :
27
+ res . push ( ch ) ;
28
+ i = forward ? i + 1 : i - 1 ;
29
+ }
30
+ }
31
+
32
+ return res . join ( '' ) ;
33
+ }
You can’t perform that action at this time.
0 commit comments