#Stack is a linear data structure which follows a particular order in which the operations are performed.The order may be LIFO


string="[aa{()}a[a]]"
stack=[]
top=-1

for i in string:
    if((i=='[') or (i=='(') or (i=='{')):  
        print(1)
        top=top+1
        stack.append(i)
    elif(i==']' and stack[top]=='['):
        print(2)
        stack.pop(top)
        top=top-1
    elif(i==')' and stack[top]=='('):
        print(3)
        stack.pop(top)
        top=top-1
    elif(i=='}' and stack[top]=='{'):
        print(4)
        stack.pop(top)
        top=top-1
    else:
        top=top
    
        
print      
if (len(stack)==0):
        print("string balanced\n")
else:
        print("string not balanced")
        
        
#(i=='}' and stack[top]=='{')   

#Stack is a linear data structure which follows a particular order in which the operations are performed.The order may be LIFO


string="[aa{()}a[a]]"
stack=[]
top=-1

for i in string:
    if((i=='[') or (i=='(') or (i=='{')):  
        print(1)
        top=top+1
        stack.append(i)
    elif(i==']' and stack[top]=='['):
        print(2)
        stack.pop(top)
        top=top-1
    elif(i==')' and stack[top]=='('):
        print(3)
        stack.pop(top)
        top=top-1
    elif(i=='}' and stack[top]=='{'):
        print(4)
        stack.pop(top)
        top=top-1
    else:
        top=top
    
        
print      
if (len(stack)==0):
        print("string balanced\n")
else:
        print("string not balanced")
        
        
#(i=='}' and stack[top]=='{')   

#Stack is a linear data structure which follows a particular order in which the operations are performed.The order may be LIFO #(i=='}' and stack[top]=='{')