Beats 100% || Easy Self Explanatory Code || 3 Approaches
3 Approaches:\n\n\n# Approach 1\nTime O(N) Space O(N)\ncpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        stack<char> S;\n       
# 3 Approaches:\n![image.png](https://assets.leetcode.com/users/images/23edaab9-1981-4d97-b13b-5f72e1d27c91_1728444291.3986533.png)\n\n# Approach 1\n**Time O(N) Space O(N)**\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        stack<char> S;\n        int c = 0;\n        for (char i : s) {\n            if (i == \'(\')\n                S.push(i);\n            else {\n                if (S.empty())\n                    c++;\n                else\n                    S.pop();\n            }\n        }\n        return c + S.size();\n    }\n};\n```\n\n# Approach 2\n**Time O(N) Space O(N)**\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        string stck;\n        for (auto c : s) {\n            if (stck.empty())\n                stck.push_back(c);\n\n            else if (stck.back() == \'(\' && c == \')\') {\n                stck.pop_back();\n            } else {\n                stck.push_back(c);\n            }\n        }\n        return stck.size();\n    }\n};\n```\n\n# Approach 3\n**Time O(N) Space O(1)**\n``` cpp []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int open = 0, close = 0;\n        for (auto c : s) {\n            if (c == \'(\') {\n                open++;\n            } else {\n                if (open > 0)\n                    open--;\n                else\n                    close++;\n            }\n        }\n        return open + close;\n    }\n};\n```\n\n![image.png](https://assets.leetcode.com/users/images/23edaab9-1981-4d97-b13b-5f72e1d27c91_1728444291.3986533.png)\n\n
['String', 'Stack', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Rust', 'Ruby', 'JavaScript']