You are given a binary string
-
Type-1: Remove the character at the start of the string
s and append it to the end of the string. -
Type-2: Pick any character in
s and flip its value, i.e., if its value is'0' it becomes'1' and vice-versa.
Return the minimum number of type-2 operations you need to perform such that
The string is called alternating if no two adjacent characters are equal.
-
For example, the strings
"010" and"1010" are alternating, while the string"0100" is not.
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".
Output: 0
Explanation: The string is already alternating.
Output: 1
Explanation: Use the second operation on the second element to make s = "1010".
Constraints:
1 <= s.length <= 105 s[i] is either'0' or'1' .
Contents
Solution 1 - Using sliding window technique, check minimum changes needed in one of the alternate strings, one starts with '0' and other starts with '1'
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":
- After removing first character, remove 1 from the beginning and adding it at end, input string will become "110001"
- Apply the same on second character, remove 1 from the beginning and adding it at end, input string will become "100011"
- Apply the same on third character, remove 1 from the beginning and adding it at end, input string will become "000111"
- Apply the same on fourth character, remove 0 from the beginning and adding it at end, input string will become "001110"
- Apply the same on fifth character, remove 0 from the beginning and adding it at end, input string will become "011100"
- Apply the same on sixth character, remove 0 from the beginning and adding it at end, input string will become "111000"
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,
- In first iteration, we can consider input string as is "111000111000".
- In next iteration, we can consider input string as if first character is removed from beginning and added at the and "111000111000".
Implementation steps:
-
First, take the length of input string
s , and store it in a variablen , because of this is going to be our window length. -
Next, modify input string, i.e. concatenate the same string at the end
s = s + s . -
Next, prepare two alternate strings with same length as modified input string
s , call themalternate1 andalternate2 , one starts with '0' and the other starts with '1' (for example, in the input strings length is 6, then alternate strings lengths would be 12) -
Next, create five variables
left ,right that we will use to adjust the window,diff1 to store minimum flips needed if we have to producealternate1 string,diff2 to store minimum flips needed if we have to producealternate2 string, andans to store the answer, initializeans with maximum value as modifieds length since we are anyway going to find the minimum flips required. -
Next, run a
while loop untilright pointer reaches end of strings , and do the following:-
Check the character at
right pointer betweens andalternate1 , if they are not same incrementdiff1 , similarly check the character atright pointer betweens andalternate1 , if they are not same incrementdiff2 , by doing so we are capturing whether a flip is needed to turn input string into one of the alternate string or not. -
Next, check if we are going out of bounds, i.e. if our window length going beyond
n (original input string length)right - left + 1 > n , if it is then move theleft pointer, also check character atleft pointer betweens andalternate1 , if they are not same, decrementdiff1 , similarly check character atleft pointer betweens andalternate2 , if they are not same, decrementdiff2 . The reason we have to decrement is, if the characters were not same, we would have incremented earlier. -
When the window size is exactly
n , i.e.right - left + 1 == n , take the minimum betweendiff1 anddiff2 and update theans . -
At the end of while loop increment
right pointer to move to the next element in strings .
-
Check the character at
-
Finally, return
ans .
Complexity Analysis:
Time complexity: Above code runs in O(n) time, where
Space complexity: O(1).
Above implementations source code can be found at