diff --git a/LeetCode/0020_Valid_Parentheses.py b/LeetCode/0020_Valid_Parentheses.py new file mode 100644 index 0000000..e2e41c5 --- /dev/null +++ b/LeetCode/0020_Valid_Parentheses.py @@ -0,0 +1,16 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + openbrackets = ('(', '[', '{') + matches = ('()', '[]', '{}') + + for c in s: + if c in openbrackets: + stack.append(c) + else: + if len(stack) < 1 or stack[-1] + c not in matches: + return False + + stack.pop() + + return len(stack) == 0 \ No newline at end of file