Skip to content

Commit 661204e

Browse files
Jeehay28Jeehay28
authored andcommitted
Add valid-parentheses solution in TypeScript
1 parent 1f5688a commit 661204e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/Jeehay28.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
function isValid(s: string): boolean {
5+
const map = new Map<string, string>([
6+
["(", ")"],
7+
["{", "}"],
8+
["[", "]"],
9+
]);
10+
11+
let stack: string[] = [];
12+
13+
for (const ch of s) {
14+
if (map.has(ch)) {
15+
stack.push(ch);
16+
} else {
17+
const last = stack.pop();
18+
if (!last || ch !== map.get(last)) {
19+
return false;
20+
}
21+
}
22+
}
23+
24+
return stack.length === 0;
25+
}
26+

0 commit comments

Comments
 (0)