Skip to content

Commit 25aca9a

Browse files
authored
Update 1190-reverse-substrings-between-each-pair-of-parentheses.js
1 parent c72315a commit 25aca9a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1190-reverse-substrings-between-each-pair-of-parentheses.js

+30
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,33 @@ const reverseParentheses = function(s) {
2323
}
2424
return res[0]
2525
}
26+
27+
// another
28+
29+
/**
30+
* @param {string} s
31+
* @return {string}
32+
*/
33+
const reverseParentheses = function(s) {
34+
const n = s.length
35+
const stack = []
36+
const pair = []
37+
for(let i = 0; i < n; i++) {
38+
if(s[i] === '(') stack.push(i)
39+
else if(s[i] === ')') {
40+
const tmp = stack.pop()
41+
pair[i] = tmp
42+
pair[tmp] = i
43+
}
44+
}
45+
let res = ''
46+
for(let i = 0, d = 1; i < n; i += d) {
47+
if(s[i] === '(' || s[i] ===')') {
48+
i = pair[i]
49+
d = -d
50+
} else {
51+
res += s[i]
52+
}
53+
}
54+
return res
55+
}

0 commit comments

Comments
 (0)