Skip to content

Commit 99aa78a

Browse files
committed
feat: 20. Valid Parentheses
1 parent bdf10fc commit 99aa78a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/gwbaik9717.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// n: len(s)
2+
// Time complexity: O(n)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {string} s
7+
* @return {boolean}
8+
*/
9+
var isValid = function (s) {
10+
const stack = [];
11+
12+
for (const chr of s) {
13+
if (
14+
(stack.at(-1) === "(" && chr === ")") ||
15+
(stack.at(-1) === "{" && chr === "}") ||
16+
(stack.at(-1) === "[" && chr === "]")
17+
) {
18+
stack.pop();
19+
continue;
20+
}
21+
22+
stack.push(chr);
23+
}
24+
25+
return stack.length === 0;
26+
};

0 commit comments

Comments
 (0)