Minimize Suffix flip to make Binary String non decreasing


In this problem, we will count the number of minimum operations required to convert string non-decreasing order by flipping the characters of the binary string.

We can flip all characters of the substring starting from the \mathrm{p^{th}} index if the character at the pth index is 0 and not matching with character at the previous index, and we can count the minimum flips.

Problem statement - We have given a binary string alpha. We need to count the minimum flips required to convert the binary string in increasing order. In one flip, we can select any index p and flip the substring starting from the pth index.

Sample examples

Input

alpha = "11000110"

Output

3

Explanation

  • We can flip the substring starting from the 2nd index in the first move. So, the resultant string will be 11111001.

  • In the second move, select the string starting from the 5th index, and flip it. So, the string becomes 11111110.

  • Flip the last character to make the string equal to 1111111.

So, we need to perform total 3 flips to convert the string to increasing order.

Input

alpha = "0011"

Output

0

Explanation - The string is already in increasing order.

Input

alpha = "1100";

Output

1

Explanation - We can flip the substring starting from the 2nd index to get the 1111 string.

Approach 1

In this approach, We will traverse the string, and when we find the current character is '0' and mismatching with the previous character, we will flip all characters which are at right from the current index.

Algorithm

Step 1 - Initialize the 'cnt' with 0 to store the minimum required flips.

Step 2 - If the character at index p and p - 1 is not the same, and the character at index 'p' is '0', follow the below steps.

Step 3 - Start traversing the string from the pth index.

Step 4 - If the character qth index is '0', replace it with '1'. Otherwise, replace it with '0'.

Step 5 - Increment the cnt value by 1.

Step 6 - At last, return the 'cnt' value.

Example

#include <bits/stdc++.h>
using namespace std;

int GetOperations(string alpha) {
   // For storing the count of operations
   int cnt = 0;
   for (int p = 1; p < alpha.length(); p++) {
     // For different adjacent characters
     if (alpha[p] != alpha[p - 1]) {
       if (alpha[p] == '0') {
         //   Flipping all bits of a substring starting from index p
         for (int q = p; q < alpha.length(); q++) {
            if (alpha[q] == '0') {
              alpha[q] = '1';
            } else {
              alpha[q] = '0';
            }
         }
         cnt++;
       }
     }
   }
   // return answer
   return cnt;
}
int main() {
   string alpha = "11000110";
   cout << "The minimum operations required to convert the string into the increasing order is " << GetOperations(alpha);
   return 0;
}

Output

The minimum operations required to convert the string into the increasing order is 3

Time complexity - O(N*N) to traverse and flip the string characters.

Space complexity - O(1), as we don't use any extra space.

Approach 2

In this approach, we will use the boolean variable to track whether string characters are flipped rather than flipping them.

Algorithm

Step 1 - Initialize the 'cnt' and 'isFlipped' with '0'.

Step 2 - Start traversing the string. If the character at p and p - 1 index doesn't match, follow the below steps.

Step 3 - If isFlipped is equal to 0, and alpha[p] is also '0', change isFlipped to 1, and increment 'cnt' by 1.

Step 4 - If isFlipped is 1, increment 'cnt' by 1.

Step 5 - Return 'cnt' from the function.

Example

#include <bits/stdc++.h>
using namespace std;

int GetOperations(string alpha) {
   // For storing the count of operations
   int cnt = 0;
   // To keep track of the flip
   bool isFlipped = 0;
   for (int p = 1; p < alpha.length(); p++) {
     // For different adjacent characters
     if (alpha[p] != alpha[p - 1]) {
       // alpha[p] is '0' and its not equal to alpha[p-1]. So, the string is in decreasing order.
       if (isFlipped == 0 && alpha[p] == '0') {
         isFlipped = 1;
         cnt++;
       }
       // When a string is in increasing order, but the string is flipped
       else if (isFlipped == 1) {
         cnt++;
       }
     }
   }
   // return answer
   return cnt;
}
int main() {
   string alpha = "11000";
   cout << "The minimum operations required to convert the string into the increasing order is " << GetOperations(alpha);
   return 0;
}

Output

The minimum operations required to convert the string into the increasing order is 1

Time complexity - O(N) to traverse the string.

Space complexity - O(1)

We track the virtually flipped characters using the boolean variable in the second approach. So, it cuts the costs of flipping all the right characters whenever we find the string is not in increasing order.

Updated on: 29-Aug-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements