Longest Valid Parentheses

s holds only ( and ). Return the length of its longest substring that is balanced: every ( closed by a later ), and no ) without a ( before it.

Constraints

  • 0 ≤ length of s ≤ 100,000

Example 1

Input
s = "(()"
Output
2
Why:

The last two characters, ().

Example 2

Input
s = ")()())"
Output
4
Why:

()() in the middle.

Example 3

Input
s = ""
Output
0
Why:

Nothing at all.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –