Skip to content
Related Articles
Min flips of continuous characters to make all characters same in a string
• Difficulty Level : Easy
• Last Updated : 07 Apr, 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++

 `// CPP program to find min flips in binary``// string to make all characters equal``#include ``using` `namespace` `std;` `// To find min number of flips in binary string``int` `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

 `// Java program to find min flips in binary``// string to make all characters equal``public` `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

 `# Python 3 program to find min flips in``# binary string to make all characters equal` `# To find min number of flips in``# binary string``def` `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 Code``if` `__name__ ``=``=` `"__main__"``:``    ` `    ``str` `=` `"00011110001110"``    ``n ``=` `len``(``str``)` `    ``print``(findFlips(``str``, n))` `# This code is contributed by ita_c`

## C#

 `// C# program to find min flips in``// binary string to make all``// characters equal``using` `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

 ``

## Javascript

 ``

Time Complexity: O(n)

Output:

`2`

This article is contributed by nuclode. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up