Minimum flips to remove any consecutive 3 0s or 1s in given Binary string - GeeksforGeeks

06 Sep, 2021
Given a binary string S consisting of N characters, the task is to find the minimum number of flips required such that there don’t exist three consecutive same characters.
Examples:
Input: S = “1100011”Output: 1Explanation:Flip the character at index 3 modifies the string S “1101011” that have no three consecutive same characters. Therefore, the minimum number of flips required is 1.
Input: S = “0001111101”Output: 2
Approach: The given problem can be solved by considering every three consecutive characters and if they are the same, then increase the count of flips required as one of the three characters is needed to be flipped. Follow the steps below to solve the problem:
Initialize the variable, say count as 0 that stores the minimum number of flips required.
If the size of the string is less than equal to 2, then return 0 as there is no need for any flips.
Iterate over the range [0, N – 2) using the variable i and perform the following steps:If the character at indices i, (i + 1), and (i + 2) characters are the same, then increment the value of count by 1 and the value of i by 3.Otherwise, increment the value of i by 1.
If the character at indices i, (i + 1), and (i + 2) characters are the same, then increment the value of count by 1 and the value of i by 3.
Otherwise, increment the value of i by 1.
After performing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
Java
Python3
Javascript
C#
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the minimum number// of flips to make all three pairs of// consecutive characters differentint minFlips(string str){    // Stores resultant count of pairs    int count = 0;     // Base Case    if (str.size() <= 2) {        return 0;    }     // Iterate over the range [0, N - 2]    for (int i = 0; i < str.size() - 2;) {         // If the consecutive 3 numbers        // are the same then increment        // the count and the counter        if (str[i] == str[i + 1]            && str[i + 2] == str[i + 1]) {            i = i + 3;            count++;        }        else {            i++;        }    }     // Return the answer    return count;} // Driver Codeint main(){    string S = "0011101";    cout << minFlips(S);     return 0;}
// Java program for the above approachimport java.io.*;class GFG{ // Function to find the minimum number// of flips to make all three pairs of// consecutive characters differentstatic int minFlips(String str){    // Stores resultant count of pairs    int count = 0;       // Base Case    if (str.length() <= 2) {        return 0;    }       // Iterate over the range [0, N - 2]    for (int i = 0; i < str.length() - 2 {           // If the consecutive 3 numbers        // are the same then increment        // the count and the counter        if (str.charAt(i) == str.charAt(i+1)            && str.charAt(i+2) == str.charAt(i+1)) {            i = i + 3;            count++;        }        else {            i++;        }    }       // Return the answer    return count;}   // Driver Codepublic static void main(String[] args){     String S = "0011101";   System.out.println(minFlips(S));}} // This code is contributed by dwivediyash
# python 3 program for the above approach## Function to find the minimum number# of flips to make all three pairs of# consecutive characters differentdef minFlips(st):     # Stores resultant count of pairs    count = 0     # Base Case    if (len(st) <= 2):        return 0     # Iterate over the range [0, N - 2]    for i in range(len(st) - 2):         # If the consecutive 3 numbers        # are the same then increment        # the count and the counter        if (st[i] == st[i + 1]                and st[i + 2] == st[i + 1]):            i = i + 3            count += 1         else:            i += 1     # Return the answer    return count # Driver Codeif __name__ == "__main__":     S = "0011101"    print(minFlips(S))     # This code is contributed by ukasp.
  <script>        // JavaScript Program to implement        // the above approach          // Function to find the minimum number        // of flips to make all three pairs of        // consecutive characters different        function minFlips(str) {            // Stores resultant count of pairs            let count = 0;             // Base Case            if (str.length <= 2) {                return 0;            }             // Iterate over the range [0, N - 2]            for (let i = 0; i < str.length - 2;) {                 // If the consecutive 3 numbers                // are the same then increment                // the count and the counter                if (str[i] == str[i + 1]                    && str[i + 2] == str[i + 1]) {                    i = i + 3;                    count++;                }                else {                    i++;                }            }             // Return the answer            return count;        }         // Driver Code         let S = "0011101";        document.write(minFlips(S)); // This code is contributed by Potta Lokesh    </script>
// C# program for the above approachusing System;  public class GFG{ // Function to find the minimum number// of flips to make all three pairs of// consecutive characters differentstatic int minFlips(string str){    // Stores resultant count of pairs    int count = 0;       // Base Case    if (str.Length <= 2) {        return 0;    }       // Iterate over the range [0, N - 2]    for (int i = 0; i < str.Length - 2;) {           // If the consecutive 3 numbers        // are the same then increment        // the count and the counter        if (str[i] == str[i+1]            && str[i+2] == str[i+1]) {            i = i + 3;            count++;        }        else {            i++;        }    }       // Return the answer    return count;}   // Driver Codepublic static void Main(string[] args){     string S = "0011101";     Console.WriteLine(minFlips(S));}} // This code is contributed by AnkThon
1
 
Time Complexity: O(N)Auxiliary Space: O(1)
lokeshpotta20
dwivediyash
ankthon
ukasp
binary-string
Bit Magic
Strings
Strings
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Reverse actual bits of the given number
Program to find parity
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types