[Java] O(n) solution without Stack
Time complexity: O(n).\nSpace complexity: O(n).\n\n\npublic String minRemoveToMakeValid(String str) {\n    int n = str.length();\n    StringBuilder sb = new Str
Time complexity: O(n).\nSpace complexity: O(n).\n\n```\npublic String minRemoveToMakeValid(String str) {\n    int n = str.length();\n    StringBuilder sb = new StringBuilder(n);\n    boolean[] remove = new boolean[n];\n    int open = 0;\n    for (int i = 0; i < n; i++) {\n        if (str.charAt(i) == \'(\') {\n            open++;\n        } else if (str.charAt(i) == \')\') {\n            if (open > 0) {\n                open--;\n            } else {\n                remove[i] = true;\n            }\n        }\n    }\n\n    int close = 0;\n    for (int i = n - 1; i >= 0; i--) {\n        if (str.charAt(i) == \')\') {\n            close++;\n        } else if (str.charAt(i) == \'(\') {\n            if (close > 0) {\n                close--;\n            } else {\n                remove[i] = true;\n            }\n        }\n    }\n\n    for (int i = 0; i < n; i++) {\n        if (!remove[i]) sb.append(str.charAt(i));\n    }\n\n    return sb.toString();\n}\n```
['Java']