Skip to content

Commit 451f99d

Browse files
committed
✅ [20] stack
1 parent 6cf9a2b commit 451f99d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

20/my_solution.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* @return {boolean}
44
*/
55
const isValid = (str) => {
6-
let map = new Map(), openStack = [], char = str.split("");
6+
let char = str.split("");
7+
8+
if (1 === char.length) return false;
9+
10+
let map = new Map(), openStack = [];
711

812
map.set("{", "}");
913
map.set("(", ")");

20/solution.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isValid = (str) => {
6+
let char = str.split(""), map = new Map(), openStack = [];
7+
8+
map.set("{", "}");
9+
map.set("(", ")");
10+
map.set("[", "]");
11+
12+
for (let i = 0; i < char.length; i++) {
13+
let isClosing = [")", "]", "}"].includes(char[i]);
14+
if (0 === openStack.length && isClosing) return false;
15+
16+
if (isClosing) {
17+
if (map.get(openStack[openStack.length - 1]) === char[i]) {
18+
openStack.pop();
19+
} else {
20+
return false;
21+
}
22+
} else {
23+
openStack.push(char[i]);
24+
}
25+
}
26+
27+
return 0 === openStack.length;
28+
};

0 commit comments

Comments
 (0)