Longest Valid Parentheses

Hard · Stack & Monotonic Stack · about 30 minutes

Asked at Meta, ByteDance, Amazon, Microsoft, Google, Bloomberg, Jane Street, Susquehanna (SIG).

s is made only of '(' and ')'. Find the longest contiguous piece of s whose brackets are properly matched and nested, and return its length.