#C11044. Balanced Brackets Validator
Balanced Brackets Validator
Balanced Brackets Validator
You are given a string s
which may contain any characters. However, only the following six characters are considered valid brackets: ('
, ')'
, '{'
, '}'
, '['
, and ']'
.
Your task is to determine if the brackets in the string are balanced. A string is considered balanced if all the following conditions are met:
- Every opening bracket has a corresponding closing bracket of the same type.
- Opening brackets occur in the proper order such that each closing bracket matches the most recent unmatched opening bracket.
Formally, if we denote an opening bracket as c
then it must be closed by its corresponding closing bracket. In LaTeX, the condition can be written as:
$$
\forall i, j \text{ such that } s[i] \in \{(,\{,\[\} \text{ and } s[j] \text{ is its matching closing bracket}, \text{ the brackets are well-nested.}
$$
Note: The string may include other non-bracket characters which should be ignored.
inputFormat
The input consists of a single line containing the string s
, which may contain whitespace and other characters.
outputFormat
Output a single line: True
if the brackets in the string are balanced, otherwise False
.
([])
True