Skip to content

Commit 7acd8f7

Browse files
committed
Valid Parentheses solution
1 parent 7cf288b commit 7acd8f7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

valid-parentheses/jdy8739.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function(s) {
6+
const stack = [];
7+
8+
for (let i=0; i<s.length; i++) {
9+
const cur = s[i];
10+
11+
if (cur === '(' || cur === '[' || cur === '{') {
12+
stack.push(cur);
13+
} else {
14+
const topOfStack = stack[stack.length - 1];
15+
16+
const caseA = topOfStack === '(' && cur === ')';
17+
const caseB = topOfStack === '[' && cur === ']';
18+
const caseC = topOfStack === '{' && cur === '}';
19+
20+
if (caseA || caseB || caseC) {
21+
stack.pop();
22+
} else {
23+
return false;
24+
}
25+
}
26+
}
27+
28+
return stack.length === 0;
29+
};
30+

0 commit comments

Comments
 (0)