In the case of the presence of brackets of several types, everything becomes a little more complicated. We create a stack to act as that balance variable. This is necessary because parentheses cannot overlap. When we walk through a line and encounter an opening parenthesis, we push it onto the stack. When we encounter a closing brace, we try to pop the opening brace of that type off the stack. If a brace of a different type is on the stack, the sequence is invalid. If the stack is non-empty at the end, the sequence is also invalid.