Skip to content

Commit 40ee4bd

Browse files
committed
solve: 20. Valid Parentheses
1 parent bfd7843 commit 40ee4bd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/evan.js

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+
if (s.length % 2 !== 0) {
7+
return false;
8+
}
9+
10+
const stack = [];
11+
const pair = {
12+
")": "(",
13+
"}": "{",
14+
"]": "[",
15+
};
16+
17+
for (let char of s) {
18+
if (pair[char] && stack.pop() !== pair[char]) {
19+
return false;
20+
}
21+
22+
stack.push(char);
23+
}
24+
25+
return stack.length === 0;
26+
};

0 commit comments

Comments
 (0)