Skip to content

Commit b5a3246

Browse files
committed
Feat: 20. Valid Parentheses
1 parent 193f10b commit b5a3246

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-parentheses/HC-kang.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/valid-parentheses
3+
* T.C. O(n)
4+
* S.C. O(n)
5+
*/
6+
function isValid(s: string): boolean {
7+
const pairs: Record<string, string> = { '{': '}', '[': ']', '(': ')' };
8+
const stack: string[] = [];
9+
10+
if (s.length % 2 == 1) return false;
11+
12+
for (const char of s) {
13+
if (pairs[char]) stack.push(char);
14+
else if (char != pairs[stack.pop()!]) return false;
15+
}
16+
return !stack.length;
17+
}

0 commit comments

Comments
 (0)