We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1f5688a commit 661204eCopy full SHA for 661204e
valid-parentheses/Jeehay28.ts
@@ -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