✅C++ solution using STACK in O(n) time complexity with explanation
\n> If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!
\n> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n\n## Explanation:\n\n### Solution 01\n\n- Here we\u2019re using a stack to store the position of parentheses.\n- If it is an open parenthesis then increase the count and push the index to stack.\n- else if it\u2019s a close parenthesis then check if the count is not 0 then decrease the count and pop the last element from the stack. if count=0 then push the index to stack.\n- now iterate the stack and remove the elements from the index present in the stack\n- **Time complexity:** O(n^2).\n\n---\n\n## Code:\n\n```cpp\n//Solution 01:\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        int n = s.size();\n        stack<int> st;\n        int i=0, count=0;\n        while(i<n){\n            if(s[i] == \'(\'){\n                count++;\n                st.push(i);\n            }\n            else if(s[i] == \')\'){\n                if(count > 0){\n                    count--;\n                    st.pop();\n                }\n                else\n                    st.push(i);\n            }\n            i++;\n        }\n        int m = st.size();\n        while(m--){\n            int temp= st.top();\n            s.erase(temp,1);\n            st.pop();\n        }\n        return s;\n    }\n};\n```\n\n---\n\n> **Please upvote this solution**\n>
['Stack', 'C', 'C++']