from collections import deque
def check(val1,val2):
return (val1=="(" and val2==")") or (val1=="{" and val2=="}") or (val1=="[" and val2=="]")
def exp_stack(exp):
stack=deque()
for i in range(len(exp)):
if (exp[i]=="(" or exp[i]=="{" or exp[i]=="["):
stack.append(exp[i])
else:
if len(stack)==0:
return False
elif check(stack[-1],exp[i]):
stack.pop()
continue
return False
return True
exp = "({})[]"
if exp_stack(exp):
print("Balanced")
else:
print("Not Balanced")
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVxdWUKCmRlZiBjaGVjayh2YWwxLHZhbDIpOgogICAgcmV0dXJuICh2YWwxPT0iKCIgYW5kIHZhbDI9PSIpIikgb3IgKHZhbDE9PSJ7IiBhbmQgdmFsMj09In0iKSBvciAodmFsMT09IlsiIGFuZCB2YWwyPT0iXSIpCgpkZWYgZXhwX3N0YWNrKGV4cCk6CiAgICBzdGFjaz1kZXF1ZSgpCiAgICBmb3IgaSBpbiByYW5nZShsZW4oZXhwKSk6CiAgICAgICAgaWYgKGV4cFtpXT09IigiIG9yIGV4cFtpXT09InsiIG9yIGV4cFtpXT09IlsiKToKICAgICAgICAgICAgc3RhY2suYXBwZW5kKGV4cFtpXSkKICAgICAgICBlbHNlOgogICAgICAgICAgICBpZiBsZW4oc3RhY2spPT0wOgogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgICAgIGVsaWYgY2hlY2soc3RhY2tbLTFdLGV4cFtpXSk6CiAgICAgICAgICAgICAgICBzdGFjay5wb3AoKQogICAgICAgICAgICAgICAgY29udGludWUKICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICByZXR1cm4gVHJ1ZQoKZXhwID0gIih7fSlbXSIKCmlmIGV4cF9zdGFjayhleHApOgogICAgcHJpbnQoIkJhbGFuY2VkIikKZWxzZToKICAgIHByaW50KCJOb3QgQmFsYW5jZWQiKQ==