O(n) time using HashSet and Stack Java Solution
```\nclass Solution {\n    public String minRemoveToMakeValid(String s) {\n        \n        int n = s.length();\n        if(n == 0) return s;\n        StringBu
```\nclass Solution {\n    public String minRemoveToMakeValid(String s) {\n        \n        int n = s.length();\n        if(n == 0) return s;\n        StringBuilder sb = new StringBuilder();\n        \n        HashSet<Integer> set = new HashSet<>();\n        Stack<Integer> stack = new Stack<>();\n        \n        for(int i = 0; i < n; i++){\n            \n            char curr = s.charAt(i);\n            \n            if(curr == \'(\'){\n                stack.push(i);\n            }\n            \n            else if(curr == \')\'){\n                if(stack.size() > 0){\n                    stack.pop();\n                }\n                else{\n                    set.add(i);\n                }\n            }\n            \n            else continue;\n        }\n        \n        while(stack.size() > 0){\n            set.add(stack.pop());\n        }\n        \n        for(int i = 0; i < n; i++){\n            if(!set.contains(i)){\n                sb.append("" + s.charAt(i));\n            }\n        }\n        \n        return sb.toString();\n        \n    }\n}
[]