🔥 BEATS ~100% | ✅ [ C++ / Java / Py / C / C# / JS / GO ]
\nC++ []\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        int open = 0;\n        for (int i = 0; i < s.size(); ++i) {\n         
\n```C++ []\nclass Solution {\npublic:\n    string minRemoveToMakeValid(string s) {\n        int open = 0;\n        for (int i = 0; i < s.size(); ++i) {\n            if (s[i] == \'(\') {\n                ++open;\n            } else if (s[i] == \')\') {\n                if (open == 0) {\n                    s.erase(i, 1);\n                    --i; // Adjust index after erasing\n                } else {\n                    --open;\n                }\n            }\n        }\n\n        // Remove excessive \'(\'\n        for (int i = s.size() - 1; open > 0 && i >= 0; --i) {\n            if (s[i] == \'(\') {\n                s.erase(i, 1);\n                --open;\n            }\n        }\n\n        return s;\n    }\n};\n\n```\n```python []\nclass Solution:\n    def minRemoveToMakeValid(self, s: str) -> str:\n        stack = []\n        s = list(s)\n        \n        # First pass: track indices of \'(\' and mark \')\' for removal\n        for i, char in enumerate(s):\n            if char == \'(\':\n                stack.append(i)\n            elif char == \')\':\n                if stack:\n                    stack.pop()\n                else:\n                    s[i] = \'\'  # Mark \')\' for removal\n        \n        # Mark remaining \'(\' for removal\n        while stack:\n            s[stack.pop()] = \'\'  # Mark \'(\' for removal\n        \n        return \'\'.join(s)\n\n```\n```Java []\nimport java.util.Stack;\n\nclass Solution {\n    public String minRemoveToMakeValid(String s) {\n        StringBuilder sb = new StringBuilder(s);\n        Stack<Integer> stack = new Stack<>();\n\n        // First pass: remove excessive \')\' and track \'(\' indices\n        for (int i = 0; i < sb.length(); ++i) {\n            if (sb.charAt(i) == \'(\') {\n                stack.push(i);\n            } else if (sb.charAt(i) == \')\') {\n                if (!stack.isEmpty()) {\n                    stack.pop();\n                } else {\n                    sb.setCharAt(i, \'*\'); // Mark \')\' for removal\n                }\n            }\n        }\n\n        // Mark excessive \'(\' for removal\n        while (!stack.isEmpty()) {\n            sb.setCharAt(stack.pop(), \'*\');\n        }\n\n        // Construct result string\n        StringBuilder result = new StringBuilder();\n        for (int i = 0; i < sb.length(); ++i) {\n            if (sb.charAt(i) != \'*\') {\n                result.append(sb.charAt(i));\n            }\n        }\n\n        return result.toString();\n    }\n}\n\n```\n```C []\n\nchar* minRemoveToMakeValid(char* s) {\n    int len = strlen(s);\n    char* result = (char*)malloc((len + 1) * sizeof(char));\n    int* stack = (int*)malloc(len * sizeof(int));\n    int top = -1;\n\n    // First pass: track indices of \'(\' and mark \')\' for removal\n    for (int i = 0; i < len; i++) {\n        if (s[i] == \'(\') {\n            stack[++top] = i;\n        } else if (s[i] == \')\') {\n            if (top >= 0) {\n                top--;\n            } else {\n                s[i] = \'\\0\'; // Mark \')\' for removal\n            }\n        }\n    }\n\n    // Mark remaining \'(\' for removal\n    while (top >= 0) {\n        s[stack[top--]] = \'\\0\'; // Mark \'(\' for removal\n    }\n\n    // Construct result string\n    int resultIndex = 0;\n    for (int i = 0; i < len; i++) {\n        if (s[i] != \'\\0\') {\n            result[resultIndex++] = s[i];\n        }\n    }\n    result[resultIndex] = \'\\0\';\n\n    free(stack);\n    return result;\n}\n\n```\n```C# []\npublic class Solution {\n    public string MinRemoveToMakeValid(string s) {\n        Stack<int> stack = new Stack<int>();\n        StringBuilder result = new StringBuilder(s);\n\n        // First pass: track indices of \'(\' and mark \')\' for removal\n        for (int i = 0; i < result.Length; i++) {\n            if (result[i] == \'(\') {\n                stack.Push(i);\n            } else if (result[i] == \')\') {\n                if (stack.Count > 0) {\n                    stack.Pop();\n                } else {\n                    result[i] = \'*\'; // Mark \')\' for removal\n                }\n            }\n        }\n\n        // Mark remaining \'(\' for removal\n        while (stack.Count > 0) {\n            result[stack.Pop()] = \'*\'; // Mark \'(\' for removal\n        }\n\n        // Construct result string\n        StringBuilder finalResult = new StringBuilder();\n        foreach (char c in result.ToString()) {\n            if (c != \'*\') {\n                finalResult.Append(c);\n            }\n        }\n\n        return finalResult.ToString();\n    }\n}\n\n```\n```Javascript []\nvar minRemoveToMakeValid = function(s) {\n    const stack = [];\n    const chars = s.split(\'\');\n\n    // First pass: track indices of \'(\' and mark \')\' for removal\n    for (let i = 0; i < chars.length; i++) {\n        if (chars[i] === \'(\') {\n            stack.push(i);\n        } else if (chars[i] === \')\') {\n            if (stack.length > 0) {\n                stack.pop();\n            } else {\n                chars[i] = \'\'; // Mark \')\' for removal\n            }\n        }\n    }\n\n    // Mark remaining \'(\' for removal\n    while (stack.length > 0) {\n        chars[stack.pop()] = \'\'; // Mark \'(\' for removal\n    }\n\n    // Construct result string\n    var minRemoveToMakeValid = function(s) {\n    const stack = [];\n    const chars = s.split(\'\');\n\n    // First pass: track indices of \'(\' and mark \')\' for removal\n    for (let i = 0; i < chars.length; i++) {\n        if (chars[i] === \'(\') {\n            stack.push(i);\n        } else if (chars[i] === \')\') {\n            if (stack.length > 0) {\n                stack.pop();\n            } else {\n                chars[i] = \'\'; // Mark \')\' for removal\n            }\n        }\n    }\n\n    // Mark remaining \'(\' for removal\n    while (stack.length > 0) {\n        chars[stack.pop()] = \'\'; // Mark \'(\' for removal\n    }\n\n    // Construct result string\n    return chars.join(\'\');\n};\n\n};\n\n```\n```Go []\nfunc minRemoveToMakeValid(s string) string {\n    var stack []int\n    chars := []rune(s)\n\n    // First pass: track indices of \'(\' and mark \')\' for removal\n    for i, char := range chars {\n        if char == \'(\' {\n            stack = append(stack, i)\n        } else if char == \')\' {\n            if len(stack) > 0 {\n                stack = stack[:len(stack)-1]\n            } else {\n                chars[i] = 0 // Mark \')\' for removal\n            }\n        }\n    }\n\n    // Mark remaining \'(\' for removal\n    for _, index := range stack {\n        chars[index] = 0 // Mark \'(\' for removal\n    }\n\n    // Construct result string\n    result := make([]rune, 0, len(chars))\n    for _, char := range chars {\n        if char != 0 {\n            result = append(result, char)\n        }\n    }\n\n    return string(result)\n}\n\n```\n\n\n```\n//  ------------------- UPVOTE PLZ IF HELPFULL -------------------//\n```
['C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']