Left and right traversal || O(n) time complexity || O(1) space complexity || Easy ||  0ms
\n\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        int left=0,right=0,ansLeft=0,ansRight=0;\n        \n        // for left trav
\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        int left=0,right=0,ansLeft=0,ansRight=0;\n        \n        // for left traversal \n        for(int i=0;i<s.size();i++){\n            if(s[i]==\'(\')\n                left++;\n            if(s[i]==\')\')\n                right++;\n            if(right>left) // if count of closing brackets becomes greater than opening bracket\n            {\n                right=0;\n                left=0;\n            }\n            else if(right==left)\n              ansLeft=max(ansLeft,left+right);\n        }\n        left=0;\n        right=0;\n        // for right traversal\n        for(int i=s.size()-1;i>=0;i--){\n            if(s[i]==\'(\')\n                left++;\n            if(s[i]==\')\')\n                right++;\n            if(left>right) // if count of opening brackets becomes greater than closing bracket\n            {   \n                left=0;\n                right=0;\n                }\n            else if(left==right)\n              ansRight=max(ansRight,left+right);\n        }\n        return max(ansLeft,ansRight);\n    }\n};\n```
['C', 'C++']