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 @@ -357,6 +357,44 @@ func reverseParentheses(s string) string {
357
357
}
358
358
```
359
359
360
+ #### JavaScript
361
+
362
+ ``` js
363
+ function reverseParentheses (s : string ): string {
364
+ const res: string [] = [];
365
+ const n = s .length ;
366
+ const pairs = Array (n).fill (- 1 );
367
+ const stack: number [] = [];
368
+
369
+ for (let i = 0 ; i < n; i++ ) {
370
+ if (s[i] === ' (' ) stack .push (i);
371
+ else if (s[i] === ' )' ) {
372
+ const j = stack .pop ()! ;
373
+ pairs[i] = j;
374
+ pairs[j] = i;
375
+ }
376
+ }
377
+
378
+ for (let i = 0 , forward = true ; i < n; ) {
379
+ const ch = s[i];
380
+
381
+ switch (s[i]) {
382
+ case ' (' :
383
+ case ' )' :
384
+ i = forward ? pairs[i] - 1 : pairs[i] + 1 ;
385
+ forward = ! forward;
386
+ break ;
387
+
388
+ default :
389
+ res .push (ch);
390
+ i = forward ? i + 1 : i - 1 ;
391
+ }
392
+ }
393
+
394
+ return res .join (' ' );
395
+ }
396
+ ```
397
+
360
398
<!-- tabs: end -->
361
399
362
400
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -350,6 +350,44 @@ func reverseParentheses(s string) string {
350
350
}
351
351
```
352
352
353
+ #### JavaScript
354
+
355
+ ``` js
356
+ function reverseParentheses (s : string ): string {
357
+ const res: string [] = [];
358
+ const n = s .length ;
359
+ const pairs = Array (n).fill (- 1 );
360
+ const stack: number [] = [];
361
+
362
+ for (let i = 0 ; i < n; i++ ) {
363
+ if (s[i] === ' (' ) stack .push (i);
364
+ else if (s[i] === ' )' ) {
365
+ const j = stack .pop ()! ;
366
+ pairs[i] = j;
367
+ pairs[j] = i;
368
+ }
369
+ }
370
+
371
+ for (let i = 0 , forward = true ; i < n; ) {
372
+ const ch = s[i];
373
+
374
+ switch (s[i]) {
375
+ case ' (' :
376
+ case ' )' :
377
+ i = forward ? pairs[i] - 1 : pairs[i] + 1 ;
378
+ forward = ! forward;
379
+ break ;
380
+
381
+ default :
382
+ res .push (ch);
383
+ i = forward ? i + 1 : i - 1 ;
384
+ }
385
+ }
386
+
387
+ return res .join (' ' );
388
+ }
389
+ ```
390
+
353
391
<!-- tabs: end -->
354
392
355
393
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function reverseParentheses ( s ) {
2
+ const res = [ ] ;
3
+ const n = s . length ;
4
+ const pairs = Array ( n ) . fill ( - 1 ) ;
5
+ const stack = [ ] ;
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