Skip to content

Commit c6811d7

Browse files
committed
valid parentheses solution
1 parent b5fc3ec commit c6811d7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

valid-parentheses/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function isValid(s: string): boolean {
2+
const stack: string[] = [];
3+
4+
const matchMap = new Map<string, string>();
5+
matchMap.set(")", "(");
6+
matchMap.set("]", "[");
7+
matchMap.set("}", "{");
8+
9+
for (const bracket of s) {
10+
if (matchMap.has(bracket)) {
11+
if (stack.length === 0 || matchMap.get(bracket) !== stack.pop()) {
12+
return false;
13+
}
14+
} else {
15+
stack.push(bracket);
16+
}
17+
}
18+
19+
return stack.length === 0;
20+
}

0 commit comments

Comments
 (0)