Check if a Binary String contains A pairs of 0s and B independent 0s or not

Checking if a Binary String contains A pairs of 0s and B independent 0s or not is a common problem encountered in computer science, particularly in the field of algorithms and data structures. The problem statement is quite simple and plays a significant role in various fields, such as cryptography, network security, and machine learning.

In this tutorial, we will discuss a solution to this problem using C++. We will first provide an overview of the approach starting with defining the problem statement with some examples and, then we will dive into the implementation details. So let’s get started!

Problem Statement

Given a binary string of length n consisting of 0s and 1s, we need to determine whether it contains A pairs of adjacent 0s (00) and B independent 0s (0 that is not adjacent to another 0).

Sample Examples

Example 1

Input:
s = "100101000"
A = 2
B = 1
Output:
Yes


Explanation: In the input string, there are two pairs of adjacent 0s: "10|010|1000". Also, there is one independent 0: "100|1|01000". Therefore, the output is "Yes" since the input string contains two pairs of adjacent 0s and one independent 0.

Example 2

Input:
s = "110010010"
A = 3
B = 2
Output:
No


Explanation: In the input string, there are three pairs of adjacent 0s: "1|100|100|10". However, there are only two independent 0s: "1100|1|0010". Therefore, the output is "No" since the input string does not contain two independent 0s.

Algorithm

• Initialize pairs and singles counters to zero.

• Iterate through the input string s using a loop with index i from 0 to n-2.

• Check if the current character and the next character are both '0'.

• If yes, increment pairs by 1 and skip the next character by incrementing i by 1.

• Otherwise, check if the current character is '0' and the next character is '1'.

• If yes, increment singles by 1.

• Check if the last character of s is '0'.

• If yes, increment singles by 1.

• Return true if the number of pairs is greater than or equal to A AND the number of singles is greater than or equal to B, otherwise, return false.

Example

Implementation of the above algorithm using C++

In this implementation, we define a function called 'contains_zeros' that takes in a binary string 's' and two integers 'A' and 'B'. The function returns 'true' if the string contains at least 'A' pairs of adjacent zeros and 'B' independent zeros; otherwise, it returns 'false'.

To determine the number of pairs and independent zeros in the string, we iterate through the string and keep track of the number of pairs and independent zeros we encounter. We skip the next character if we find a pair of adjacent zeros. At the end of the loop, we check if the last character is a zero and increment the count of independent zeros accordingly.

Finally, in the 'main' function, we call 'contains_zeros' with the input string 's', and the values of 'A' and 'B'. We print "Yes" if the function returns 'true', and "No" otherwise.

#include <iostream>
#include <string>
using namespace std;
bool contains_zeros(string s, int A, int B) {
int n = s.size();
int pairs = 0;
int singles = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == '0' && s[i+1] == '0') {
pairs++;
i++; // Skip the next character
} else if (s[i] == '0' && s[i+1] == '1') {
singles++;
}
}
if (s[n-1] == '0') {
singles++;
}
return pairs >= A && singles >= B;
}
int main() {
string s = "100101000";
int A = 2;
int B = 1;
bool result = contains_zeros(s, A, B);
cout << "Input:" << endl;
cout << "s = \"" << s << "\"" << endl;
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << endl;
cout << "Output:" << endl;
if (result) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}


Output

On execution the above C++ program will produce the following output:

Input:
s = "100101000"
A = 2
B = 1

Output:
Yes


Conclusion

In conclusion, we have discussed how to check whether a binary string contains a given number of pairs of 0s and independent 0s using a C++ program. We have provided an algorithm and implemented a function that takes a string 's' and two integers 'A' and 'B' as inputs and returns a boolean value indicating whether 's' contains at least 'A' pairs of 0s and 'B' independent 0s.

The time complexity of the algorithm is O(n), where n is the length of the input string 's' since we need to iterate through 's' once to count the number of pairs and singles.

Updated on: 08-Sep-2023

53 Views