c++ 10 lines easy stack solution O(n) TC using stack
\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int>stck;\n        stck.push(-1);\n        int res=0;\n        \n       
```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int>stck;\n        stck.push(-1);\n        int res=0;\n        \n        for(int i=0;i<s.size();i++){\n            \n            if(s[i]==\'(\') stck.push(i);\n            \n            else{\n                if(!stck.empty())stck.pop();\n                \n                if(!stck.empty()){\n                    res=max(res,i-stck.top());\n                }\n                else stck.push(i);\n            }\n        }\n        \n        return res;\n    }\n};\n```
['Stack', 'C', 'C++']