Minimum removals to make a string concatenation of a substring of 0s followed by a substring of 1s


The problem "Minimum removals to make a string concatenation of a substring of 0s" deals with the job of manipulating strings. A string of 0s and 1s is provided as input, and the result is an integer that reflects the minimal number of 0s that must be eliminated in order to produce a substring of consecutive 0s.

In other words, the problem might be rephrased as follows: Given a string of 0s and 1s, how many 0s must be eliminated in order for the remainder of the string to include a substring of consecutive 0s.

Algorithm

Step 1: Variable Initialization

  • Define a count variable to keep a record of the length of the current sequence of zeros.

  • Define a max_count variable to keep track of the longest sequence of zeros encountered thus far.

  • Set both variables to 0.

Step 2: String Traversal

Use a loop to traverse each character in the string.

Step 3: Zero Detection

If the current character is a zero, increase the count variable.

Step 4: One Detection

  • If the current character is a one, compare the count variable to the max_count variable.

  • If the count variable is higher than the max_count variable, set the max_count variable equal to the count variable.

  • Reset the count variable to 0.

Step 5: Loop Completion

Repeat the process until all characters in the string have been processed.

Step 6: Minimum Deletion Calculation

The minimum number of deletions required to remove all the zeros so that the remaining ones are not separated by any zero can be calculated by subtracting max_count from the length of the string.

Step 7: Result Output

Print the result to the console.

Approaches to follow

  • Dynamic Approach

  • Iterative Approach

Approach 1: Dynamic Approach

Dynamic programming can be used to efficiently solve this problem. In order to create a substring of consecutive 0s, we can create an array dp[] where dp[i] is the smallest number of 0s that are to be eliminated from the substring s[0...i]. The smallest number of 0s to eliminate from an empty substring is 0, therefore we may initialize dp[0] to 0.

Then, we can iterate over the string s and update dp[i] as −

  • If s[i] is '0', then dp[i] = dp[i-1] since we can include s[i] in the substring of consecutive 0s or remove it.

  • We must obtain the closest index j to i which includes a substring of consecutive 0s when s[i] is '1'. This can be done by iterating from i-1 to 0 and seeing if the substring s[j...i] includes consecutive 0s. If the index j is found, then dp[i] = dp[j-1] + (i-j+1), where dp[j-1] denotes the minimum number of 0s that must be eliminated from the substring s[0...j-1] and (i-j+1) is the total number of 1s that must be eliminated in order to achieve the substring of consecutive 0s s[j...i]. If no such index j is discovered then, dp[i] = dp[i-1] because we cannot include s[i] in the substring of consecutive 0s.

    Finally, the minimum number of 0s that need to be removed from the entire string s in order to obtain a substring of consecutive 0s is given by dp[n-1] where n is the length of the string s.

Example-1

The below program uses the method we have discussed above, to first read the input string from standard input and then identify all substrings of 0s. The length of the longest substring of 0s and the length of the string that would be generated by concatenating each substring of 0s are then calculated. In order to determine the least number of eliminations necessary, it finally subtracts the length of the longest substring of 0s from the sum of all substrings of 0s and displays the result to standard output.

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

int main() {
   string s = "100100011000110"; // constant input string

   vector<pair<int, int>> substrings; // vector to store start and end indices of each substring of 0s

   int start = -1;
   for (int i = 0; i < s.length(); i++) {
      if (s[i] == '0') {
         if (start == -1) {
         start = i;
         }
      } else {
         if (start != -1) {
         substrings.push_back(make_pair(start, i - 1));
         start = -1;
         }
      }
   }
   if (start != -1) {
      substrings.push_back(make_pair(start, s.length() - 1));
   }

   int totalLength = 0;
   for (auto& p : substrings) {
      totalLength += p.second - p.first + 1;
   }

   int maxLength = 0;
   for (auto& p : substrings) {
      int len = p.second - p.first + 1;
      if (len > maxLength) {
         maxLength = len;
      }
   }

   int removals = totalLength - maxLength;
   cout << "Input string: " << s << endl;
   cout << "Minimum removals: " << removals << endl;

   return 0;
}

Output

Input string: 100100011000110
Minimum removals: 6

Approach 2: Iterative Approach

This approach uses a straightforward iterative method to run over the string that is given character by character while updating the values of two variables, count and max_count. The method updates the count and max_count variables in accordance with whether the current character is a 0 or a 1. It then provides the difference between max_count and the length of the longest substring of 0s.

Example 2

The code is a C++ software that calculates the minimum number of eliminations required to remove all the zeros from a binary string so that the remaining ones are not separated by any zero. The min_deletions function takes the binary string as input and employs a loop to traverse each character in the string. The loop increases a count variable each time it encounters a zero and resets it to zero when it encounters a one. The maximum value of the count variable is saved in max_count, and at the conclusion, the length of the string is subtracted from max_count to obtain the minimum number of deletions necessary. The result is then displayed to the user.

#include <iostream> 
#include <string> 
using namespace std; 
 
int min_deletions(string str) { 
   int count = 0, max_count = 0; 
   for (char c : str) { 
      if (c == '0') { 
         count++; 
      } else { 
         max_count = max(max_count, count); 
         count = 0; 
      } 
   } 
    return str.length() - max_count; 
} 
 
int main() { 
   string str = "100010011000110"; 
   int deletions = min_deletions(str); 
   cout << "Minimum deletions needed: " << deletions << endl; 
   return 0; 
} 

Output

Minimum deletion needed: 12 

Conclusion

Determining all substrings of 0s, calculating the length of the string that would result from concatenating each substring of 0s and determining the length of the longest substring of 0s are the three steps that can be implemented to solve the given problem. The length of the largest substring of 0s may then be subtracted from the sum of all substrings of 0s to get the lowest number of deletions necessary.

The method we used to get the answer is straightforward and effective, and it operates in linear time, making it suitable for big inputs. But it may be further enhanced by applying a more sophisticated method, such as dynamic programming.

Updated on: 21-Jul-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements