```
class Solution(object):
def longestValidParentheses(self, s):
stack, valid = [], [False]*len(s)
for i,v in enumerate(s):
if v == "(":
stack.append(i)
elif len(stack) > 0:
valid[stack.pop()], valid[i] = True, True
max, current = 0, 0
for v in valid:
if v:
current += 1
else:
max = current if current > max else max
current = 0
max = current if current > max else max
return max
```