C++ Solution ||  O(1) Space and O(n) time || Easy to Understand
Intuition\nThe problem requires determining the minimum number of parentheses that need to be added to make a given string of parentheses valid. A valid parenth
# Intuition\nThe problem requires determining the minimum number of parentheses that need to be added to make a given string of parentheses valid. A valid parentheses string has balanced open and close parentheses. If we encounter a closing parenthesis without a matching opening one, we need to add an opening parenthesis. Similarly, if there are more opening parentheses left unmatched, we need to add closing parentheses.\n\n# Approach\n- `size`: the current balance of unmatched opening parentheses. It is incremented when encountering an opening parenthesis `(`.\n- `cnt`: the number of unmatched closing parentheses `)`. It is incremented when a closing parenthesis is encountered and there are no unmatched opening parentheses to pair with it.\n\n1. For each character in the string:\n    - If the character is an opening parenthesis `(`, increase the `size` (unmatched opening parentheses).\n    - If the character is a closing parenthesis `)`:\n        - If there are unmatched opening parentheses (`size > 0`), decrease the size by 1, as the closing parenthesis can balance an opening one.\n        - If no unmatched opening parentheses are available (`size == 0`), increment the `cnt` (count of unmatched closing parentheses).\n2. At the end of the loop:\n    - `cnt` holds the number of unmatched closing parentheses.\n    - `size` holds the number of unmatched opening parentheses.\n3. The total number of parentheses that need to be added to make the string valid is the sum of `cnt` and `size`.\n\n# Complexity\n- Time complexity: $$O(n)$$ \n\n- Space complexity: $$O(1)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int size = 0;\n        int cnt = 0;\n        for (auto& ch : s) {\n            if (ch == \'(\') {\n                size++;\n            } else {\n                if (size == 0)\n                    cnt++;\n                else\n                    size--;\n            }\n        }\n        return cnt + size;\n    }\n};\n```\n\n> ***Success is not final, failure is not fatal: It is the courage to continue that counts.***\n\n![1721315779911.jpeg](https://assets.leetcode.com/users/images/7ef4dc4e-7b1d-49e7-95bb-84cc3e885721_1728457436.8190918.jpeg)\n
['String', 'C++']