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

Given a string S consisting of characters 0, 1, and β€˜?’, the task is to count all possible combinations of the binary string formed by replacing β€˜?’ by 0 or 1.

Examples:

Input: S = β€œ0100?110”
Output: 2
Explanation: Replacing each β€˜?’s with β€˜1’ and β€˜0’, the count of such strings formed will be equal to β€œ01001110” and β€œ01000110”. Therefore, the total count of such strings formed is 2.

Input: S = β€œ00?0?111”
Output: 4

Approach: The given problem can be solved based on the following observations:

  • Since each β€˜?’ can be replaced by β€˜0’ or β€˜1’, two possible choices exist for every β€˜?’ character.
  • Now, the task reduces to finding the count of β€˜?’s, say count.
  • Therefore, the total number of different strings that can be formed is 2count.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++14




#include <bits/stdc++.h>
 
using namespace std;
 
//Function to count all possible
//permutations of the string s
void countPermutations(string s){
 
    //Stores frequency of the
    //character '?'
    int count = 0;
 
    //Traverse the string
    for (char i:s){
        if (i == '?')
            count += 1;
      }
 
    //Print the answer
    cout<<pow(2,count);
}
 
//Driver Code
int main()
{
  //Given string S
  string s = "0100?110";
 
  //Function call to count
  //the number of permutations
  countPermutations(s);
 
  return 0;
}


Java




// Java program for the above approach
class GFG{
 
// Function to count all possible
// permutations of the string s
static void countPermutations(String s)
{
 
    // Stores frequency of the
    // character '?'
    int count = 0;
 
    // Traverse the string
    for (char i : s.toCharArray())
    {
        if (i == '?')
            count += 1;
      }
 
    // Print the answer
    System.out.print((int)Math.pow(2,count));
}
 
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string S
  String s = "0100?110";
 
  // Function call to count
  // the number of permutations
  countPermutations(s);
}
}
 
// This code is contributed by code_hunt.


Python3




# Python3 program for the above approach
 
# Function to count all possible
# permutations of the string s
def countPermutations(s):
 
    # Stores frequency of the
    # character '?'
    count = 0
 
    # Traverse the string
    for i in s:
        if i == '?':
 
            # Increment count
            count += 1
 
    # Print the answer
    print(2**count)
 
# Driver Code
 
# Given string S
s = "0100?110"
 
# Function call to count
# the number of permutations
countPermutations(s)
 
# This code is contribute by mohit kumar 29.


C#




// C# program for above approach
using System;
 
public class GFG
{
   
// Function to count all possible
// permutations of the string s
static void countPermutations(string s)
{
 
    // Stores frequency of the
    // character '?'
    int count = 0;
 
    // Traverse the string
    foreach (char i in s)
    {
        if (i == '?')
            count += 1;
      }
 
    // Print the answer
    Console.WriteLine(Math.Pow(2,count));
}
 
// Driver code
public static void Main(String[] args)
{
   
    // Given string S
  string s = "0100?110";
 
  // Function call to count
  // the number of permutations
  countPermutations(s);
}
}
 
// This code is contributed by splevel62.


Javascript




<script>
 
 
//Function to count all possible
//permutations of the string s
function countPermutations(s){
 
    //Stores frequency of the
    //character '?'
    var count = 0;
 
    //Traverse the string
    for(var i =0; i< s.length; i++)
    {
        if (s[i] == '?')
            count += 1;
    }
 
    //Print the answer
    document.write( Math.pow(2,count));
}
 
//Driver Code
 
//Given string S
var s = "0100?110";
 
//Function call to count
//the number of permutations
countPermutations(s);
 
 
</script>


Output: 

2

 

Time Complexity: O(N)
Auxiliary Space: O(1)