Us a stack. Every time a bracket opens push the bracket type (1,2,3) onto the stack. Every time a bracket closes it's type must match the type of the last one on the stack, which can then be removed. The last bracket in the string must be a closing bracket that removes the last bracket type from the stack.