Easy Solution (No Stack)🧠 Python | Java | C++ | C | C# | GO | Rust O(N), O(1)
Intuition\n- Objective: Determine the minimum number of parentheses to add to make a string valid.\n\n- Goal: Keep two counters: one for open parentheses and on
# Intuition\n- **Objective**: Determine the minimum number of parentheses to add to make a string valid.\n\n- **Goal**: Keep two counters: one for open parentheses and one for the number of unmatched closing parentheses.\n\n# Approach\n1. Traverse the string character by character.\n2. Keep track of:\n- Open parentheses that still need to be matched.\n- Unmatched closing parentheses.\n3. For every open parenthesis \'(\', increase the count of open parentheses.\n4. For every closing parenthesis \')\', check if there\'s an unmatched open parenthesis available to pair it with:\n- If yes, decrease the open parenthesis count.\n- If not, increase the unmatched closing parenthesis count.\n5. At the end of the traversal, the result is the **sum** of `unmatched open` and `unmatched closing` parentheses, which indicates how many parentheses are needed to make the string valid.\n6. \n\n# Complexity\n- Time complexity: `O(N)`\n\n- Space complexity: `O(1)`\n\n# Code\n```python3 []\nclass Solution:\n    def minAddToMakeValid(self, s: str) -> int:\n        open_c = close_c = 0\n        for c in s:\n            if c == \'(\':\n                open_c += 1\n            elif c == \')\' and open_c > 0:\n                open_c -= 1\n            else:\n                close_c += 1\n        return open_c + close_c\n```\n\n``` java []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n        int open_c = 0;\n        int close_c = 0;\n\n        for (char c : s.toCharArray()) {\n            if (c == \'(\') {\n                open_c++;\n            } else if (c == \')\' && open_c > 0) {\n                open_c--;\n            } else {\n                close_c++;\n            }\n        }\n        return open_c + close_c;\n    }\n}\n```\n\n``` C++ []\nclass Solution {\npublic:\n    int minAddToMakeValid(string s) {\n        int open_c = 0;\n        int close_c = 0;\n\n        for (char c : s) {\n            if (c == \'(\') {\n                open_c++;\n            } else if (c == \')\' && open_c > 0) {\n                open_c--;\n            } else {\n                close_c++;\n            }\n        }\n        return open_c + close_c;\n    }\n};\n```\n\n``` C# []\npublic class Solution {\n    public int MinAddToMakeValid(string s) {\n        int open_c = 0;\n        int close_c = 0;\n\n        foreach (char c in s) {\n            if (c == \'(\') {\n                open_c++;\n            } else if (c == \')\' && open_c > 0) {\n                open_c--;\n            } else {\n                close_c++;\n            }\n        }\n        return open_c + close_c;\n    }\n}\n```\n\n``` GO []\nfunc minAddToMakeValid(s string) int {\n    open_c, close_c := 0, 0\n\n    for _, c := range s {\n        if c == \'(\' {\n            open_c++\n        } else if c == \')\' && open_c > 0 {\n            open_c--\n        } else {\n            close_c++\n        }\n    }\n    return open_c + close_c\n}\n```\n``` Rust []\nimpl Solution {\n    pub fn min_add_to_make_valid(s: String) -> i32 {\n        let mut open_c = 0;\n        let mut close_c = 0;\n\n        for c in s.chars() {\n            if c == \'(\' {\n                open_c += 1;\n            } else if c == \')\' && open_c > 0 {\n                open_c -= 1;\n            } else {\n                close_c += 1;\n            }\n        }\n        open_c + close_c\n    }\n}\n```\n\n``` C []\nint minAddToMakeValid(char* s) {\n    int open_c = 0, close_c = 0;\n\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (s[i] == \'(\') {\n            open_c++;\n        } else if (s[i] == \')\' && open_c > 0) {\n            open_c--;\n        } else {\n            close_c++;\n        }\n    }\n    return open_c + close_c;\n}\n```\n# Congrats!! You\'ve learned something new today \u2B06\uFE0F\uD83E\uDD29\uD83D\uDC96\uD83D\uDC3E
['C', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'C#']