JAVA Sliding window O(n) code as explaination
\n     public int minFlips(String s) {\n        int n = s.length();\n\t\t// cnt: number of flips for starting as \'10\'\n\t\t// cnt2: number of flips for starti
```\n     public int minFlips(String s) {\n        int n = s.length();\n\t\t// cnt: number of flips for starting as \'10\'\n\t\t// cnt2: number of flips for starting as \'01\'\n        int cnt = 0, cnt2 = 0;\n        for(int i = 0; i < s.length(); i++) {\n            if (i % 2 == 0 && s.charAt(i) != \'1\') cnt++;\n            if (i % 2 == 1 && s.charAt(i) != \'0\') cnt++;\n            if (i % 2 == 0 && s.charAt(i) != \'0\') cnt2++;\n            if (i % 2 == 1 && s.charAt(i) != \'1\') cnt2++;\n        }\n        s += s;\n        int res = Integer.MAX_VALUE;\n        for (int i = n; i < s.length(); i++) {\n            if (i % 2 == 0 && s.charAt(i) != \'1\') cnt++;\n            if (i % 2 == 1 && s.charAt(i) != \'0\') cnt++;\n            if (i % 2 == 0 && s.charAt(i) != \'0\') cnt2++;\n            if (i % 2 == 1 && s.charAt(i) != \'1\') cnt2++;\n            if ((i - n) % 2 == 0 && s.charAt(i - n) == \'0\') cnt--;\n            if ((i - n) % 2 == 1 && s.charAt(i - n) == \'1\') cnt--;\n            if ((i - n) % 2 == 0 && s.charAt(i - n) == \'1\') cnt2--;\n            if ((i - n) % 2 == 1 && s.charAt(i - n) == \'0\') cnt2--;\n            res = Math.min(res, Math.min(cnt, cnt2));\n        }\n        return res;\n     }\n```
[]