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…).
Examples:
Input: str = “1100”
Output: 2
Replace 2nd character with ‘0’ and 3rd character with ‘1’Input: str = “1010”
Output: 0
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
- 101010…
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum number of // characters of the given binary string // to be replaced to make the string alternating int minReplacement(string s, int len) { int ans = 0; for (int i = 0; i < len; i++) { // If there is 1 at even index positions if (i % 2 == 0 && s[i] == '1') ans++; // If there is 0 at odd index positions if (i % 2 == 1 && s[i] == '0') ans++; } return min(ans, len - ans); } // Driver code int main() { string s = "1100"; int len = s.size(); cout << minReplacement(s, len); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the minimum number of // characters of the given binary string // to be replaced to make the string alternating static int minReplacement(String s, int len) { int ans = 0; for (int i = 0; i < len; i++) { // If there is 1 at even index positions if (i % 2 == 0 && s.charAt(i) == '1') ans++; // If there is 0 at odd index positions if (i % 2 == 1 && s.charAt(i) == '0') ans++; } return Math.min(ans, len - ans); } // Driver code public static void main(String args[]) { String s = "1100"; int len = s.length(); System.out.print(minReplacement(s, len)); } } |
Python3
# 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) print(minReplacement(s, length)) # This code is contributed by Rituraj Jain |
C#
// C# implementation of the approach using System; class GFG { // Function to return the minimum number of // characters of the given binary string // to be replaced to make the string alternating static int minReplacement(String s, int len) { int ans = 0; for (int i = 0; i < len; i++) { // If there is 1 at even index positions if (i % 2 == 0 && s[i] == '1') ans++; // If there is 0 at odd index positions if (i % 2 == 1 && s[i] == '0') ans++; } return Math.Min(ans, len - ans); } // Driver code public static void Main(String []args) { String s = "1100"; int len = s.Length; Console.Write(minReplacement(s, len)); } } // This code contributed by Rajput-Ji |
PHP
<?php // PHP 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 function minReplacement($s, $len) { $ans = 0; for ($i = 0; $i < $len; $i++) { // If there is 1 at even index positions if ($i % 2 == 0 && $s[$i] == '1') $ans++; // If there is 0 at odd index positions if ($i % 2 == 1 && $s[$i] == '0') $ans++; } return min($ans, $len - $ans); } // Driver code $s = "1100"; $len = strlen($s); echo minReplacement($s, $len); // This code is contributed by chandan_jnu ?> |
2
Time Complexity: O(len) where len is the length of the given string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum number of flips with rotation to make binary string alternating
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum swaps required to make a binary string alternating
- Minimum substring reversals required to make given Binary String alternating
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements in a string to make adjacent characters unequal
- Minimum subarray reversals required to make given binary array alternating
- Minimum replacements required to make given Matrix palindromic
- Convert string X to an anagram of string Y with minimum replacements
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum replacements required to obtain a K-periodic palindromic string
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum digits to be removed to make either all digits or alternating digits same
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Maximum and minimum sums from two numbers with digit replacements
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

