Advertisement
horozov86

Balanced Parentheses

May 7th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. sequence = input()
  2.  
  3. stack = []
  4.  
  5. flag = True
  6. for ch in sequence:
  7. if ch in "({[":
  8. stack.append(ch)
  9. else:
  10. if len(stack) == 0:
  11. flag = False
  12. break
  13. last_opening_bracket = stack.pop()
  14. pair = f"{last_opening_bracket}{ch}"
  15. if pair not in "(){}[]":
  16. flag = False
  17. break
  18. if flag and len(stack) == 0:
  19. print("YES")
  20.  
  21. else:
  22. print("NO")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement