Skip to content

Commit b2a6a85

Browse files
committed
Add valid-parentheses solution
1 parent 7287a87 commit b2a6a85

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

valid-parentheses/Jeehay28.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
6+
// Time Complexity: O(n)
7+
// Space Complexity: O(n)
8+
var isValid = function (s) {
9+
const obj = {
10+
"(" : ")",
11+
"{" : "}",
12+
"[" : "]",
13+
};
14+
15+
let stack = [];
16+
17+
for (any of s) {
18+
// open bracket
19+
if (obj[any]) {
20+
stack.push(any);
21+
} else {
22+
// close bracket
23+
if (stack.length === 0) {
24+
return false;
25+
} else if (obj[stack[stack.length - 1]] !== any) {
26+
return false;
27+
} else {
28+
stack.pop();
29+
}
30+
}
31+
}
32+
return stack.length === 0 ? true : false;
33+
};
34+
35+

0 commit comments

Comments
 (0)