Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
428 B
4
Indexable
Never
import functools

class Solution:
    def longestValidParentheses(self, s: str) -> int:
        @functools.lru_cache(maxsize=None)
        def dfs(i, l, r):
            if i == len(s):
                return 2 * min(l, r)

            if s[i] == "(":
                l += 1
            else:
                r += 1
            if r > l:
                l = r = 0

            return dfs(i + 1, l, r)

        return dfs(0, 0, 0)
Leave a Comment