#K74267. Valid Parentheses Sequence Checker
Valid Parentheses Sequence Checker
Valid Parentheses Sequence Checker
You are given a string S consisting solely of the characters (')
, ')'
, '{'
, '}'
, '['
and ']'
. Your task is to determine whether the sequence of parentheses is valid.
A sequence is considered valid if:
- Every opening bracket has a corresponding closing bracket.
- Brackets are closed in the correct order.
An empty string is also considered valid.
This can be formally defined using the following recursive formulation:
$$\text{valid} = \epsilon \quad \text{or} \quad valid = (A) \quad \text{or} \quad valid = [A] \quad \text{or} \quad valid = \{A\} \quad \text{or} \quad valid = AB$$
where \(A\) and \(B\) are valid sequences.
inputFormat
The input is read from standard input (stdin). It consists of a single line containing the string S, which includes only the characters '(', ')', '{', '}', '[' and ']'.
outputFormat
Print a single line to standard output (stdout) containing "YES" if the sequence is valid, otherwise print "NO".## sample
()
YES