Skip to content

Commit 426e28c

Browse files
committed
add Valid Parentheses solution
1 parent 30250fe commit 426e28c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

valid-parentheses/HoonDongKang.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* [Problem]: [20] Valid Parentheses
3+
* (https://leetcode.com/problems/valid-parentheses/)
4+
*/
5+
function isValid(s: string): boolean {
6+
//시간복잡도: O(n)
7+
//공간복잡도: O(n)
8+
const stack: string[] = [];
9+
const pairs: Record<string, string> = { ")": "(", "}": "{", "]": "[" };
10+
11+
for (const char of s) {
12+
if (["(", "{", "["].includes(char)) {
13+
stack.push(char);
14+
} else {
15+
if (stack.pop() !== pairs[char]) return false;
16+
}
17+
}
18+
19+
return stack.length === 0;
20+
}

0 commit comments

Comments
 (0)