260. Longest Valid Parentheses

HardStringString

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

Input: A quoted parentheses string.

Output: Integer — length of longest valid substring.

Examples

Example 1
Input: "(()"
Output: 2
Explanation: '()' has length 2.
Example 2
Input: ")()())"
Output: 4
Explanation: '()()' has length 4.
Example 3
Input: ""
Output: 0
Explanation: Empty string.

Constraints

Asked by

MetaAmazonIBMMicrosoftBloombergGoogle
Solve this problem in the editor →