Valid Brackets
View as PDFGiven a sequence of brackets, determine whether or not it is balanced. A sequence of brackets is balanced if each opening bracket has a corresponding closing bracket in the correct location. For example ()[{}] is balanced but ([]} and ([]([]{} are not. Here are some more unbalanced bracket sequences:
([)]
))((
{{))
Input
The first line of input is an even number,
, representing the number of bracket characters that must be processed.
The next line of input is bracket characters consisting only of
()[]{}.
Output
If the bracket sequence is valid, output YES. Otherwise output NO.
Sample Input
4
(){}
Sample Output
YES
Comments