Count permutations possible by replacing '?' characters in a Binary String Last Updated : 27 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 '?' 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: Count the number of occurrences of '?', say countPrint the value of 2count as the resultant combination of the strings formed. 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) Comment More infoAdvertise with us Next Article Count of possible distinct Binary strings after replacing "11" with "0" S soumibardhan10 Follow Improve Article Tags : Strings Mathematical Combinatorial DSA Google MAQ Software interview-preparation binary-string frequency-counting +5 More Practice Tags : GoogleMAQ SoftwareCombinatorialMathematicalStrings +1 More Similar Reads Count of possible distinct Binary strings after replacing "11" with "0" Given a binary string str of size N containing 0 and 1 only, the task is to count all possible distinct binary strings when a substring "11" can be replaced by "0". Examples: Input: str = "11011"Output: 4Explanation: All possible combinations are "11011", "0011", "1100", "000". Input: str = "1100111 15 min read Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there 10 min read Count of possible Strings by replacing consonants with nearest vowel Given a string str consisting of N letters, the task is to find the total number of strings that can be generated by replacing each consonant with the vowel closest to it in the English alphabet. Examples: Input: str = "code"Output: 2Explanation: Str = "code" has two consonant c and d. Closest vowel 4 min read Count of 1-bit and 2-bit characters in the given binary string Given two special characters, the first character can be represented by one bit which is 0 and the second character can be represented by two bits either 10 or 11. Now given a string represented by several bits. The task is to return the number of characters it represents. Note that the given string 4 min read Count all possible strings that can be generated by placing spaces Given a string S, the task is to count all possible strings that can be generated by placing spaces between any pair of adjacent characters of the string. Examples: Input: S = "AB"Output: 2Explanation: All possible strings are { "A B", "AB"}. Input: S = "ABC"Output: 4Explanation: All possible string 4 min read Check If it is Possible to Convert Binary String into Unary String Given a binary string S of length N. You can apply following operation on S any number of times. Choose two adjacent characters, such that both are 1's or 0's. Then invert them, Formally, if both are 0's then into 1's or vice-versa. Then your task is to output YES or NO, by checking that using given 6 min read Like