Java Solution - Double Input String & Sliding window
\nclass Solution {\n    public int minFlips(String s) {\n        StringBuilder sb1 = new StringBuilder();\n        StringBuilder sb2 = new StringBuilder();\n   
```\nclass Solution {\n    public int minFlips(String s) {\n        StringBuilder sb1 = new StringBuilder();\n        StringBuilder sb2 = new StringBuilder();\n        int n = s.length();\n        for (int i = 0; i < 2 * n; i++) {\n            if (i % 2 == 0) {\n                sb1.append(\'1\');\n                sb2.append(\'0\');\n            } else {\n                sb1.append(\'0\');\n                sb2.append(\'1\');\n            }\n        }\n        // construct two alternating strings\n        String s1 = sb1.toString();\n        String s2 = sb2.toString();\n        // double the input string\n        s += s;\n        int ans1 = 0;\n        int ans2 = 0;\n        int ans = n;\n        // use sliding window to compare \n        for (int i = 0; i < 2 * n; i++) {\n            if (s.charAt(i) != s1.charAt(i)) {\n                ans1++;\n            }\n            if (s.charAt(i) != s2.charAt(i)) {\n                ans2++;\n            }\n            if (i >= n) {   // left-most char is getting out of the sliding window\n                if (s.charAt(i - n) != s1.charAt(i - n)) {\n                    ans1--;\n                }\n                if (s.charAt(i - n) != s2.charAt(i - n)) {\n                    ans2--;\n                }\n            }\n            // get min ans\n            if (i >= n - 1) {\n                ans = Math.min(Math.min(ans1, ans2), ans);\n            }\n        }\n        return ans;\n    }\n}\n```
[]