I have to write algorithm that will generate all possible combinations of different binary strings but under some conditions. The combinations are created by: Replacing binary "1" with "00"
Other conditions:
- Input binary string, if contains 0, they are in pairs always, so "00"
- The output also can contain 0 only in pairs
Example:
Input:
11
Output:
001
100
0000
11
In above example, there is no 010
, because as mentioned earlier, the "0" must have a pair (another "0")
Note that if given binary string contains "00", we don't change them to 1.
In other words, the algorithm should determine how many different binary strings can be created by replacing "1" with "00" (but under the conditions present above), for given binary string and returns all the possible combinations.
I tried O(n^2) algorithm, recursion but can't achieve my goal :/ That's my code:
void get_combinations(const std::string& bin, std::set<std::string>& result) {
result.insert(bin);
for (int i = 0; i < bin.length(); i++) {
std::string local_combination = bin;
for (int j = i; j < bin.length(); j++) {
if (local_combination[j] == '1') {
local_combination[j] = '0';
local_combination.insert(j, "0");
result.insert(local_combination);
}
}
}
}
It works e.g. for simple input 10
, 01
. But for input 11
, the output doesn't contain 0000
. For "longer" inputs, like 1111
it gives completely bad output.