#C6818. Balanced Braces

    ID: 50620 Type: Default 1000ms 256MiB

Balanced Braces

Balanced Braces

You are given a string consisting only of the characters '(' and ')'. Your task is to determine whether the braces in the string are balanced. A balanced string is one where every opening brace '(' has a corresponding closing brace ')' in the correct order.

Formally, a string \(S\) is balanced if and only if it can be fully reduced by repeatedly removing adjacent matching pairs \(\(\)\). Otherwise, the string is not balanced. The solution typically uses a stack data structure.

For example:

  • Input: (), Output: YES
  • Input: (() , Output: NO

Implement a program that reads several test cases from standard input and outputs the result for each test case on a new line.

inputFormat

The first line contains a single integer \(T\) (\(1 \leq T \leq 10^4\)) denoting the number of test cases. Each of the following \(T\) lines contains a non-empty string consisting only of the characters '(' and ')'.

Input is read from standard input.

outputFormat

For each test case, output a single line containing "YES" if the braces are balanced, or "NO" otherwise. The output should be written to standard output.

## sample
3
()
(()
(())
YES

NO YES

</p>