#K51642. Balanced Parentheses
Balanced Parentheses
Balanced Parentheses
Given a string consisting solely of the characters for three types of parentheses: ()
, {}
, and []
, determine whether the string is balanced or not. A string is considered balanced if every opening bracket has a corresponding closing bracket in the correct order and the types match.
The balancing condition can also be represented by the following logic in LaTeX format: $$ \text{A string is balanced if } \forall i, \text{ whenever } s[i] \text{ is an opening bracket, there exists a } s[j] \text{ (with } j > i \text{) such that } s[i] \text{ and } s[j] \text{ form a matching pair and the substring between them is balanced.} $$
Your task is to implement this check. The input will be provided via standard input (stdin) as a single line, and the result, either YES
or NO
, should be printed to standard output (stdout).
inputFormat
The input consists of a single line containing the string of parentheses. The string may include the characters '(', ')', '{', '}', '[' and ']'.
outputFormat
Output a single line: YES
if the string is balanced; otherwise, print NO
.
{[()()]}
YES