Skip to content

Commit 19d9ba7

Browse files
committed
Add Valid Parentheses Problem
1 parent 2bf811b commit 19d9ba7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const ValidParentheses = (s) => {
2+
var stack = [];
3+
4+
obj = {
5+
"(": ")",
6+
"{": "}",
7+
"[": "]",
8+
};
9+
10+
for (let i of s) {
11+
if (Object.keys(obj).includes(i)) {
12+
stack.push(i);
13+
} else if (stack && obj[stack[stack.length - 1]] == i) {
14+
stack.pop();
15+
} else {
16+
return false;
17+
}
18+
}
19+
20+
return stack.length === 0 ? true : false;
21+
};
22+
23+
s = "()";
24+
s1 = "()[]{}";
25+
s2 = "(]";
26+
27+
console.log(ValidParentheses(s));
28+
console.log(ValidParentheses(s1));
29+
console.log(ValidParentheses(s2));

0 commit comments

Comments
 (0)