8-line Python solution, stack, 80ms
To make sure the stack is not empty, first put in a tuple (-1, ')') as it will never be poped out and -1 can be used to calculate result.\n\n    def longestVali
To make sure the stack is not empty, first put in a tuple `(-1, ')')` as it will never be poped out and `-1` can be used to calculate `result`.\n\n    def longestValidParentheses(self, s):\n        stack, result = [(-1, ')')], 0\n        for i, paren in enumerate(s):\n            if paren == ')' and stack[-1][1] == '(':\n                stack.pop()\n                result = max(result, i - stack[-1][0])\n            else:\n                stack += (i, paren),\n        return result
['Stack', 'Python']