<p>Given a string containing just the characters <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>, return <em>the length of the longest valid (well-formed) parentheses </em><span data-keyword="substring-nonempty"><em>substring</em></span>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(()&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest valid parentheses substring is &quot;()&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;)()())&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest valid parentheses substring is &quot;()()&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;&quot;
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>s[i]</code> is <code>&#39;(&#39;</code>, or <code>&#39;)&#39;</code>.</li>
</ul>

class Solution:
    def longestValidParentheses(self, s: str) -> int:
        # keep track of valid paranethese
        # if they are continuous join them
        valids = []
        pointer = 0

        stack = []

        for index, c in enumerate(s):
            if self.isClose(c):
                if not stack:
                    continue
    
                opening = stack.pop()
                pair = self.isPair(opening[0], c)
                if not pair:
                    continue

                # check if continuous
                if not valids:
                    valids.append([opening[1], index])
                else:
                    continuous = valids[pointer][1] + 1 == opening[1]
                    if valids and continuous:
                        valids[pointer][1] = index
                    else:
                        valids.append([opening[1], index])
                        pointer += 1
            else:
                stack.append((c, index))

        # edge case
        if not valids:
            return 0

        # there might be overlaps again
        # example: "()(())" -> [[0, 1], [3, 4], [2, 5]]
        valids.sort(key=lambda x: x[0])
        overlaps = [valids[0]]
        pointer = 0

        for i in range(1, len(valids)):
            continuous = overlaps[pointer][1] + 1 >= valids[i][0]
            if continuous:
                overlaps[pointer][1] = max(overlaps[pointer][1], valids[i][1])
            else:
                overlaps.append(valids[i])
                pointer += 1
        
        # find the longest parentheses
        longest = 0
        for c in overlaps:
            longest = max(longest, c[1] - c[0] + 1)
        return longest

    
    def isClose(self, p):
        return p == ")" or p == "]" or p == "}"

    def isPair(self, l, r):
        rounded = ["(", ")"]
        square = ["[", "]"]
        curly = ["{", "}"]

        return (l in rounded and r in rounded) \
            or (l in square and r in square) \
            or (l in curly and r in curly)

18000