68 ms fast python solution (beat 97%), only put starting '(' index in stack. Detailed explaination
class Solution(object):\n        def longestValidParentheses(self, s):\n            """\n            :type s: str\n            :rtype: int\n            """\n   
    class Solution(object):\n        def longestValidParentheses(self, s):\n            """\n            :type s: str\n            :rtype: int\n            """\n            result=0\n            stk=[]\n            lst=-1\n            for i in xrange(len(s)):\n                if s[i]=='(':\n                    if lst!=-1:\n                        stk.append(lst)\n                        lst=-1\n                    else:\n                        stk.append(i)\n                else:\n                    if stk:\n                        stt=stk.pop()\n                        if i-stt+1>result:\n                            result=i-stt+1\n                        lst=stt\n                    else:\n                        lst=-1\n            return result\n\nstk store the index of '('. <br>\nwe intereate through the string. <br>\nresult is the length of the longest unbroken bracket chain at that position, we update it when we iterate through the string. We initiate it to 0.<br>\nlst is -1 or the starting index of the unbroken bracket chain we just extended with a matching ')', its initial value is -1, if we iterate to a '(' or a umatchable ')', we set lst to -1. So we are using lst for two purposes.<br>\nEvery time we see a '(',  we push a index to the stack. if the lst is not -1 (means last time we just got a matching ')' and extended the current unbroken bracket chain and we set lst to the starting index of that chain), we push lst into the stack. If lst is -1, it means we are on index 0 or last character is not a match ')'. We push the index i into the stack. We set lst to -1 in either case.<br>\nEvery time we see a ')' and the stack is not empty ( means it's a matching ')' ),we pop the stack. we calculate the length of the chain and update the result and set the lst to the popped index. If the stack is empty, it means that's a unmatchable ')', and we set lst to -1.<br>\nA lot of times, we pop a index and push it right back like when we iterate to index 2 of string "()()"\nIn the end of the interation we will have the length of the longest chain in the result variable\n\nLet me know if the explaination doesn't make sense or hard to understand. I will try my best to revise it.
[]