Skip to content

Commit d64bb4a

Browse files
authored
Create 1190-reverse-substrings-between-each-pair-of-parentheses.js
1 parent 447fd8e commit d64bb4a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const reverseParentheses = function(s) {
6+
const res = ['']
7+
let control = 0
8+
let order = 1
9+
for (let i = 0; i < s.length; i++) {
10+
if (s[i] === '(') {
11+
control++
12+
order = order ? 0 : 1
13+
res.push('')
14+
} else if (s[i] === ')') {
15+
if (order) res[control - 1] = res.pop() + res[control - 1]
16+
else res[control - 1] = res[control - 1] + res.pop()
17+
order = order ? 0 : 1
18+
control--
19+
} else {
20+
if (order) res[control] = res[control] + s[i]
21+
else res[control] = s[i] + res[control]
22+
}
23+
}
24+
return res[0]
25+
}

0 commit comments

Comments
 (0)