32. Longest Valid Parentheses
Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Example 2:
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
Codeļ¼
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
stack = [0]
longest = 0
for c in s:
if c == "(":
stack.append(0)
else:
if len(stack) > 1:
val = stack.pop()
stack[-1] += val + 2
longest = max(longest, stack[-1])
else:
stack = [0]
return longest