File tree Expand file tree Collapse file tree 1 file changed +11
-23
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses Expand file tree Collapse file tree 1 file changed +11
-23
lines changed Original file line number Diff line number Diff line change 1
1
function reverseParentheses ( s : string ) : string {
2
- const n = s . length ;
3
- const d = new Array ( n ) . fill ( 0 ) ;
4
- const stk : number [ ] = [ ] ;
5
- for ( let i = 0 ; i < n ; ++ i ) {
6
- if ( s [ i ] === '(' ) {
7
- stk . push ( i ) ;
8
- } else if ( s [ i ] === ')' ) {
9
- const j = stk . pop ( ) ! ;
10
- d [ i ] = j ;
11
- d [ j ] = i ;
12
- }
13
- }
14
- let i = 0 ;
15
- let x = 1 ;
16
- const ans : string [ ] = [ ] ;
17
- while ( i < n ) {
18
- const c = s . charAt ( i ) ;
19
- if ( c === '(' || c === ')' ) {
20
- i = d [ i ] ;
21
- x = - x ;
2
+ const stk : string [ ] = [ ] ;
3
+ for ( const c of s ) {
4
+ if ( c === ')' ) {
5
+ const t : string [ ] = [ ] ;
6
+ while ( stk . at ( - 1 ) ! !== '(' ) {
7
+ t . push ( stk . pop ( ) ! ) ;
8
+ }
9
+ stk . pop ( ) ;
10
+ stk . push ( ...t ) ;
22
11
} else {
23
- ans . push ( c ) ;
12
+ stk . push ( c ) ;
24
13
}
25
- i += x ;
26
14
}
27
- return ans . join ( '' ) ;
15
+ return stk . join ( '' ) ;
28
16
}
You can’t perform that action at this time.
0 commit comments