Skip to content

Commit 08135ae

Browse files
Create valid_parentheses.js
1 parent e04c613 commit 08135ae

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid_parentheses.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function(s) {
6+
const brackets = {
7+
"(": ")",
8+
"{": "}",
9+
"[": "]"
10+
};
11+
12+
let test = [];
13+
14+
if(s.length % 2 !== 0) return false;
15+
16+
if(s[0] === ")" || s[0] === "]" || s[0] === "}") return false;
17+
18+
if(s[s.length - 1] === "(" || s[s.length - 1] === "[" || s[s.length - 1] === "{") return false;
19+
20+
for(let i = 0; i < s.length; i++) {
21+
if(s[i] === "(" || s[i] === "[" || s[i] === "{") test.push(s[i]);
22+
else if(brackets[test.pop()] !== s[i]) return false;
23+
}
24+
25+
return test.length === 0;
26+
};

0 commit comments

Comments
 (0)