#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def longestValidParentheses(self , s ):
        # write code here
        res,left,right = 0,0,0
        for c in s:
            if c == '(':
                left += 1
            else :
                right += 1
            if right==left:
                res = max(res,right*2)
            if right > left:
                left,right = 0,0
        left,right = 0,0
        for c in s[::-1]:
            if c ==')':
                right += 1
            else:
                left += 1
            if right == left:
                res = max(res,left*2)
            if left>right:
                left,right = 0,0
                
        return res