Advertisement
pacho_the_python

parenteses

Jan 14th, 2024
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. class BalancedParentheses:
  2.     def __init__(self, parentheses):
  3.         self.parentheses = parentheses
  4.         self.balance_dict = {"(": ")", "{": "}", "[": "]"}
  5.         self.brackets = []
  6.  
  7.     def process(self):
  8.         condition = True
  9.         for bracket in self.parentheses:
  10.             if bracket in "({[":
  11.                 self.brackets.append(bracket)
  12.             elif not self.brackets:
  13.                 condition = False
  14.                 break
  15.             else:
  16.                 last_open_bracket = self.brackets.pop()
  17.                 if self.balance_dict[last_open_bracket] != bracket:
  18.                     condition = False
  19.                     break
  20.  
  21.         if condition and len(self.brackets) == 0:
  22.             print("YES")
  23.         else:
  24.             print("NO")
  25.  
  26.  
  27. obj = BalancedParentheses(parentheses=input())
  28. obj.process()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement