#C11178. Valid Bracket Sequence Checker
Valid Bracket Sequence Checker
Valid Bracket Sequence Checker
You are given a string containing various characters including brackets: (')', '{', '['
and their corresponding closing brackets. Your task is to determine whether the bracket sequence in the input string is valid.
A valid bracket sequence meets the following conditions:
- Every opening bracket has a corresponding closing bracket of the same type.
- The pairs of brackets are properly nested.
For example:
(a + b)
is valid.{[a + (b * c)]}
is valid.{a + [b * (c + d)]}}
is invalid due to an extra closing bracket.{(a + b]}
is invalid because the brackets do not match.
Your solution should read the input from standard input (stdin) and output the result to standard output (stdout) as either True
or False
.
inputFormat
The input consists of a single line containing a string s which may include letters, digits, spaces, and bracket characters ('(', ')', '{', '}', '[', ']')
.
You can assume that the length of the string is within reasonable limits for processing.
outputFormat
Output a single line: True
if the input string contains a valid bracket sequence; otherwise, output False
.
(a + b)
True