Minimize Suffix flip to make Binary String non decreasing
Given a binary string str, the task is to find the minimum operations required to convert the binary string in a non-decreasing manner such that in each operation an index i is chosen and all bits right to it are flipped.
Examples:
Input: str = “100010”
Output: 3
Explanation: On flipping at index 0 it becomes 111101.
On flipping at the index at index 3 it becomes 111110.
On flipping at index 4 it becomes 111111, i.e., non-decreasing
So the minimum operations required are 3.
Input: str = “00001111”
Output: 0
Explanation: The string is already in non-decreasing order so 0 operations are required.
Naive Approach: This problem can be solved using the following idea:
At index i, where previous character at index i-1 is different, there will be having two choices:
- Change all the right bits from i to n-1 and increase the count of operations.
- Ignoring this bit and iterate further.
Follow the steps mentioned below to solve the problem:
- Initialize a variable res =0 to count the minimum operation.
- Iterate through the string and check if the character at index i is equal to the previous character or not.
- If not, either flip all the right bits from that index i or ignore that index and iterate further.
- If all the right bits are flipped, increment the res.
- Return the res.
Time Complexity: O(2(N*N))
Auxiliary Space: O(1)
Efficient Approach: This can be solved using the following idea:
At index i, where previous character at index i – 1 is different, just maintain a flag to check whether flip has occurred or not before and increase the count of operations.
Follow the steps mentioned below to solve the problem:
- Initialize a variable res = 0 to count the minimum operations and flag = 0 to check if the flip has occurred before.
- Iterate through the string and check if the character at index i is equal to the previous character or not.
- If not, and flag = 0 and the current character is ‘0’, it means it is a decreasing sequence and it needs to be flipped.
- So make the flag = 1 and increment the res.
- Else if flag = 1 there is a flip before and so the result should be incremented since the present and previous are not equal.
- Return the res.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string str)
{
int res = 0;
bool flag = 0;
for ( int i = 1; i < str.length(); i++) {
if (str[i] != str[i - 1]) {
if (flag == 0 && str[i] == '0' ) {
flag = 1;
res++;
}
else if (flag == 1) {
res++;
}
}
}
return res;
}
int main()
{
string str = "100010" ;
cout << minOperations(str);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int minOperations(String str)
{
int res = 0 ;
int flag = 0 ;
for ( int i = 1 ; i < str.length(); i++) {
if (str.charAt(i) != str.charAt(i - 1 )) {
if (flag == 0 && str.charAt(i) == '0' ) {
flag = 1 ;
res++;
}
else if (flag == 1 ) {
res++;
}
}
}
return res;
}
public static void main(String[] args)
{
String str = "100010" ;
System.out.print(minOperations(str));
}
}
|
Python3
def minOperations( str ):
res = 0
flag = 0
for i in range ( 1 , len ( str )):
if ( str [i] ! = str [i - 1 ]):
if (flag = = 0 and str [i] = = '0' ):
flag = 1
res + = 1
elif (flag = = 1 ):
res + = 1
return res
str = "100010"
print (minOperations( str ))
|
C#
using System;
public class GFG{
public static int minOperations( string str)
{
int res = 0;
int flag = 0;
for ( int i = 1; i < str.Length; i++) {
if (str[i] != str[i - 1]) {
if (flag == 0 && str[i] == '0' ) {
flag = 1;
res++;
}
else if (flag == 1) {
res++;
}
}
}
return res;
}
static public void Main (){
string str = "100010" ;
Console.WriteLine(minOperations(str));
}
}
|
Javascript
const minOperations = (str) => {
let res = 0;
let flag = 0;
for (let i = 1; i < str.length; i++) {
if (str[i] != str[i - 1]) {
if (flag == 0 && str[i] == '0' ) {
flag = 1;
res++;
}
else if (flag == 1) {
res++;
}
}
}
return res;
}
str = "100010" ;
console.log(minOperations(str));
|
Time Complexity: O(N) where N is the length of the string.
Auxiliary Space: O(1)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...