From 32ac792085f32e6253c341aa7bdf3d862cb57319 Mon Sep 17 00:00:00 2001 From: Gabriel Donnan <47415809+gabedonnan@users.noreply.github.com> Date: Wed, 18 Jan 2023 19:57:41 +0000 Subject: [PATCH] Create valid-parentheses.py not the fastest but works for this one --- valid-parentheses.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 valid-parentheses.py diff --git a/valid-parentheses.py b/valid-parentheses.py new file mode 100644 index 0000000..4226d69 --- /dev/null +++ b/valid-parentheses.py @@ -0,0 +1,12 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + for letter in s: + if (stack[-1] if len(stack) > 0 else "") == self.other(letter): + stack.pop() + else: + stack.append(letter) + return len(stack) == 0 + + def other(self, ch: str) -> str: + return {"{": None, "}": "{", "(": None, ")": "(", "[": None, "]": "["}[ch]