File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,39 @@ var reverseParentheses = function (s) {
214
214
};
215
215
```
216
216
217
+ #### JavaScript
218
+
219
+ ``` ts
220
+ function reverseParentheses(s : string ): string {
221
+ const n = s .length ;
222
+ const d = new Array (n ).fill (0 );
223
+ const stk = [];
224
+ for (let i = 0 ; i < n ; ++ i ) {
225
+ if (s [i ] === ' (' ) {
226
+ stk .push (i );
227
+ } else if (s [i ] === ' )' ) {
228
+ const j = stk .pop ()! ;
229
+ d [i ] = j ;
230
+ d [j ] = i ;
231
+ }
232
+ }
233
+ let i = 0 ;
234
+ let x = 1 ;
235
+ const ans = [];
236
+ while (i < n ) {
237
+ const c = s .charAt (i );
238
+ if (c === ' (' || c === ' )' ) {
239
+ i = d [i ];
240
+ x = - x ;
241
+ } else {
242
+ ans .push (c );
243
+ }
244
+ i += x ;
245
+ }
246
+ return ans .join (' ' );
247
+ }
248
+ ```
249
+
217
250
<!-- tabs: end -->
218
251
219
252
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -207,6 +207,39 @@ var reverseParentheses = function (s) {
207
207
};
208
208
```
209
209
210
+ #### JavaScript
211
+
212
+ ``` ts
213
+ function reverseParentheses(s : string ): string {
214
+ const n = s .length ;
215
+ const d = new Array (n ).fill (0 );
216
+ const stk = [];
217
+ for (let i = 0 ; i < n ; ++ i ) {
218
+ if (s [i ] === ' (' ) {
219
+ stk .push (i );
220
+ } else if (s [i ] === ' )' ) {
221
+ const j = stk .pop ()! ;
222
+ d [i ] = j ;
223
+ d [j ] = i ;
224
+ }
225
+ }
226
+ let i = 0 ;
227
+ let x = 1 ;
228
+ const ans = [];
229
+ while (i < n ) {
230
+ const c = s .charAt (i );
231
+ if (c === ' (' || c === ' )' ) {
232
+ i = d [i ];
233
+ x = - x ;
234
+ } else {
235
+ ans .push (c );
236
+ }
237
+ i += x ;
238
+ }
239
+ return ans .join (' ' );
240
+ }
241
+ ```
242
+
210
243
<!-- tabs: end -->
211
244
212
245
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function reverseParentheses ( s : string ) : string {
2
+ const n = s . length ;
3
+ const d = new Array ( n ) . fill ( 0 ) ;
4
+ const stk = [ ] ;
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 = [ ] ;
17
+ while ( i < n ) {
18
+ const c = s . charAt ( i ) ;
19
+ if ( c === '(' || c === ')' ) {
20
+ i = d [ i ] ;
21
+ x = - x ;
22
+ } else {
23
+ ans . push ( c ) ;
24
+ }
25
+ i += x ;
26
+ }
27
+ return ans . join ( '' ) ;
28
+ }
You can’t perform that action at this time.
0 commit comments