Bracket Validation Pipeline
A configuration processor maintains a dynamic string of brackets and applies a stream of operations to it. After each operation, you must validate whether the bracket sequence is currently balanced.
A bracket sequence is balanced if:
- It contains an equal number of opening and closing brackets.
- At no point while reading left to right does the count of closing brackets exceed the count of opening brackets.
- Brackets are properly matched:
(matches with),[matches with],{matches with}.
The processor supports three operations on the bracket string:
- INSERT
posbracket: Insert the given bracket at positionpos(0-indexed). Existing characters from positionposonward shift right. - DELETE
pos: Remove the character at positionpos. - VALIDATE: Check if the current bracket sequence is balanced. Output
YESorNO.
Input
The first line contains n (1 ≤ n ≤ 1000), the number of operations.
Each of the next n lines describes an operation:
INSERT pos bracket: Insert a bracket (one of(,),[,],{,}) at positionpos(0 ≤ pos ≤ current length).DELETE pos: Delete the bracket at positionpos(0 ≤ pos < current length).VALIDATE: Validate the current sequence.
Output
For each VALIDATE operation, print YES if the sequence is balanced, NO otherwise.
Constraints
- 1 ≤ n ≤ 1000
- After all operations, the string length is at most 10000
- Position indices are valid (0 ≤ pos ≤ string length for INSERT, 0 ≤ pos < string length for DELETE)