Longest well-formed parentheses stretch
s contains only the characters ( and ). A stretch is well formed when it has as many ( as ) and, read from left to right, never has more ) than ( so far, like "(()())" or "()()". Return the length of the longest contiguous well-formed stretch of s, or 0 if there is none.
Aim for O(n): checking every stretch is O(n²) at best.
Besides the tests, one large input (6,000 parentheses) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
longest_well_formed("(()")→2longest_well_formed(")()())")→4longest_well_formed("()(())")→6