# Minimize count of alternating subsequences to divide a given Binary String with subsequence number

Here, you are provided with a binary string as part of the issue. In order to prevent any subsequence from including adjacent zeros and ones, we must reduce the number of subsequences and output the subsequence number that corresponds to each string element.

A subsequence represents a sequence which can be created by taking the supplied sequence and eliminating zero or more members while maintaining the initial position of the elements that remain.

### Input

Let us consider the Input: str = “10010100”, and the length given is equal to 8.

### Output

3
1 1 2 2 2 2 2 3


Explanation − It is possible for there to be at least three subsequences without any adjacent ones or zeros.

### Input

Let us consider the Input: str = “10000”, and the length given is equal to 5.

### Output

4
1 1 2 3 4


Explanation − It is possible for there to be at least four subsequences without any adjacent ones or zeros.

### Input

Let us consider the Input: str = “101101”, and the length given is equal to 6.

### Output

2
1 1 1 2 2 2


Explanation − It is possible for there to be at least two subsequences without any adjacent ones or zeros.

### Input

Let us consider the Input: str = “10001000”, and the length given is equal to 8.

### Output

5
1 1 2 3 3 3 4 5


Explanation − It is possible for there to be at least five subsequences without any adjacent ones or zeros.

## Problem Statement

Implement a program to minimize count of alternating subsequences to divide a given binary string with subsequence number.

### Algorithm

The algorithm to minimize count of alternating subsequences to divide a given binary string with subsequence number is given below −

• Step 1 − Define a function findSubsequences(string str, int length).

This function determines the bare minimum of subsequences that str must split into as well as the subsequences that each character in str corresponds to.

• Step 2 − Now Define a vector.

The subsequences that each character in str belongs to are stored here in this vector.

• Step 3 − In order to traverse each and every character of the String str, add a loop.

• Step 4 − Define a variable newSubsequence which preserves track of how many additional subsequences will be created.

• Step 5 − check whether the character is '0'

• Step 6 − In the absence of a string which terminates with '1’, add newSubsequence into zeroSubSeq. Otherwise, Place the final element of oneSubSeq into newSubsequence and eliminate the final element of oneSubSeq.

• Step 7 − In the absence of a string which terminates with '0’, add newSubsequence into oneSubSeq. Otherwise, Place the final element of zeroSubSeq into newSubsequence and eliminate the final element of zeroSubSeq.

• Step 8 − Print the bare minimum number of subsequences obtained.

### Example (C++ Program)

Here is the C++ program implementation of the above written algorithm to Minimize count of alternating subsequences to divide a given Binary String with subsequence number

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

void findSubsequences(string str, int length){
vector<int> result(length);
vector<int> zeroSubSeq,oneSubSeq ;
for (int i = 0; i < length; ++i) {
int newSubsequence = zeroSubSeq.size() + oneSubSeq.size();
if (str[i] == '0') {
if (oneSubSeq.empty()) {
zeroSubSeq.push_back(newSubsequence);
} else {
newSubsequence = oneSubSeq.back();
oneSubSeq.pop_back();
zeroSubSeq.push_back(newSubsequence);
}
} else {
if (zeroSubSeq.empty()) {
oneSubSeq.push_back(newSubsequence);
} else {
newSubsequence = zeroSubSeq.back();
zeroSubSeq.pop_back();
oneSubSeq.push_back(newSubsequence);
}
}
result[i] = newSubsequence;
}
cout << zeroSubSeq.size() + oneSubSeq.size() << endl;
for (int i = 0; i < length; ++i) {
cout << result[i] + 1 << " ";
}
}
int main(){
string str = "10010100";
int length = 8;
findSubsequences(str, length);
return 0;
}


### Output

On execution, it will produce the following output −

3
1 1 2 2 2 2 2 3


## Conclusion

Likewise, we can find a way to minimize count of alternating subsequences to divide given binary string with subsequence number. The challenge of obtaining the program to mminimize count of alternating subsequences to divide a given binary string with subsequence number is resolved in this article.

Here C++ programming code as well as the algorithm to minimize count of alternating subsequences to divide a given binary string with subsequence number are provided.

Updated on: 31-Oct-2023

31 Views