✅C++ || 2 approach || Easy and Simple
Please Upvote If It Helps\n\nApproach : 1 (Using Two Pointer)\n\n\n\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n       int open=0, 
**Please Upvote If It Helps**\n\n**Approach : 1 (Using Two Pointer)**\n\n\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n       int open=0, close=0;\n        int maxl = 0;\n        for(int i=0; i<s.size(); i++)\n        {\n            if(s[i] == \'(\')\n                open++;\n            else\n                close++;\n            if(close==open)\n                maxl = max(maxl, 2*close);\n            else if(close>=open)\n                open=0, close=0;\n            \n        }\n        \n        open = 0, close = 0;\n        for(int i = s.size()-1; i>=0; i--)\n        {\n            if(s[i]==\'(\')\n                open++;\n            else\n                close++;\n            if(close==open)\n                maxl = max(maxl, 2*open);\n            else if(open>=close)\n                open = 0, close = 0;\n        }\n        return maxl;\n    }\n};\n```\n\n\n\n**Approach : 2 (Using Stack)**\n\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int> stk;\n        stk.push(-1);\n        int maxLength = 0;\n        for(int i=0; i<s.size(); i++)\n        {\n            if(s[i] == \'(\')\n            {\n                stk.push(i);\n            }\n            else\n            {\n                stk.pop();\n                \n                if(stk.empty())\n                {\n                    stk.push(i);\n                }\n                else\n                {\n                    maxLength = max(maxLength, i-stk.top());\n                }\n            }\n        }\n        return maxLength;\n    }\n};\n```
['Two Pointers', 'Stack', 'C++']