Minimum characters required to be removed to sort binary string in ascending order


In computer science, string manipulation is an essential topic that involves operations such as concatenation, substring, reversing, and more. One common problem related to string manipulation is to remove all 0s from a binary string. In this article, we will discuss an algorithm to solve this problem using a minimum number of non-adjacent pair flips.

Problem Statement

Given a binary string, we have to remove all 0s from the string using the minimum number of non-adjacent pair flips. A flip is defined as selecting two adjacent characters and swapping them. In other words, we need to find the minimum number of flips required to make all 0s in the string to the end of the string.

Approach

We can solve this problem using a greedy approach. We can start from the left of the string and keep track of the last index where we have flipped a 0 to the end. For each 0 we encounter, we swap it with the last flipped 0 to move it to the end of the string. If we encounter a 1, we simply move to the next index.

Let's see the algorithm in detail −

  • Initialize two variables, "lastFlipped" and "flipCount" to -1 and 0, respectively.

  • Traverse the binary string from left to right.

  • If the current character is '0', swap it with the character at the index "lastFlipped + 1" and increment the "lastFlipped" variable.

  • Increment the "flipCount" variable for every swap operation.

  • Once the traversal is completed, all the 0s will be at the end of the string, and the "flipCount" will contain the minimum number of flips required to remove all 0s.

Example

Here're the codes to implement the above algorithm −

#include <stdio.h>
#include <string.h>

int minNonAdjacentPairFlips(char* s) {
   int lastFlipped = -1;
   int flipCount = 0;

   for (int i = 0; i < strlen(s); i++) {
      if (s[i] == '0') {
         char temp = s[i];
         s[i] = s[lastFlipped + 1];
         s[lastFlipped + 1] = temp;
         lastFlipped++;
         flipCount++;
      }
   }
   return flipCount;
}
int main() {
   char s[] = "100101000";
   printf("Binary String: %s\n", s);
   printf("Minimum Non-adjacent Pair Flips: %d\n", minNonAdjacentPairFlips(s));
   return 0;
}

Output

Binary String: 100101000
Minimum Non-adjacent Pair Flips: 6
#include <iostream>
#include <string>

using namespace std;

int minNonAdjacentPairFlips(string s) {
   int lastFlipped = -1;
   int flipCount = 0;
   
   for (int i = 0; i < s.length(); i++) {
      if (s[i] == '0') {
         swap(s[i], s[lastFlipped + 1]);
         lastFlipped++;
         flipCount++;
      }
   }
   
   return flipCount;
}
int main() {
   string s = "100101000";
   cout << "Binary String: " << s << endl;
   cout << "Minimum Non-adjacent Pair Flips: " << minNonAdjacentPairFlips(s) << endl;
   return 0;
}

Output

Binary String: 100101000
Minimum Non-adjacent Pair Flips: 6
public class Main {
   public static int minNonAdjacentPairFlips(String s) {
      
      int lastFlipped = -1;
      int flipCount = 0;

      char[] chars = s.toCharArray();

      for (int i = 0; i < chars.length; i++) {
         if (chars[i] == '0') {
            char temp = chars[i];
            chars[i] = chars[lastFlipped + 1];
            chars[lastFlipped + 1] = temp;
            lastFlipped++;
            flipCount++;
         }
      }
      return flipCount;
   }
   public static void main(String[] args) {
      String s = "100101000";
      System.out.println("Binary String: " + s);
      System.out.println("Minimum Non-adjacent Pair Flips: " + minNonAdjacentPairFlips(s));
   }
}

Output

Binary String: 100101000
Minimum Non-adjacent Pair Flips: 6
def min_non_adjacent_pair_flips(s):
   last_flipped = -1
   flip_count = 0

   for i in range(len(s)):
      if s[i] == '0':
         s_list = list(s)
         s_list[i], s_list[last_flipped + 1] = s_list[last_flipped + 1], s_list[i]
         s = ''.join(s_list)
         last_flipped += 1
         flip_count += 1

   return flip_count

s = "100101000"
print("Binary String:", s)
print("Minimum Non-adjacent Pair Flips:", min_non_adjacent_pair_flips(s))

Output

Binary String: 100101000
Minimum Non-adjacent Pair Flips: 6

Explanation of Test Case

Let's take a binary string "100101000" as an example. We have to remove all 0s from this string using the minimum number of non-adjacent pair flips.

  • Initially, "lastFlipped" and "flipCount" are set to -1 and 0, respectively.

  • We start traversing the string from left to right.

  • At index 1, we encounter a '0'. We swap it with the character at index "lastFlipped + 1" (which is index 0) and increment "lastFlipped" to 0. The string becomes "010101000". The "flipCount" is incremented to 1.

  • At index 4, we encounter another '0'. We swap it with the character at index "lastFlipped + 1" (which is index 1) and increment "lastFlipped" to 1. The string becomes "011010000". The "flipCount" is incremented to 2.

  • At index 5, we encounter a '1'. We simply move to the next index

Conclusion

In this article, we have discussed an algorithm to remove all 0s from a binary string using the minimum number of non-adjacent pair flips. The approach used in this algorithm is greedy, which makes it efficient and easy to implement.

This problem can also be solved using dynamic programming, but the greedy approach provides a simpler and faster solution. The time complexity of this algorithm is O(n), where n is the length of the binary string.

In conclusion, the minimum non-adjacent pair flips algorithm is a useful tool in string manipulation and can be applied in various contexts.

Updated on: 27-Oct-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements