Python using stacks O(n). Beats 97%
Simple stack implementation in Python.\n\n```\ndef longestValidParentheses(self, s: str) -> int:\n        stack = [0,] # Initial value to handle "()"\n        m
Simple stack implementation in Python.\n\n```\ndef longestValidParentheses(self, s: str) -> int:\n        stack = [0,] # Initial value to handle "()"\n        max_parenthesis = 0\n        for bracket in s:\n            if bracket == \'(\':\n                stack.append(0)\n            else:\n                if len(stack) > 1:\n                    val = stack.pop()\n                    stack[-1] += val + 2  # Add 2 when a ")" matches "("\n                    max_parenthesis = max(max_parenthesis, stack[-1]) # Keep track of longest valid sequence\n                else:\n                    stack = [0]\n\n        return max_parenthesis\n\t
['Stack', 'Python']