diff --git a/algorithms/valid-parentheses.py b/algorithms/valid-parentheses.py new file mode 100644 index 000000000000..e7fa7b2443de --- /dev/null +++ b/algorithms/valid-parentheses.py @@ -0,0 +1,12 @@ +class Solution: + def isValid(self, s): + stack = [] + mapping = {")": "(", "}": "{", "]": "["} + for char in s: + if char in mapping: + top = stack.pop() if stack else "#" + if mapping[char] != top: + return False + else: + stack.append(char) + return not stack