Question:
Given a string of length N, output “Correct” if brackets close in correct order else output “Incorrect”
Input : ({}[((({{}})[{()}]))])
Solution(Python):
# Author : Sreejith Sreekantan # Description : # Given a string of length N, output "Correct" if brackets close in correct order else output "Incorrect" # Input : ({}[((({{}})[{()}]))]) # #!/usr/bin/python def validString(input1): stk = [] flag = True for x in input1: if x in ['{', '(', '[']: stk.append(x) else: if len(stk)==0: flag = False elif x == '}' and not stk[len(stk)-1]=='{' : flag = False elif x == ')' and not stk[len(stk)-1]=='(' : flag = False elif x == '}' and not stk[len(stk)-1]=='{' : flag = False if not flag: return "Incorrect" else: stk.pop() return "Correct" input1 = '({}[((({{}})[{()}]))])' input2 = '({}((({{}})[{()}]))])' print validString(input1) print validString(input2)