30. Generate All Binary Strings
โ GFG solution to Generate All Binary Strings problem: generate all possible binary strings of length n using bit manipulation, backtracking, and iterative techniques. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given an integer n
, you need to generate all the binary strings of n
characters representing bits. Each position in the string can be either '0' or '1', resulting in 2^n possible combinations.
Note: Return the strings in ascending order.
๐ Examples
Example 1
Input: n = 2
Output: [00, 01, 10, 11]
Explanation: As each position can be either 0 or 1, the total possible combinations are 4.
Example 2
Input: n = 3
Output: [000, 001, 010, 011, 100, 101, 110, 111]
Explanation: As each position can be either 0 or 1, the total possible combinations are 8.
๐ Constraints
$1 \le n \le 20$
โ
My Approach
The optimal approach uses Bit Manipulation to generate all binary strings efficiently by treating each number from 0 to 2^n-1 as a binary representation:
Bit Manipulation Approach
Calculate Total Combinations:
Total number of binary strings = 2^n, computed using left shift:
(1 << n)
.
Iterate Through All Numbers:
Loop from 0 to 2^n - 1, where each number represents a unique binary string.
Extract Binary Representation:
For each number, extract its binary form by checking each bit from position n-1 to 0.
Use right shift and bitwise AND operations:
((i >> j) & 1)
.
Build String:
Append '1' if bit is set, '0' otherwise.
Add the constructed string to result vector.
Return Result:
All strings are naturally generated in ascending order due to sequential number iteration.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n * 2^n), as we generate 2^n strings, and each string construction takes O(n) time to build n characters.
Expected Auxiliary Space Complexity: O(1), excluding the output space, as we only use constant extra space for loop variables and temporary string construction.
๐งโ๐ป Code (C++)
class Solution {
public:
vector<string> binstr(int n) {
vector<string> res;
for (int i = 0; i < (1 << n); i++) {
string s = "";
for (int j = n - 1; j >= 0; j--) s += ((i >> j) & 1) ? '1' : '0';
res.push_back(s);
}
return res;
}
};
โ Code (Java)
class Solution {
public ArrayList<String> binstr(int n) {
ArrayList<String> res = new ArrayList<>();
for (int i = 0; i < (1 << n); i++) {
StringBuilder s = new StringBuilder();
for (int j = n - 1; j >= 0; j--) s.append((i >> j & 1) == 1 ? '1' : '0');
res.add(s.toString());
}
return res;
}
}
๐ Code (Python)
class Solution:
def binstr(self, n):
res = []
for i in range(1 << n):
s = ""
for j in range(n - 1, -1, -1):
s += '1' if (i >> j) & 1 else '0'
res.append(s)
return res
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated