Easy stack based approach in C++
\nclass Solution \n{\npublic:\n    int longestValidParentheses(string s) \n    {\n        stack<int>stk;\n        int ans=0;\n        stk.push(-1);\n        for
```\nclass Solution \n{\npublic:\n    int longestValidParentheses(string s) \n    {\n        stack<int>stk;\n        int ans=0;\n        stk.push(-1);\n        for(int i=0;i<s.length();i++)\n        {\n            if(s[i]==\'(\')\n            {\n                stk.push(i);\n            }\n            else\n            {\n                stk.pop();\n                if(!stk.empty())\n                {\n                    ans=max(ans,i-stk.top());\n                }\n                else\n                {\n                    stk.push(i);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n***Please upvote if you have got any help from my code. Thank you.***
['C']