Skip to content

Commit 3f52b76

Browse files
authored
Update 20-valid-parentheses.js
1 parent c21332b commit 3f52b76

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

20-valid-parentheses.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isValid = function(s) {
6+
const stack = []
7+
const n = s.length
8+
for(let c of s) {
9+
if(c === '(' || c === '{' || c === '[') stack.push(c)
10+
else if(c === ')') {
11+
if(stack[stack.length - 1] === '(') stack.pop()
12+
else return false
13+
}
14+
else if(c === '}') {
15+
if(stack[stack.length - 1] === '{') stack.pop()
16+
else return false
17+
}
18+
else if(c === ']') {
19+
if(stack[stack.length - 1] === '[') stack.pop()
20+
else return false
21+
}
22+
}
23+
return stack.length === 0
24+
};
25+
26+
// another
27+
128
/**
229
* @param {string} s
330
* @return {boolean}

0 commit comments

Comments
 (0)