Skip to content
Onsite

Longest Valid Parentheses

HardP2Coding

Given a string s containing only ( and ), return the length of the longest substring that is a valid (well-formed) parentheses string. A substring must be contiguous.

Example 1

Input
s = "(()"
Output
2

Example 2

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

Example 3

Input
s = ""
Output
0

Constraints

  • 0 <= s.length <= 3 * 10^4
  • s[i] is '(' or ')'

Hints

Run checks the 3 visible tests, Submit all 14.