From 93876d975ee383e866624a1d6606fdc7eb5ea13c Mon Sep 17 00:00:00 2001 From: Yashwant Bisht Date: Tue, 28 Oct 2025 01:04:29 +0530 Subject: [PATCH] Implement valid parentheses checker function Signed-off-by: Yashwant Bisht --- ValidParentheses.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ValidParentheses.py 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("({[})"))