Minimum Number of flips to make binary string alternating
Code
# Code\n```java []\nclass Solution {\n    public int minFlips(String s) {\n        int n = s.length();\n        \n        int ans = Integer.MAX_VALUE;\n        int ans1 = 0;\n        int ans2 = 0;\n        for (int i = 0; i < 2 * n; i++) {\n            char ch1 = i % 2 == 0 ? \'0\' : \'1\';\n            char ch2 = i % 2 == 0 ? \'1\' : \'0\';\n            if (ch1 != s.charAt(i % n)) {\n                ans1++;\n            }\n            if (ch2 != s.charAt(i % n)) {\n                ans2++;\n            }\n\n            if (i >= n) {\n                char ch3 = (i - n) % 2 == 0 ? \'0\' : \'1\';\n                char ch4 = (i - n) % 2 == 0 ? \'1\' : \'0\';\n                if (ch3 != s.charAt(i - n)) {\n                    ans1--;\n                }\n                if (ch4 != s.charAt(i - n)) {\n                    ans2--;\n                }\n            }\n\n            if (i >= n - 1) {\n                ans = Math.min(ans, Math.min(ans1, ans2));\n            }\n        }\n        return ans;\n    }\n}\n```
['String', 'Greedy', 'Sliding Window', 'Java']