Python No DP No Stack O(n)
Logic: Counting left and right parenthesis\n\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        m = left = right = 0\n        \n  
Logic: Counting left and right parenthesis\n```\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        m = left = right = 0\n        \n        for i in s:\n            if i==\'(\':\n                left += 1\n            else:\n                right += 1\n            if left == right:\n                m = max(m, right+left)\n            elif right>left :\n                left = right = 0\n                \n        left = right=0\n        \n        for i in s[::-1]:\n            if i ==\')\':\n                right += 1\n            else:\n                left += 1\n            if left == right:\n                m = max(m,right+left )\n            elif left >right:\n                left =right=0\n\n        return m\n```
['Python']