Skip to content

Commit 775cf7f

Browse files
committed
feat: add ts solution to lc problem: No.1190
1 parent 0a38ca6 commit 775cf7f

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
@@ -395,6 +395,44 @@ function reverseParentheses(s: string): string {
395395
}
396396
```
397397

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+
398436
<!-- tabs:end -->
399437

400438
<!-- 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
@@ -388,6 +388,44 @@ function reverseParentheses(s: string): string {
388388
}
389389
```
390390

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+
391429
<!-- tabs:end -->
392430

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

0 commit comments

Comments
 (0)