Simple python solution


  • 0
    S
    class Solution(object):
    def longestValidParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        stack=[]
        for i in range (0, len(s)):
            if not stack: stack.append(i)
            else:
                if s[i] ==")" and s[stack[-1]] == "(":
                    stack.pop()
                    continue
                stack.append(i)
        end = len(s)
        if not stack: return end
        res = 0
        while stack:
            start = stack.pop()
            res = max(res, end-start-1)
            end = start
        res = max(res, end)
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.