Advertisement
biswasrohit20

brack

Apr 4th, 2021
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def validBrackets(string):
  5. # maintain balance of string
  6. bal = 0
  7. ans = 0
  8. for i in range(0, len(string)):
  9. if (string[i] == '('):
  10. bal += 1
  11. else:
  12. bal += -1
  13.  
  14. if (bal == -1):
  15. ans += 1
  16. bal += 1
  17. return bal + ans
  18.  
  19.  
  20. def main():
  21. # read the input String
  22. f = sys.stdin
  23. invalid = f.readline().strip()
  24. print(validBrackets(invalid))
  25.  
  26.  
  27. if __name__ == "__main__":
  28. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement