Skip to content

Commit 3060c83

Browse files
committed
feat: add ts solution to lc problem: No.1190
1 parent d273d27 commit 3060c83

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,39 @@ var reverseParentheses = function (s) {
214214
};
215215
```
216216

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+
217250
<!-- tabs:end -->
218251

219252
<!-- solution:end -->

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,39 @@ var reverseParentheses = function (s) {
207207
};
208208
```
209209

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+
210243
<!-- tabs:end -->
211244

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

0 commit comments

Comments
 (0)