
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Permutations 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.