Longest Valid Parentheses LeetCode solution in JavaScript

1
var longestValidParentheses = function (s) {
2
let indexStack = [-1];
3
let characterStack = [];
4
let maxLength = 0;
5
for (let i = 0; i < s.length; i++) {
6
if (s[i] == "(") {
7
indexStack.push(i);
8
characterStack.push(s[i]);
9
} else {
10
if (characterStack[characterStack.length - 1] == "(") {
11
indexStack.pop();
12
characterStack.pop();
13
if (maxLength < i - indexStack[indexStack.length - 1]) {
14
maxLength = i - indexStack[indexStack.length - 1];
15
}
16
} else {
17
indexStack.push(i);
18
}
19
}
20
}
21
return maxLength;
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0