#C8274. Balanced Brackets
Balanced Brackets
Balanced Brackets
Given an integer n and a string s consisting of the characters ( ) [ ] { }
, determine if the sequence of brackets is balanced.
A sequence is balanced if every opening bracket has a corresponding closing bracket in the correct order.
In mathematical terms, a bracket sequence s is balanced if and only if when processing each character from left to right using a stack, we have:
\( \text{if } s[i] \text{ is an opening bracket, push it onto the stack; if it is a closing bracket, then the top of the stack must be the matching opening bracket.} \)
The sequence is balanced if the stack is empty after processing all characters.
inputFormat
The input consists of two lines:
- The first line contains an integer n representing the length of the string.
- The second line contains the string s of length n which consists solely of the characters: (, ), [, ], {, }.
outputFormat
Output a single line containing YES
if the sequence is balanced, or NO
otherwise.
6
()[]{}
YES