':type s: str
:rtype: int'

def longestValidParentheses(self, s):

    def length(it, start, c):
        (depth, longest) = (0, 0)
        for i in it:
            if (s[i] == c):
                depth += 1
            else:
                depth -= 1
                if (depth < 0):
                    (start, depth) = (i, 0)
                elif (depth == 0):
                    longest = max(longest, abs((i - start)))
        return longest
    return max(length(xrange(len(s)), (-1), '('), length(reversed(xrange(len(s))), len(s), ')'))
