C++// Beats 100% - easy approach
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst thought was to keep a count of open bracket and close brackets. The differece wou
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst thought was to keep a count of open bracket and close brackets. The differece would be the moves we need, but it would fail for a testcase like: \') ) ) ( ( (\'.\n\nHence, we need to make a move at each point when the string becomes unbalanced. \nIn the end, we also need to add the number of extra open parentheses in the moves.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Maintain 3 variables: open, close, move.\n- Iterate over the string and keep the count of open and close brackets.\n- If at any iteration the number of close brackets is more than open brackets increment the move. Also increment open, as if we added an open bracket at that index to balance the string up until that point.\n- After we have iterated the whole string we add the difference of open and close bracket in moves. This is for the open brackets that appear at the end of the string. Ex. "( ) ) ) ( (".\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 i = 0, n = s.length();\n        int open = 0, close = 0, move = 0;\n        for(int i = 0;i<n;i++){\n            if(s[i] == \'(\') open++;\n            else if(s[i] == \')\') close++;\n            if(close>open){\n                move++;\n                open++;\n            } \n        }\n        move+= open - close;\n        return move;\n    }\n};\n```
['C++']