Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Difficulty Level : Medium
  • Last Updated : 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:

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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: S = “1100011”
Output: 1
Explanation:
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.
  • After performing the above steps, print the value of count as the result.

Below is the implementation of the above approach:

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 different
int 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 Code
int main()
{
    string S = "0011101";
    cout << minFlips(S);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
class GFG
{
 
// Function to find the minimum number
// of flips to make all three pairs of
// consecutive characters different
static 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 Code
public static void main(String[] args)
{
     String S = "0011101";
   System.out.println(minFlips(S));
}
}
 
// This code is contributed by dwivediyash

Python3




# python 3 program for the above approach
#
# Function to find the minimum number
# of flips to make all three pairs of
# consecutive characters different
def 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 Code
if __name__ == "__main__":
 
    S = "0011101"
    print(minFlips(S))
 
    # This code is contributed by ukasp.

Javascript




  <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#




// C# program for the above approach
using System;
 
 
public class GFG
{
 
// Function to find the minimum number
// of flips to make all three pairs of
// consecutive characters different
static 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 Code
public static void Main(string[] args)
{
     string S = "0011101";
     Console.WriteLine(minFlips(S));
}
}
 
// This code is contributed by AnkThon
Output: 
1

 

Time Complexity: O(N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :