Skip to content

Commit 5591312

Browse files
committed
1. Valid Parentheses
1 parent 193f10b commit 5591312

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

valid-parentheses/sunjae95.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* stack
5+
*
6+
* n: length of s
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var isValid = function (s) {
11+
const stack = [];
12+
13+
for (let i = 0; i < s.length; i++) {
14+
if (s[i] === "(" || s[i] === "{" || s[i] === "[") {
15+
stack.push(s[i]);
16+
continue;
17+
}
18+
19+
const top = stack.length ? stack[stack.length - 1] : null;
20+
if (top === null) return false;
21+
22+
if (
23+
(top === "(" && s[i] === ")") ||
24+
(top === "{" && s[i] === "}") ||
25+
(top === "[" && s[i] === "]")
26+
) {
27+
stack.pop();
28+
} else return false;
29+
}
30+
31+
return !stack.length;
32+
};

0 commit comments

Comments
 (0)