python
#
#
# @param s string字符串
# @return int整型
#
class Solution:
def longestValidParentheses(self , s ):
# write code here
if not s:
return 0
n = len(s)
dp = [0] * n
for i in range(1, n):
if s[i] == ')':
if s[i-1] == '(' and i >= 2:
dp[i] = dp[i-2] + 2
else:
if s[i-dp[i-1]-1] == '(':
dp[i] = dp[i-1] + 2 + dp[i-dp[i-1]-2]
return max(dp)