Count permutations possible by replacing ‘?’ characters in a Binary String
Input: S = “0100?110”
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”
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 count
- Print the value of 2count as the resultant combination of the strings formed.
Below is the implementation of the above approach:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.