← Discussions on Valid Parentheses
S svarma OFFICIAL
08 Aug 2026, 04:06

Why does my solution TLE on the last test case?

A monotonic stack is the trick: pop while the current element breaks the order, and the popped elements have just found their answer.

3 replies

divya_13 2 months ago

Great explanation, the edge case with an empty input tripped me up too.

abose 2 months ago

Thanks! The dry run on the second example made it click.

arjun_21 2 months ago

Nice. Got asked a variant of this in an interview.

Sign in to reply.