Practice/Meta/Leetcode 32. Longest Valid Parentheses
CodingMust
Given a string containing only the characters '(' and ')', determine the length of the longest contiguous substring that forms a valid (properly matched and balanced) sequence of parentheses.
A valid parentheses string means:
You need to find the maximum length among all possible valid substrings within the input string.
"(())" and adjacent patterns like "()()"'(' and ')' charactersExample 1:
Input: s = "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" at the end, which has length 2.
Example 2:
Input: s = ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()", which has length 4.
Example 3:
Input: s = "(()())" Output: 6 Explanation: The entire string is a valid parentheses sequence with length 6.
Example 4:
Input: s = "" Output: 0 Explanation: An empty string has no parentheses, so the answer is 0.