Skip to content

Commit 3e8b54a

Browse files
committed
Add LeetCode#20 Valid Parentheses
1 parent af0b0bb commit 3e8b54a

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)