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:2Explanation: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**‘?’**characters. - Now, the task reduces to finding the count of
**‘?’**s, say**count**. - Thereofore, the total number of different strings that can be formed is
**2**.^{count}

Follow the steps below to solve the problem:

- Count the number of occurrences of
**‘?’**, say**count** - Print the value of
**2**as the resultant combination of the strings formed.^{count}

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.` |

**Output:**

2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.