Skip to content

Commit d96cdb8

Browse files
committed
valid-parentheses
1 parent e1ae708 commit d96cdb8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

valid-parentheses/jun0811.js

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

0 commit comments

Comments
 (0)