You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:

Return the minimum number of type-2 operations you need to perform such that s becomes alternating.

The string is called alternating if no two adjacent characters are equal.

Input: s = "111000"
Output: 2
Explanation: Use the first operation two times to make s = "100011".
Then, use the second operation on the third and sixth elements to make s = "101010".
Input: s = "010"
Output: 0
Explanation: The string is already alternating.
Input: s = "1110"
Output: 1
Explanation: Use the second operation on the second element to make s = "1010".
Constraints:

Contents

The intution of of this approach is that, say we apply Type-1 operation on every single character in input string, for example input string is "111000":

As you notice, when you apply Type-1 operation on all characters, we will get the same input string.

In terms of alternate strings possible, there are only two possible strings with alternate characters, one starts with '0' and the other starts with '1', in the above example with input string of length 6, alternate strings can be either "010101" or "101010".

So, in this approach, we are going to concatenate input string twice, "111000111000" and also take two alternate string with this length, 010101010101 and 101010101010, the reason we are doing this is because, when we have to try applying on Type -1 operation on every single single character, it will end up with the same string again, so, using this concatenated string we can look at a window length of 6. For e.g.:

Similarly, continue for next iterations, now we can check how many Type -2 operations would be needed to convert this to either of alternate strings at the same window length, and take the minimum of these 2.

Implementation steps:
public class MinimumNumberOfFlipsToMakeTheBinaryStringAlternate { static int minFlips(String s) { //111000 - if we apply type-1 operation, i.e. remove character from beginning and add it at end. //to apply for all characters, that means until we get the same string back, 111000 -> 111000 // In this approach, we will take the whole string as if we apply type-1 on every character 111000111000 // then apply sliding window with length as 6 and see how many type-2 operations are required to get one of the possible // alternate string, one starts with 1 and another starts with 0. int n= s.length(); s = s+ s; StringBuilder alternate1= new StringBuilder(); StringBuilder alternate2= new StringBuilder(); // First let's construct the two possible alternate strings for(int i=0; i<s.length(); i++) { if(i%2==0){ alternate1.append("0"); alternate2.append("1"); } else { alternate1.append("1"); alternate2.append("0"); } } // now apply sliding window int left = 0; int right=0; int diff1 =0; int diff2 =0; int ans = s.length();//we are going to find min, so setting it to max possible while(right < s.length()) { if(s.charAt(right) != alternate1.charAt(right)){ diff1++; } if(s.charAt(right) != alternate2.charAt(right)) { diff2++; } if(right - left +1 >n) { if(s.charAt(left) != alternate1.charAt(left)) { diff1--; } if(s.charAt(left) != alternate2.charAt(left)) { diff2--; } left++; } //update ans, only if the window size is n, that is because, we want to know how many // flips needed to alternate the string with original size. if(right - left +1 == n) { ans = Math.min(ans, Math.min(diff1, diff2)); } right++; } return ans; } public static void main(String[] args) { System.out.println(minFlips("01001001101")); System.out.println(minFlips("111000")); System.out.println(minFlips("010")); System.out.println(minFlips("1110")); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time, where n is the length of input string s.
Space complexity: O(1).

Above implementations source code can be found at GitHub link for Java code