#K70172. Balanced Bracket Checker
Balanced Bracket Checker
Balanced Bracket 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 corresponding closing bracket in the correct order.
For example, the string ([])
is balanced, whereas ([)]
is not. An empty string is also considered balanced.
You can use a stack-based approach to solve this problem. Mathematically, you must ensure that if i indexes an opening bracket then there exists a corresponding closing bracket at index j such that:
$$ balance(s)=\begin{cases} YES & \text{if all brackets match correctly}\\ NO & \text{otherwise} \end{cases} $$inputFormat
The input consists of a single line containing the string s
made up solely of the characters '(', ')', '[' and ']'. The string may be empty.
outputFormat
Output a single line: YES
if the string is balanced and NO
otherwise.## sample
([])
YES
</p>