diff --git a/ValidParentheses.py b/ValidParentheses.py new file mode 100644 index 0000000..2cc36ef --- /dev/null +++ b/ValidParentheses.py @@ -0,0 +1,16 @@ +def is_valid(s): + stack = [] + mapping = {')': '(', '}': '{', ']': '['} + + for ch in s: + if ch in mapping: + if not stack or stack[-1] != mapping[ch]: + return False + stack.pop() + else: + stack.append(ch) + return not stack + + +print("Valid:", is_valid("({[]})")) +print("Invalid:", is_valid("({[})"))