Problem editorialhard
Longest valid parentheses
The trick is not storing brackets — it is storing indices. The answer is a length, and lengths are measured between boundaries.
20 min read·time O(n)·space O(n), O(1) variant
Stack of indicesSentinelBoundary arithmetic