Skip to content

Commit 2c0524b

Browse files
committed
add: #222 Valid Parentheses
1 parent 58386e4 commit 2c0524b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

valid-parentheses/sukyoungshin.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const pairs = {
2+
")": "(",
3+
"}": "{",
4+
"]": "[",
5+
};
6+
7+
function isValid(s: string): boolean {
8+
const stack: string[] = [];
9+
for (let i = 0; i < s.length; i++) {
10+
const str = s[i];
11+
12+
if (str in pairs) {
13+
if (pairs[str] !== stack[stack.length - 1]) {
14+
return false;
15+
} else {
16+
stack.pop();
17+
}
18+
} else {
19+
stack.push(str);
20+
}
21+
}
22+
23+
return stack.length === 0;
24+
};

0 commit comments

Comments
 (0)