Skip to content

Commit 47e2958

Browse files
committed
Valid Parentheses solution
1 parent 14958f6 commit 47e2958

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

19-validParentheses.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
let arr = [];
7+
8+
let input = s.split("");
9+
for (let i = 0; i < s.length; i++) {
10+
if (s[i] === "(" || s[i] === "{" || s[i] === "[") {
11+
arr.push(s[i]);
12+
}
13+
14+
if (s[i] === ")") {
15+
if (arr.pop() !== "(") {
16+
return false;
17+
}
18+
}
19+
20+
if (s[i] === "}") {
21+
if (arr.pop() !== "{") {
22+
return false;
23+
}
24+
}
25+
26+
if (s[i] === "]") {
27+
if (arr.pop() !== "[") {
28+
return false;
29+
}
30+
}
31+
}
32+
33+
return arr.length === 0;
34+
};
35+
36+
console.log(isValid("({[[]]]})"));

0 commit comments

Comments
 (0)