Effcient Aproach || 0 ms || C++ || Minimum Add to Make Parentheses Valid
Intuition\n Describe your first thoughts on how to solve this problem. \nHere, for every opening parentheses \'(\', there should be corresponding closing bracke
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere, for every opening parentheses \'(\', there should be corresponding closing bracket \')\' to make a string valid. We, iterate the string from start to end then the closing parenthesis should not exceed the opening parenthesis.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. To make string valid, we intialize variable ans and count as 0. \n2. Here, ans is a total no. of moves and count will track the opening & closing braces as we iterate.\n3. If size is 0 then return 0 only.\n4. We iterate a string from left to right, if s[i] is a opening parenthese, increment the ans.\n5. else, If s[i] is a closing parenthese, decrement the ans.\n6. If, ans < 0, then increment count and set ans as 0.\n7. At last, return sum of count and ans.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int n = s.size();\n\n        int ans = 0;\n        int count  = 0;\n\n        if(n == 0)\n        {\n            return 0;\n        }\n\n        for(int i=0; i<n; i++)\n        {\n            if(s[i] == \'(\')\n            {\n                ans++;\n            }\n            else if(s[i] == \')\')\n            {\n                ans--;\n            }\n\n            if(ans < 0)\n            {\n                count++;\n                ans = 0;\n\n            }\n        }\n\n        return count + ans;\n    }\n};\n```
['String', 'Stack', 'Greedy', 'C++']