Skip to content

Commit a2976d0

Browse files
authored
Merge pull request #1875 from Lustellz/main
[Lustellz] Week6 solution
2 parents 4fa8a1d + bfb7025 commit a2976d0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/Lustellz.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/valid-parentheses
2+
// Rumtime: 6ms
3+
// Memory: 59.11MB
4+
5+
function isValid(s: string): boolean {
6+
let stack: string[] = [];
7+
8+
for (let i = 0; i < s.length; i++) {
9+
if (["(", "{", "["].includes(s[i])) {
10+
stack.push(s[i]);
11+
} else {
12+
switch (s[i]) {
13+
case ")":
14+
if (stack.pop() !== "(") return false;
15+
break;
16+
case "}":
17+
if (stack.pop() !== "{") return false;
18+
break;
19+
case "]":
20+
if (stack.pop() !== "[") return false;
21+
break;
22+
}
23+
}
24+
}
25+
return stack.length === 0;
26+
}

0 commit comments

Comments
 (0)