Balanced Brackets
You are given an expression string S which contains expressions that are made of bracket characters {, }, (, ), [, ]. Each expression in the string is separated by space.
Two brackets are considered to be a matched pair if an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and ().
A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example,
{[(])} is not balanced because the contents in between { and } are not balanced. The pair of square brackets enclose a single, unbalanced opening bracket, (, and the pair of brackets enclose a single, unbalanced closing square bracket, ].
Write a program to examine whether each expression in
S is balanced.
Input
The first line contains a string S with space-separated bracket expressions.
Sample Input 1
{()} ({}
Sample Output 1
YES
NO
Sample Input 2
{}[] [({})] {}
Sample Output 2
YES
YES
YES
Comments
Leave a comment