-
Notifications
You must be signed in to change notification settings - Fork 0
/
20. Valid Parentheses.js
34 lines (33 loc) · 1019 Bytes
/
20. Valid Parentheses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let stack = [];
if(s.length % 2) return false;
for(let i = 0; i < s.length; i++)
{
let curr = s[i];
if(curr == "(" || curr == "[" || curr == "{")
{
switch(curr)
{
case "(":
stack.push(")");
break;
case "[":
stack.push("]");
break;
case "{":
stack.push("}");
break;
}
}
else if(curr == ")" || curr == "]" || curr == "}")
{
if(stack.pop() != curr) return false;
}
}
if(stack.length != 0) return false;
return true;
};