C++ | Short and simple solution
Easy stack based problem.\nComplexities :\n Time : O(n)\n Space : O(n)\n\n\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        \n\t
Easy stack based problem.<br>\nComplexities :\n* **Time :** `O(n)`\n* **Space :** `O(n)`\n\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        \n\t    int maxVal = 0;\n\n    \tstack<int> st;\n\t    st.push(-1);\n\n    \tfor(int i = 0; i < s.size(); i++)\n\t    \tif(s[i] == \'(\')\n                st.push(i);\n\n    \t\telse {       \n\t    \t\tst.pop();\n\n\t\t    \tif(st.empty())\n                    st.push(i);\n    \t\t\telse\n                    maxVal = max(maxVal, i - st.top());\n    \t\t}        \n        \n\t    return maxVal;\n    }\n};\n```
['Stack', 'C', 'C++']