#K86632. Balanced Parentheses Checker
Balanced Parentheses Checker
Balanced Parentheses Checker
Given a string s
consisting only of the characters ()
, {}
, and []
, determine whether the string is balanced.
A string is considered balanced if every opening bracket has a matching closing bracket in the correct order, and there are no unmatched or misordered brackets. In other words, if we denote the set of valid brackets by \(\{ (, ), {, }, [, ] \}\), then a string \(s\) is balanced if after processing it using a stack mechanism (pushing when encountering an opening bracket and popping when a matching closing bracket is found), the stack is empty.
If the string contains any characters outside of the allowed set, the string should be considered invalid and not balanced.
inputFormat
The input consists of a single line containing the string s
(which may be empty). Read the input from stdin.
outputFormat
Output a single line to stdout containing YES
if the string is balanced, and NO
otherwise.
()
YES