Advertisement
Spocoman

04. Balanced Brackets

Jan 20th, 2022 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  1. n = int(input())
  2. open_brackets = 0
  3.  
  4. for i in range(n):
  5.     bracket = input()
  6.     if bracket == ")":
  7.         open_brackets -= 1
  8.         if open_brackets < 0:
  9.             break
  10.     elif bracket == "(":
  11.         open_brackets += 1
  12.         if open_brackets > 1:
  13.             break
  14.  
  15. if open_brackets == 0:
  16.     print("BALANCED")
  17. else:
  18.     print("UNBALANCED")
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement