Min flips of continuous characters to make all characters same in a string

05 Nov, 2021
Given a string consisting only of 1’s and 0’s. In one flip we can change any continuous sequence of this string. Find this minimum number of flips so the string consist of same characters only.Examples:  
Input : 00011110001110
Output : 2
We need to convert 1's sequence
so string consist of all 0's.

Input : 010101100011
Output : 4
 
We need to find the min flips in string so all characters are equal. All we have to find numbers of sequence which consisting of 0’s or 1’s only. Then number of flips required will be half of this number as we can change all 0’s or all 1’s. 
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find min flips in binary// string to make all characters equal#include <bits/stdc++.h>using namespace std; // To find min number of flips in binary stringint findFlips(char str[], int n){    char last = ' '; int res = 0;     for (int i = 0; i < n; i++) {         // If last character is not equal        // to str[i] increase res        if (last != str[i])            res++;        last = str[i];    }     // To return min flips    return res / 2;} // Driver program to check findFlips()int main(){    char str[] = "00011110001110";    int n = strlen(str);     cout << findFlips(str, n);     return 0;}
// Java program to find min flips in binary// string to make all characters equalpublic class minFlips {     // To find min number of flips in binary string    static int findFlips(String str, int n)    {        char last = ' '; int res = 0;         for (int i = 0; i < n; i++) {             // If last character is not equal            // to str[i] increase res            if (last != str.charAt(i))                res++;            last = str.charAt(i);        }         // To return min flips        return res / 2;    }     // Driver program to check findFlips()    public static void main(String[] args)    {        String str = "00011110001110";        int n = str.length();         System.out.println(findFlips(str, n));    }}
# Python 3 program to find min flips in# binary string to make all characters equal # To find min number of flips in# binary stringdef findFlips(str, n):     last = ' '    res = 0     for i in range( n) :         # If last character is not equal        # to str[i] increase res        if (last != str[i]):            res += 1        last = str[i]     # To return min flips    return res // 2 # Driver Codeif __name__ == "__main__":         str = "00011110001110"    n = len(str)     print(findFlips(str, n)) # This code is contributed by ita_c
// C# program to find min flips in// binary string to make all// characters equalusing System; public class GFG {     // To find min number of flips    // in binary string    static int findFlips(String str, int n)    {        char last = ' '; int res = 0;         for (int i = 0; i < n; i++) {             // If last character is not            // equal to str[i] increase            // res            if (last != str[i])                res++;            last = str[i];        }         // To return min flips        return res / 2;    }     // Driver program to check findFlips()    public static void Main()    {        String str = "00011110001110";        int n = str.Length;         Console.Write(findFlips(str, n));    }} // This code is contributed by nitin mittal
<?php// PHP program to find min flips in binary// string to make all characters equal // To find min number of// flips in binary stringfunction findFlips($str, $n){    $last = ' ';    $res = 0;     for ($i = 0; $i < $n; $i++)    {         // If last character is not equal        // to str[i] increase res        if ($last != $str[$i])            $res++;        $last = $str[$i];    }     // To return min flips    return intval($res / 2);}     // Driver Code    $str = "00011110001110";    $n = strlen($str);     echo findFlips($str, $n);     // This code is contributed by Sam007?>
<script> // JavaScript program to find min flips in binary// string to make all characters equal      // To find min number of flips in binary string    function findFlips( str , n) {        var last = ' ';        var res = 0;         for (i = 0; i < n; i++) {             // If last character is not equal            // to str[i] increase res            if (last != str.charAt(i))                res++;            last = str.charAt(i);        }         // To return min flips        return parseInt(res / 2);    }     // Driver program to check findFlips()             var str = "00011110001110";        var n = str.length;         document.write(findFlips(str, n));  // This code contributed by aashish1995 </script>
Time Complexity: O(n) Auxiliary Space: O(1)
Output: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
default, selected
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
2
 
Minimum flips of continuous characters to make all same in a binary string | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersMinimum flips of continuous characters to make all same in a binary string | 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 / 1:45•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=ZYqT5pDnQt8" 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 nuclode. 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
Sam007
ukasp
aashish1995
rohitsingh07052
binary-string
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
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
Different Methods to Reverse a String in C++
Python program to check if a string is palindrome or not
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Length of the longest substring without repeating characters
Convert string to char array in C++