Number of flips to make binary string alternate | Set 1 - GeeksforGeeks

11 Aug, 2021
Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Examples :  
Input : str = “001”
Output : 1
Minimum number of flips required = 1
We can flip 1st bit from 0 to 1 

Input : str = “0001010111”
Output : 2
Minimum number of flips required = 2
We can flip 2nd bit from 0 to 1 and 9th 
bit from 1 to 0 to make alternate 
string “0101010101”.
Expected time complexity : O(n) where n is length of input string. 
We can solve this problem by considering all possible results, As we are supposed to get alternate string, there are only 2 possibilities, alternate string starting with 0 and alternate string starting with 1. We will try both cases and choose the string which will require minimum number of flips as our final answer. Trying a case requires O(n) time in which we will loop over all characters of given string, if current character is expected character according to alternation then we will do nothing otherwise we will increase flip count by 1. After trying strings starting with 0 and starting with 1, we will choose the string with minimum flip count. Total time complexity of solution will be O(n)  
C++
Java
Python 3
C#
PHP
Javascript
// C/C++ program to find minimum number of// flip to make binary string alternate#include <bits/stdc++.h>using namespace std; //  Utility method to flip a characterchar flip(char ch){    return (ch == '0') ? '1' : '0';} //  Utility method to get minimum flips when//  alternate string starts with expected charint getFlipWithStartingCharcter(string str,                                char expected){    int flipCount = 0;    for (int i = 0; i < str.length(); i++)    {        //  if current character is not expected,        // increase flip count        if (str[i] != expected)            flipCount++;         //  flip expected character each time        expected = flip(expected);    }    return flipCount;} // method return minimum flip to make binary// string alternateint minFlipToMakeStringAlternate(string str){    //  return minimum of following two    //  1) flips when alternate string starts with 0    //  2) flips when alternate string starts with 1    return min(getFlipWithStartingCharcter(str, '0'),               getFlipWithStartingCharcter(str, '1'));} //  Driver code to test above methodint main(){    string str = "0001010111";    cout << minFlipToMakeStringAlternate(str);    return 0;}
// Java program to find minimum number of// flip to make binary string alternateclass GFG{    //  Utility method to flip a character    public static char flip(char ch)    {        return (ch == '0') ? '1' : '0';    }          //  Utility method to get minimum flips when    //  alternate string starts with expected char    public static int getFlipWithStartingCharcter(String str,                                    char expected)    {        int flipCount = 0;        for (int i = 0; i < str.length(); i++)        {            //  if current character is not expected,            // increase flip count            if (str.charAt(i) != expected)                flipCount++;                  //  flip expected character each time            expected = flip(expected);        }        return flipCount;    }          // method return minimum flip to make binary    // string alternate    public static int minFlipToMakeStringAlternate(String str)    {        //  return minimum of following two        //  1) flips when alternate string starts with 0        //  2) flips when alternate string starts with 1        return Math.min(getFlipWithStartingCharcter(str, '0'),                   getFlipWithStartingCharcter(str, '1'));    }          //  Driver code to test above method    public static void main(String args[])    {        String str = "0001010111";        System.out.println(minFlipToMakeStringAlternate(str));    }} // This code is contributed by Sumit Ghosh
# Python 3 program to find minimum number of# flip to make binary string alternate # Utility method to flip a characterdef flip( ch):    return '1' if (ch == '0') else '0' # Utility method to get minimum flips when# alternate string starts with expected chardef getFlipWithStartingCharcter(str, expected):     flipCount = 0    for i in range(len( str)):                 # if current character is not expected,        # increase flip count        if (str[i] != expected):            flipCount += 1         # flip expected character each time        expected = flip(expected)    return flipCount # method return minimum flip to make binary# string alternatedef minFlipToMakeStringAlternate(str):     # return minimum of following two    # 1) flips when alternate string starts with 0    # 2) flips when alternate string starts with 1    return min(getFlipWithStartingCharcter(str, '0'),            getFlipWithStartingCharcter(str, '1')) # Driver code to test above methodif __name__ == "__main__":         str = "0001010111"    print(minFlipToMakeStringAlternate(str))
// C# program to find minimum number of// flip to make binary string alternateusing System; class GFG{    // Utility method to    // flip a character    public static char flip(char ch)    {        return (ch == '0') ? '1' : '0';    }         // Utility method to get minimum flips    // when alternate string starts with    // expected char    public static int getFlipWithStartingCharcter(String str,                                                char expected)    {        int flipCount = 0;        for (int i = 0; i < str.Length; i++)        {            // if current character is not            // expected, increase flip count            if (str[i] != expected)                flipCount++;                 // flip expected character each time            expected = flip(expected);        }        return flipCount;    }         // method return minimum flip to    // make binary string alternate    public static int minFlipToMakeStringAlternate(string str)    {        // return minimum of following two        // 1) flips when alternate string starts with 0        // 2) flips when alternate string starts with 1        return Math.Min(getFlipWithStartingCharcter(str, '0'),                getFlipWithStartingCharcter(str, '1'));    }         // Driver Code    public static void Main()    {        string str = "0001010111";        Console.Write(minFlipToMakeStringAlternate(str));    }} // This code is contributed by nitin mittal.
<?php// PHP program to find minimum number of// flip to make binary string alternate // Utility method to flip a characterfunction flip( $ch){    return ($ch == '0') ? '1' : '0';} // Utility method to get minimum flips when// alternate string starts with expected charfunction getFlipWithStartingCharcter($str,                                $expected){         $flipCount = 0;    for ($i = 0; $i < strlen($str); $i++)    {                 // if current character is not expected,        // increase flip count        if ($str[$i] != $expected)            $flipCount++;         // flip expected character each time        $expected = flip($expected);    }    return $flipCount;} // method return minimum flip to make binary// string alternatefunction minFlipToMakeStringAlternate( $str){         // return minimum of following two    // 1) flips when alternate string starts with 0    // 2) flips when alternate string starts with 1    return min(getFlipWithStartingCharcter($str, '0'),               getFlipWithStartingCharcter($str, '1'));} // Driver code to test above method$str = "0001010111";echo minFlipToMakeStringAlternate($str); // This code is contributed by anuj_67.?>
<script> // Javascript program to find minimum number of// flip to make binary string alternate         //  Utility method to flip a character    function flip(ch)    {        return (ch == '0') ? '1' : '0';    }         //  Utility method to get minimum flips when    //  alternate string starts with expected char    function getFlipWithStartingCharcter(str,expected)    {        let flipCount = 0;        for (let i = 0; i < str.length; i++)        {            //  if current character is not expected,            // increase flip count            if (str.charAt(i) != expected)                flipCount++;                    //  flip expected character each time            expected = flip(expected);        }        return flipCount;    }         // method return minimum flip to make binary    // string alternate    function minFlipToMakeStringAlternate(str)    {         //  return minimum of following two        //  1) flips when alternate string starts with 0        //  2) flips when alternate string starts with 1        return Math.min(getFlipWithStartingCharcter(str, '0'),                   getFlipWithStartingCharcter(str, '1'));    }         //  Driver code to test above method    let str = "0001010111";    document.write(minFlipToMakeStringAlternate(str));         // This code is contributed by avanitrachhadiya2155     </script>
Output : 
2
Time Complexity: O(N)Auxiliary Space: O(1) Minimum number of replacements to make the binary string alternating | Set 2  
YouTubeGeeksforGeeks507K subscribersNumber of flips to make binary string alternate | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:00•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=3jxxC7959jY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
vt_m
ukasp
BhawanaGupta3
avanitrachhadiya2155
simmytarika5
pankajsharmagfg
binary-string
Algorithms
Strings
Strings
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
How to write a Pseudo Code?
Understanding Time Complexity with Simple Examples
How to Start Learning DSA?
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4