#K91982. Balanced Brackets
Balanced Brackets
Balanced Brackets
Given a string containing only the characters (
, )
, {
, }
, [
, and ]
, determine if the input string is valid using the following rules:
- Every opening bracket must have a corresponding closing bracket of the same type.
- Brackets must be closed in the correct order.
For example, the string ()
is valid, while the string (]
is not. The solution should implement a function using a stack-based approach to verify balanced brackets. In cases where the input string is empty, consider it as balanced.
The formal conditions of validity can be expressed in LaTeX:
$$ \text{For a string } s \text{ to be valid, if } s = x\,y, \text{ then } x \text{ and } y \text{ must both be valid, and if } s = \{a\}, \text{ then } a \text{ must be valid.} $$
inputFormat
The input is provided via standard input (stdin) and consists of a single line containing a non-empty string of brackets. The string includes only the characters (
, )
, {
, }
, [
, and ]
.
outputFormat
The output should be printed to standard output (stdout) as a single line: True
if the string is balanced, and False
otherwise.
()
True
</p>