#C7410. Balanced Parentheses Checker
Balanced Parentheses Checker
Balanced Parentheses Checker
You are given m strings consisting solely of the characters '(' and ')'. For each string, determine whether the string is balanced. A string is balanced if:
- Every opening parenthesis \( ( \) has a corresponding closing parenthesis \( ) \) that comes after it.
- The matching is done in the correct order, i.e. parentheses must close in the reverse order they were opened.
Formally, if you process the string from left to right and use a stack to keep track of opening parentheses, the string is balanced if the stack is empty at the end. In mathematical form, a string \( S = s_1 s_2 \dots s_n \) is balanced if
$$ \forall i \quad 1 \leq i \leq n,\; \#(\text{’(’ in } s_1 s_2 \dots s_i) \geq \#(\text{‘)’ in } s_1 s_2 \dots s_i) $$and after processing the full string, the counts are equal.
inputFormat
The input is given via standard input (stdin) in the following format:
m s1 s2 ... sm
Here, \( m \) is a positive integer representing the number of test strings. Each of the following \( m \) lines contains one string \( s_i \) consisting only of the characters '(' and ')'.
outputFormat
For each test string, output a single line containing either "YES" if the string is balanced, or "NO" if it is not. The output should be sent to standard output (stdout), with each answer on a separate line, in the same order as the input strings.
## sample3
()
(()())
(()
YES
YES
NO
</p>