Easy To Understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        max_length = 0\n        stack = [-1]  # Initialize with a start index\n\n        for i in range(len(s)):\n            if s[i] == \'(\':\n                stack.append(i)\n            else:\n                stack.pop()\n                if not stack:\n                    stack.append(i)  # If popped -1, add a new start index\n                else:\n                    # Update the length of the valid substring\n                    max_length = max(max_length, i - stack[-1])\n\n        return max_length\n\n```
['Python3']