Converted into longest consecutive 1s, very easy
Approach\n Describe your approach to solving the problem. \n1. Initialize an array arr of length n, where n is the length of the input string s, filled with zer
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize an array arr of length n, where n is the length of the input string s, filled with zeros.\n2. Initialize an empty stack.\n3. Iterate through each character s[i] in the input string s.\n4. If s[i] is \'(\', push its index i onto the stack.\n5. If s[i] is \')\' and the stack is not empty, mark the corresponding opening parenthesis index and the current index as valid by setting arr[stack.pop()] = 1 and arr[i] = 1.\n6. Now we need to find longest consecutive 1s in the array :)\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        n = len(s)\n        arr = [0]*n\n        stack = []\n\n        for i in range(n):\n            if s[i]==\'(\':\n                stack.append(i)\n            if s[i]==\')\' and stack:\n                arr[stack.pop()]=1\n                arr[i]=1\n        \n        ans = 0\n        temp = 0\n        for i in arr:\n            if i==1:\n                temp+=1\n                ans = max(ans,temp)\n            else:\n                temp=0\n\n        return ans\n        \n```
['Python3']