Given a binary string str, the task is to find the minimum number of characters in the string that have to be replaced in order to make the string alternating (i.e. of the form 01010101… or 10101010…).
Input: str = “1100”
Replace 2nd character with ‘0’ and 3rd character with ‘1’
Input: str = “1010”
The string is already alternating.
We have discussed one approach in Number of flips to make binary string alternate. In this post a better approach is discussed.
Approach: For the string str, there can be two possible solutions. Either the resultant string can be
- 010101… or
In order to find the minimum replacements, count the number of replacements to convert the string in type 1 and store it in count then minimum replacement will be min(count, len – count) where len is the length of the string. len – count is the number of replacements to convert the string in type 2.
Below is the implementation of the above approach:
# Python3 implementation of the approach.
# Function to return the minimum number of
# characters of the given binary string
# to be replaced to make the string alternating
def minReplacement(s, length):
ans = 0
for i in range(0, length):
# If there is 1 at even index positions
if i % 2 == 0 and s[i] == ‘1’:
ans += 1
# If there is 0 at odd index positions
if i % 2 == 1 and s[i] == ‘0’:
ans += 1
return min(ans, length – ans)
# Driver code
if __name__ == “__main__”:
s = “1100”
length = len(s)
# This code is contributed by Rituraj Jain
Time Complexity: O(len) where len is the length of the given string.
- Minimum swaps required to make a binary string alternating
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum number of characters to be removed to make a binary string alternate
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Covert string X to an anagram of string Y with minimum replacements
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of deletions to make a string palindrome
- Number of flips to make binary string alternate | Set 1
- Minimum number of Appends needed to make a string palindrome
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum changes required to make first string substring of second string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.