Skip to content

Commit 45bd633

Browse files
authored
Update Solution.ts
1 parent 012c46d commit 45bd633

File tree

1 file changed

+11
-23
lines changed
  • solution/1100-1199/1190.Reverse Substrings Between Each Pair of Parentheses

1 file changed

+11
-23
lines changed
Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
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);
2211
} else {
23-
ans.push(c);
12+
stk.push(c);
2413
}
25-
i += x;
2614
}
27-
return ans.join('');
15+
return stk.join('');
2816
}

0 commit comments

Comments
 (0)