Bracket Expression Parser
You are given a string containing only bracket characters: (, ), [, ], {, and }.
Determine if the bracket expression is valid or report the position of the first error.
A valid bracket expression satisfies:
- Every opening bracket has a matching closing bracket of the same type.
- Brackets are properly nested (no crossing pairs like
([)]). - There are no unmatched closing brackets.
- There are no unmatched opening brackets at the end.
Input
A single line containing the bracket expression. The string is non-empty and contains only characters from {, }, [, ], (, ).
Output
If the expression is valid, print valid.
Otherwise, print the 1-indexed position of the first bracket that causes an error.
An error is defined as:
- A closing bracket with no matching opening bracket of the same type (report the position of this closing bracket).
- A closing bracket that doesn't match the most recent unmatched opening bracket (report the position of this closing bracket).
- An unmatched opening bracket at the end of the string (report
n+1, wherenis the length of the string).
Constraints
- 1 ≤ n ≤ 10^6 (length of the bracket expression)