JAVA | 100% Beats | Linear Approach with O(1) Space
\n# Code\njava []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n        int set = 0;\n        int res = 0;\n        for (char c : s.toCharArr
\n# Code\n```java []\nclass Solution {\n    public int minAddToMakeValid(String s) {\n        int set = 0;\n        int res = 0;\n        for (char c : s.toCharArray()) {\n            switch (c) {\n                case \'(\' -> set += 1;\n                case \')\' -> set -= 1;\n            }\n            if (set == -1) {\n                set += 1;\n                res += 1;\n            }\n        }\n        return res + set;\n    }\n}\n```
['Java']