Count permutations possible by replacing β€˜?’ characters in a Binary String


In this problem, we have given a string containing 0, 1, and ? characters. We need to find permutations of the string by replacing β€˜?’ with 0 and 1. The logic to solve the problem is that we can replace every β€˜?’ with either 0 or 1. So, by replacing one β€˜?’, we can generate two different permutations, and by replacing N β€˜?’ with 2 possibilities, we can generate 2^N permutations.

In this tutorial, we will learn two different approaches to solving the given problem.

Problem statement – We have given string str containing β€˜0’, β€˜1’ and β€˜?’ characters. We need to replace the β€˜?’ with 0 or 1 and find all permutations of the given string.

Sample examples

Input β€“ str = "0101001??01"

Output β€“ 4

Explanation β€“ The 4 different combinations are 01010010101, 01010011001, 01010010001, and 01010011101

Input β€“ str = ’01?0’

Output β€“ 2

Explanation β€“ The 2 different combinations are 0100, and 0110.

Input β€“ str = β€˜01010’

Output β€“ 1

Explanation β€“ The given string is a single permutation itself.

Approach 1

In this approach, we will count the total number of β€˜? In the given string, by traversing the string. After that, we will use the left shift operator to get the 2^N, where N is the total number of β€˜?’ in the string.

Algorithm

  • Define the β€˜cnt’ variable and initialize with 0 to store the count of β€˜?’ in the given string

  • Traverse the string using the loop. If the current character is β€˜?’, increase the value of the β€˜cnt’ variable by 1.

  • Use the left shift operator to get the 2cnt and store it to the β€˜perm’ variable.

  • Return the value of the β€˜perm’ variable

Example

#include <iostream>
using namespace std;

// function to count the number of permutations of the given string by replacing '?' with '0' or '1'
int totalPermutations(string s){
   // variable to store the count of '?' in the string
   int cnt = 0;
   // loop to count the number of '?' in the string
   for (int i = 0; i < s.length(); i++){
      if (s[i] == '?'){
         cnt++;
      }
   }
   // total number of permutations of the string by replacing'?' with '0' or '1' is 2^cnt
   int perm = 0;
   //   calculating 2^cnt with left shift operator
   perm = 1 << cnt;
   return perm;
}
int main(){
   string str = "0101001??01";
   cout << "The total number of permutations of the string we can get by replacing ? with 0 or 1 is " << totalPermutations(str);
   return 0;
}

Output

The total number of permutations of the string we can get by replacing ? with 0 or 1 is 4

Time complexity – O(N), as we traverse the string.

Space complexity – O(1), as we don’t use dynamic space.

Approach 2

In this approach, we will use the count() method to count the total number of β€˜?’ characters in the given string. After that, we use the pow() method to calculate the 2^N.

Algorithm

  • Define the β€˜cnt’ variable and initialize it with the zero.

  • Use the count() method to count the total number of occurrences of β€˜?’ in the given string. We need to pass the starting point of the string as a first parameter, the ending point as a second parameter, and β€˜?’ as a third parameter.

  • Use the pow() method to get the 2cnt.

  • Return the β€˜perm’ value.

Example

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

// function to count the number of permutations of the given string by replacing '?' with '0' or '1'
int totalPermutations(string s){
   // variable to store the count of '?' in the string
   int cnt = 0;
   //    using the count() function to count the number of '?' in the string
   cnt = count(s.begin(), s.end(), '?');
   int perm = 0;
   //   calculating 2^cnt using the pow() function
   perm = pow(2, cnt);
   return perm;
}
int main(){
   string str = "0101001??01";
   cout << "The total number of permutations of the string we can get by replacing ? with 0 or 1 is " << totalPermutations(str);
   return 0;
}

Output

The total number of permutations of the string we can get by replacing ? with 0 or 1 is 4

Time complexity – O(N), as the count() method iterates the string of length N.

Space complexity – O(1)

We learned two different approaches to calculate the total number of permutations we can get by replacing the β€˜?’ character with 0 or 1. The programmer can use the pow() method of the left shift operator to count the 2N.

Updated on: 10-Aug-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements