Problems

Longest Valid Parentheses

hard
hard
strings
stack
dynamic-programming

Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.

Constraints

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

Examples

Example 1

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

Example 2

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

Empty

Input: s = ""
Output: 0

Mixed

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

Running will execute all 4 cases.