Easy C++ Solution using Stack.
Approach\n Describe your approach to solving the problem. \nDefine a stack,\nPush all the \'(\' that come in the way.\nIf you encounter \')\' there\'s nothing i
# Approach\n<!-- Describe your approach to solving the problem. -->\nDefine a stack,\nPush all the \'(\' that come in the way.\nIf you encounter \')\' there\'s nothing in the stack yet, so increase the counter, as we have to add \'(\' to every corresponding \')\'.\nIf stack is non-empty pop an \'(\' from it.\n\nAt last add the counter and stack size as we have to add \')\' to every \'(\' in the stack.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nWe traversed the string once so, $$O(n)$$.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nStack in worst case can use $$O(n)$$ space.\n\n# Code\n```\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        stack<int> st;\n        int cnt=0;\n        for(auto a:s)\n        {\n            if(a==\'(\')\n                st.push(a);\n            else if(a==\')\')\n            {\n                if(!st.empty())\n                    st.pop();\n                else\n                    cnt++;\n            }\n        }\n        return cnt+st.size();\n    }\n};\n```
['C++']