Beats 100% || C++
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        int n = s.size();\n        stack <int>st;\n        st.push(-1);\n        int len_max = 0;\n        for(int i=0;i<s.size();i++){\n            if(s[i]==\'(\'){\n                st.push(i);\n            }\n            \n            else{\n                st.pop();\n                if(st.empty()){\n                  st.push(i);\n                }\n                else {\n                    int len = i-st.top();\n                 len_max = max(len,len_max);\n                   \n                }\n            }\n            \n        }\n        return len_max;\n    }\n};\n```
['C++']