Minimum Number of Replacements done of the Substring β€œ01” with β€œ110” to Remove it Completely


The minimum number of replacements done of the substring β€œ01” with β€œ110” to remove it completely is a common problem in string manipulation and optimization. In this tutorial, we delve into this problem and present an efficient solution using C++.

The problem entails finding the minimum number of replacements required to transform a binary string by replacing all occurrences of the substring "01" with "110" while ensuring that the resulting string does not contain the substring "10".

We provide a detailed explanation of the problem statement, present an algorithmic approach to solve it, and offer a step-by-step implementation in C++. By the end of this tutorial, readers will have a clear understanding of the problem and will be equipped with a practical solution to tackle similar scenarios involving string transformations.

Problem Statement

We are given a binary string S. The objective is to determine the minimum number of replacements needed to transform all occurrences of the substring "01" in S into the string "110" such that there are no remaining instances of the substring "01" in S.

Let’s understand this problem statement with examples.

Sample Example 1

Input

S = β€œ01”

Output

Minimum number of replacements required: 1

Explanation

The substring (0, 1) in the string "01" was selected and replaced with "110", resulting in the modified string "110".

After performing the above operations, the string S (now equal to "110") no longer contains the substring "01". Hence, only one operation was required to achieve this.

Sample Example 2

Input

S = β€œ001”

Output

Minimum number of replacements required: 3

Explanation

Step 1: The substring (1, 2) in the string "001" was selected and replaced with "110", resulting in the modified string "0110".

Step 2: Next, the substring (0, 1) in the string "0110" was chosen and replaced with "110", resulting in the modified string "11010".

Step 3: Finally, the substring (2, 3) in the string "11010" was selected and replaced with "110", resulting in the modified string "111100".

After executing the above operations, the string S (now equal to "111100") no longer contains any occurrence of the substring "01". Therefore, the total number of operations required is 3.

Algorithm

STEP 1. Define a function β€˜minimumOperations’ that takes the binary string β€˜S’ and its length β€˜N’ as input.

STEP 2. Initialize β€˜ans’ as 0 to store the number of operations performed and β€˜cntOne’ as 0 to store the count of encountered ones.

STEP 3. Traverse the string β€˜S’ from the end using a loop with β€˜i’ initialized to β€˜N - 1’.

STEP 4. Inside the loop, check if the current character β€˜S[i]’ is '0':

  • If it is '0', add the count of encountered ones β€˜cntOne’ to the answer β€˜ans’.

  • Double the count of encountered ones β€˜cntOne’ by multiplying it by 2.

STEP 5. If the current character β€˜S[i]’ is '1', increment the count of encountered ones β€˜cntOne’ by 1.

STEP 6. After the loop, print the minimum number of replacements required as "Minimum number of replacements required: " followed by the value of β€˜ans’.

STEP 7. In the β€˜main’ function, declare a binary string β€˜S’ and its length β€˜N’, then call the β€˜minimumOperations’ function passing β€˜S’ and β€˜N’ as arguments.

This algorithm efficiently computes the minimum number of replacements required to transform the binary string according to the given conditions.

Example

Implementation of the above algorithm using C++

The below C++ program solves the problem of finding the minimum number of replacements required to transform a binary string by replacing the substring "01" with "110" while ensuring that the transformed string does not contain the substring "10". The program utilizes an algorithm that iterates through the string from the end and keeps track of the count of encountered ones. It calculates the number of replacements by considering the occurrences of zeros and updating the count of ones accordingly. Finally, it prints the minimum number of replacements required.

#include <iostream>
#include <string>
// The function aims to determine the minimum count of replacements required to change every occurrence of "01" to "110"
// In order to avoid having the substring "10" within string S
void minimumOperations(std::string S, int N) {
   int ans = 0; // Keeps track of the total number of operations executed
   int cntOne = 0; // Holds the resulting count of substrings
   // Iterate through the string S starting from the last character
   for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '0') { // In the case where the current character is 0
         ans += cntOne; // Add the count of ones encountered so far to the answer
         cntOne *= 2; // Double the count of ones encountered so far
      } else {
         cntOne++; // If the current character is 1, increment the count of ones encountered so far
      }
   }
   std::cout << "Minimum number of replacements required: " << ans;
}
int main() {
   std::string S = "01";
   int N = S.length();
   minimumOperations(S, N);
   return 0;
}

Output

Minimum number of replacements required: 1

Conclusion

To sum up, the problem of finding the minimum number of replacements required to remove all occurrences of the substring "01" by replacing it with "110" while avoiding the substring "10" has been effectively addressed in this tutorial. We have provided a comprehensive explanation of the problem statement, outlined an algorithmic solution, and presented a detailed implementation in C++. By understanding the underlying concepts and following the step-by-step approach, readers can confidently solve similar string manipulation challenges.

Updated on: 08-Sep-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements