Minimum flips to remove any consecutive 3 0s or 1s in given Binary string
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:
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++
#include <bits/stdc++.h>
using namespace std;
int minFlips(string str)
{
int count = 0;
if (str.size() <= 2) {
return 0;
}
for ( int i = 0; i < str.size() - 2;) {
if (str[i] == str[i + 1]
&& str[i + 2] == str[i + 1]) {
i = i + 3;
count++;
}
else {
i++;
}
}
return count;
}
int main()
{
string S = "0011101" ;
cout << minFlips(S);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minFlips(String str)
{
int count = 0 ;
if (str.length() <= 2 ) {
return 0 ;
}
for ( int i = 0 ; i < str.length() - 2 😉 {
if (str.charAt(i) == str.charAt(i+ 1 )
&& str.charAt(i+ 2 ) == str.charAt(i+ 1 )) {
i = i + 3 ;
count++;
}
else {
i++;
}
}
return count;
}
public static void main(String[] args)
{
String S = "0011101" ;
System.out.println(minFlips(S));
}
}
|
Python3
def minFlips(st):
count = 0
if ( len (st) < = 2 ):
return 0
for i in range ( len (st) - 2 ):
if (st[i] = = st[i + 1 ]
and st[i + 2 ] = = st[i + 1 ]):
i = i + 3
count + = 1
else :
i + = 1
return count
if __name__ = = "__main__" :
S = "0011101"
print (minFlips(S))
|
Javascript
<script>
function minFlips(str) {
let count = 0;
if (str.length <= 2) {
return 0;
}
for (let i = 0; i < str.length - 2;) {
if (str[i] == str[i + 1]
&& str[i + 2] == str[i + 1]) {
i = i + 3;
count++;
}
else {
i++;
}
}
return count;
}
let S = "0011101" ;
document.write(minFlips(S));
</script>
|
C#
using System;
public class GFG
{
static int minFlips( string str)
{
int count = 0;
if (str.Length <= 2) {
return 0;
}
for ( int i = 0; i < str.Length - 2;) {
if (str[i] == str[i+1]
&& str[i+2] == str[i+1]) {
i = i + 3;
count++;
}
else {
i++;
}
}
return count;
}
public static void Main( string [] args)
{
string S = "0011101" ;
Console.WriteLine(minFlips(S));
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...