#C12808. Bracket Validator
Bracket Validator
Bracket Validator
This problem requires you to determine whether a given string containing only the characters (')
, {'}
, and [']
is valid or not. A string is considered valid if:
- Every opening bracket (
(
,{
,[
) is closed by the same type of bracket. - Brackets are closed in the correct order.
For example, the string ()[]{}{}
is valid, while the string (]
is not. If you encounter an expression like ()
or even an empty string, it should be considered valid. In mathematical terms, if we denote the set of valid expressions as S, the problem is to decide if a given expression s belongs to S.
The matching rules can sometimes be succinctly represented using a stack where the last opened bracket is expected to be closed first, i.e. LIFO (Last-In-First-Out). Formally, if S is the stack of open brackets then for every closing bracket c the following must hold:
$$ S.top() = \text{matching opener of } c$$
inputFormat
The input consists of a single line containing a string s that comprises only the characters: (')
, {'}
, and [']
. This string is provided via standard input (stdin).
outputFormat
Output a single line to standard output (stdout) containing either True
if the string is valid or False
if it is not.
()
True