Wednesday, 27 Oct 2021

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

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: 2Approach: 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++#include using namespace std;  int minFlips(string str){        int count = 0;          if (str.size()