Beats 100%, simple easy to understand, and beginner-friendly using stack
\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n    s
\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n    stack<char> st;  // Stack to keep track of unmatched \'(\' characters\n    int n = s.size();  // Length of the input string\n    int count = 0;  // Counter for unmatched \')\' characters\n\n    // Loop through each character in the string\n    for (int i = 0; i < n; i++) {\n        if (s[i] == \'(\') {  \n            st.push(s[i]);  // Push \'(\' onto the stack to track unmatched \'(\'\n        } else if (s[i] == \')\') {  // When encountering a \')\'\n            if (!st.empty() && st.top() == \'(\') {\n                st.pop();  // If there\'s a matching \'(\', pop it from the stack\n            } else {\n                count++;  // If there\'s no matching \'(\', increment the count of unmatched \')\'\n            }\n        }\n    }\n\n    // Add the remaining unmatched \'(\' in the stack to the count\n    count += st.size();\n\n    return count;  // Return the total number of insertions needed\n    }\n};\n```
['String', 'Stack', 'Greedy', 'C++']