Java O(N) using set, sliding window
\nclass Solution {\n    public int minFlips(String s) {\n        int min = Integer.MAX_VALUE;\n        int n = s.length();\n        Set<Integer> set = new HashS
```\nclass Solution {\n    public int minFlips(String s) {\n        int min = Integer.MAX_VALUE;\n        int n = s.length();\n        Set<Integer> set = new HashSet<>();\n        Set<Integer> set2 = new HashSet<>();\n        int expected = s.charAt(0) - \'0\';\n        int expected2 = 1 - (s.charAt(0) - \'0\');\n        for (int i = 0; i < 2*n; i++) {\n            if (s.charAt(i % n) - \'0\' != expected) {\n                set.add(i);\n            }\n            if (s.charAt(i % n) - \'0\' != expected2) {\n                set2.add(i);\n            }\n            expected = 1 - expected;\n            expected2 = 1 - expected2; \n            if (i > n - 1) {\n                set.remove(i - n);\n                set2.remove(i - n);\n            }\n            if (i >= n - 1) {\n                min = Math.min(set.size(), Math.min(set2.size(), min));\n            }\n        }\n        return min;\n    }\n}\n```
[]