Skip to content

Commit 95a8ea4

Browse files
committed
feat: solve new question
1 parent 3f530e5 commit 95a8ea4

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

valid-parentheses/main.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,25 @@
55
var isValid = function(s) {
66
const stack = []
77

8+
let matching_brackets = {
9+
"]" : "[",
10+
"}" : "{",
11+
")" : "("
12+
};
13+
814
for (let i=0; i < s.length; i++) {
9-
if (s[i] === "(") {
10-
stack.push(s[i]);
11-
} else if (s[i] === "[") {
12-
stack.push(s[i]);
13-
} else if (s[i] === "{") {
15+
if (["(", "{", "["].includes(s[i])) {
1416
stack.push(s[i]);
15-
} else if (s[i] === "]") {
16-
if (stack[stack.length -1 ] == "[") {
17-
stack.pop("[")
18-
} else {
19-
return false;
20-
}
21-
} else if (s[i] === "}") {
22-
if (stack[stack.length -1 ] == "{") {
23-
stack.pop("{")
24-
} else {
25-
return false;
26-
}
27-
} else if (s[i] === ")") {
28-
if (stack[stack.length -1 ] == "(") {
29-
stack.pop("(")
30-
} else {
31-
return false;
32-
}
17+
} else if (stack.length > 0 && stack[stack.length - 1] === matching_brackets[s[i]]) {
18+
stack.pop();
19+
} else {
20+
return false;
3321
}
3422
}
35-
if (stack.length == 0) {
23+
24+
if (stack == 0) {
3625
return true;
26+
} else {
27+
return false;
3728
}
38-
return false;
3929
};
40-

0 commit comments

Comments
 (0)