✅100% Beats simple solution✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is to keep a balance between open and close parentheses while iterating throug
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to keep a balance between open and close parentheses while iterating through the string. If there are more close parentheses than open ones at any point, you should track the extra close parentheses needed. At the end, the remaining unmatched open parentheses should be counted as well.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Traverse the string and track unmatched open (() and close ()) parentheses.\n \n2. For each (, increment the count of open parentheses.\n\n3. For each ), if there are unmatched open parentheses, decrement the open count; otherwise, increment the count of unmatched close parentheses.\n\n4. The final result is the sum of unmatched open and close parentheses, which gives the minimum additions needed to make the string valid.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n   O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n   O(1)\n```c++ []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int open = 0, close = 0;\n        for (char c : s) {\n            if (c == \'(\') {\n                open++;\n            } else {\n                if (open > 0) {\n                    open--;\n                } else {\n                    close++;\n                }\n            }\n        }\n        return open + close;\n    }\n};\n\n```\n```python []\nclass Solution:\n    def minAddToMakeValid(self, s: str) -> int:\n        open = close = 0\n        for c in s:\n            if c == \'(\':\n                open += 1\n            else:\n                if open > 0:\n                    open -= 1\n                else:\n                    close += 1\n        return open + close\n\n```\n```java []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n       int open=0;\n       int close=0;\n       for(int i=0;i<s.length();i++){\n        if(s.charAt(i)==\'(\'){\n            open++;\n        }\n        else if(s.charAt(i)==\')\'){\n            if (open > 0) {\n                    open--;  \n                } else {\n                    close++; \n                }\n           }\n       } \n       return open+close;\n    }\n}\n```
['String', 'Python', 'C++', 'Java']