diff --git a/valid-parentheses/ppxyn1.py b/valid-parentheses/ppxyn1.py new file mode 100644 index 000000000..191bb518d --- /dev/null +++ b/valid-parentheses/ppxyn1.py @@ -0,0 +1,22 @@ +# idea : stack + +class Solution: + def isValid(self, s: str) -> bool: + mapping = {"(": ")", "{": "}", "[": "]"} + stack = [] + + for ch in s: + if ch in mapping: + stack.append(ch) + else: + if not stack: + return False + if mapping[stack.pop()] != ch: + return False + if stack: + return False + + return True + + +