#C10597. Valid Event Sequence
Valid Event Sequence
Valid Event Sequence
You are given a sequence of events represented as strings. Each event is either entry
or exit
. The sequence is considered valid if at every point in the execution, an exit
event does not occur before a corresponding entry
event. In other words, while scanning the sequence from left to right, maintain a counter which increases by 1 for an entry
and decreases by 1 for an exit
. The sequence is invalid if the counter would become negative at any step. It is acceptable for some entry
events to remain unmatched at the end.
The validity condition can be formulated as follows using LaTeX: If we let \( c(i) \) denote the counter after the first \( i \) events, then the sequence is valid if and only if \[ \forall i,\ c(i) \ge 0. \]
inputFormat
The input is given via standard input (stdin). The first line contains an integer \( n \) representing the number of events. The second line contains \( n \) space-separated strings, where each string is either entry
or exit
.
outputFormat
Output a single line to standard output (stdout): True
if the sequence of events is valid, and False
otherwise.
4
entry exit entry exit
True