Minimum number of replacements to make the binary string alternating | Set 2

22 Apr, 2022
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... or101010...
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++
Java
Python3
C#
PHP
Javascript
// 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 alternatingint 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 codeint main(){    string s = "1100";    int len = s.size();    cout << minReplacement(s, len);     return 0;}
// Java implementation of the approachclass 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 implementation of the approach. # Function to return the minimum number of# characters of the given binary string# to be replaced to make the string alternatingdef 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 codeif __name__ == "__main__":     s = "1100"    length = len(s)    print(minReplacement(s, length))         # This code is contributed by Rituraj Jain
// C# implementation of the approachusing 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 implementation of the approach // Function to return the minimum number of// characters of the given binary string// to be replaced to make the string alternatingfunction 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?>
<script>  // Javascript implementation of the approach // Function to return the minimum number of// characters of the given binary string// to be replaced to make the string alternatingfunction minReplacement(s, len){    var ans = 0;    for (var 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 codevar s = "1100";var len = s.length;document.write(minReplacement(s, len));  </script>
2
 
Time Complexity: O(len), where len is the length of the given string. We are traversing till len.
Auxiliary Space: O(1), as we are not using any extra space.
Rajput-Ji
Chandan_Kumar
rituraj_jain
rutvik_56
rohitsingh07052
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Find minimum number of coins that make a given value
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Write a program to reverse an array or string
Reverse a string in Java
Different Methods to Reverse a String in C++
Python program to check if a string is palindrome or not
Longest Common Subsequence | DP-4