diff --git a/valid-parentheses/Lustellz.ts b/valid-parentheses/Lustellz.ts new file mode 100644 index 000000000..330d97553 --- /dev/null +++ b/valid-parentheses/Lustellz.ts @@ -0,0 +1,26 @@ +// https://leetcode.com/problems/valid-parentheses +// Rumtime: 6ms +// Memory: 59.11MB + +function isValid(s: string): boolean { + let stack: string[] = []; + + for (let i = 0; i < s.length; i++) { + if (["(", "{", "["].includes(s[i])) { + stack.push(s[i]); + } else { + switch (s[i]) { + case ")": + if (stack.pop() !== "(") return false; + break; + case "}": + if (stack.pop() !== "{") return false; + break; + case "]": + if (stack.pop() !== "[") return false; + break; + } + } + } + return stack.length === 0; +}