Advertisement
go6odn28

6_balanced_parenhesis

May 14th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. parenthesis = input()
  2.  
  3. my_stack = []
  4. is_balanced = True
  5. valid_parentheses = {"(": ")", "{": "}", "[": "]"}
  6.  
  7.  
  8. for bracket in parenthesis:
  9.     if bracket in "({[":
  10.         my_stack.append(bracket)
  11.     else:
  12.         if not my_stack or bracket != valid_parentheses[my_stack.pop()]:
  13.             is_balanced = False
  14.             break
  15.  
  16. print("YES") if is_balanced and len(my_stack) % 2 == 0 else print("NO")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement