My ten lines python solution
let dp[i] is the number of longest valid  Parentheses ended with the i - 1 position of s, then we have the following relationship:\ndp[i + 1] = dp[p] + i - p + 
   let dp[i] is the number of longest valid  Parentheses ended with the i - 1 position of s, then we have the following relationship:\ndp[i + 1] = dp[p] + i - p + 1 where p is the position of '(' which can matches current ')' in the stack.\n\n      def longestValidParentheses(self, s):\n        dp, stack = [0]*(len(s) + 1), []\n        for i in range(len(s)):\n            if s[i] == '(':\n                stack.append(i)\n            else:\n                if stack:\n                    p = stack.pop()\n                    dp[i + 1] = dp[p] + i - p + 1\n        return max(dp)
[]