Balancing Brackets
Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 99 megabytes
Time limit: 1.0 seconds
Output: Standard Output (stdout)
Memory limit: 99 megabytes
Time limit: 1.0 seconds
Greg is learning a dialect of Lisp that uses brackets, braces and parentheses but he is really bad at figuring out if they are balanced. He's asked you to write a program to help you figure out if everything is balanced.
Brackets are balanced when each closing brace matches the type of the last opening brace that is not yet matched, and visa versa. For example, )(
and ([)]
are not matched but {()}[]
is.
Input
The first line will contain the single value 1 \le N \le 1000000, the length of Greg's program.
The next line will contain N characters and is the body of the program.
Output
If all brackets, braces and parentheses are balanced output YES
, otherwise output NO
-
Sample Input 1
10 (Hello[])a
Sample Output 1
YES
-
Sample Input 2
10 {Error!}[1
Sample Output 2
NO