Skip to content

Commit e8c532d

Browse files
committed
feat(soobing): week6 > valid-parentheses
1 parent fdf3120 commit e8c532d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

valid-parentheses/soobing.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function isValid(s: string): boolean {
2+
const result: string[] = [];
3+
const open = ["(", "[", "{"];
4+
const close = [")", "]", "}"];
5+
for (let i = 0; i < s.length; i++) {
6+
if (open.includes(s[i])) {
7+
result.push(s[i]);
8+
} else {
9+
const index = close.findIndex((item) => item === s[i]);
10+
const popedItem = result.pop();
11+
if (popedItem !== open[index]) return false;
12+
}
13+
}
14+
return result.length === 0;
15+
}

0 commit comments

Comments
 (0)