C++ || Beats 93% 🔥|| ✅Easy Solution with Explanation || Stack
Approach\n\n- If opening parenthesis \'(\':\n  - Push its index into stack.\n- If closing parenthesis \')\' , then two cases arises:\n  - stack is not empty {me
# Approach\n\n- If opening parenthesis \'(\':\n  - Push its index into stack.\n- If closing parenthesis \')\' , then two cases arises:\n  - stack is not empty {means there is a matching opening parenthesis}, pop the top index from the stack.\n  - stack is empty, means there is no matching opening parenthesis for this closing parenthesis, so erase it from the string and decrement the loop index.\n- After processing all parentheses, remove any remaining unmatched opening parentheses by erasing them from the string.\n- Return the modified string.\n\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        stack<int> st;\n        for (int i = 0; i < s.length(); i++) {\n            if(s[i] == \'(\'){\n                st.push(i);\n            }else if(s[i] == \')\'){\n                if(!st.empty()) st.pop();\n                else{\n                    s.erase(i, 1);\n                    i--;\n                }\n            }\n        }\n        while (!st.empty()) {\n            int pos = st.top();\n            st.pop();\n            s.erase(pos,1);\n        }\n        return s;\n    }\n};\n```
['String', 'Stack', 'C++']