#C855. Valid Bracket Sequence Check
Valid Bracket Sequence Check
Valid Bracket Sequence Check
Given a string \(s\), determine whether it has a valid sequence of matching brackets. Only the brackets ('(', ')', '{', '}', '[' and ']')
are considered. A string is said to have a valid bracket sequence if:
- The number of opening and closing brackets of each type is equal.
- Every closing bracket has a corresponding matching opening bracket of the same type.
- The brackets are properly nested.
Note that any other characters in the string should be ignored. If the string has a valid bracket sequence, output 1
; otherwise, output 0
.
The validity condition can be summarized by the following logic: \[ \text{isValid}(s)=\begin{cases} 1, & \text{if brackets in } s \text{ are balanced and well-nested},\\ 0, & \text{otherwise}. \end{cases} \]
inputFormat
The input is read from standard input. It consists of a single line containing the string \(s\), which may include any characters. Only the bracket characters ('(', ')', '{', '}', '[' and ']')
are used for validation; other characters should be ignored.
outputFormat
Output a single integer (1
or 0
) to standard output. Output 1
if the bracket sequence in \(s\) is valid, otherwise output 0
.
([]){}
1