Skip to content

Commit 0a38ca6

Browse files
committed
feat: add js solution to lc problem: No.1190
1 parent 3060c83 commit 0a38ca6

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,44 @@ func reverseParentheses(s string) string {
357357
}
358358
```
359359

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+
360398
<!-- tabs:end -->
361399

362400
<!-- solution:end -->

solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses/README_EN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,44 @@ func reverseParentheses(s string) string {
350350
}
351351
```
352352

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+
353391
<!-- tabs:end -->
354392

355393
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)