Try the Problem
You can solve the problem here:
https://leetcode.com/problems/find-unique-binary-string/
Problem Description
You are given an array nums containing n unique binary strings, where each string has length n.
Your task is to return any binary string of length n that does not appear in the array.
Important Conditions
- Each string consists only of
'0'and'1'. - Every string in the array is unique.
- The output must be a binary string of length
n. - If multiple valid answers exist, any one of them is acceptable.
Examples
Example 1
Input
Output
Explanation
Possible binary strings of length 2:
Since "01" and "10" are already present, valid answers could be:
Example 2
Input
Output
Another valid output could be:
Example 3
Input
Output
Other valid answers include:
Constraints
Important Observation
The total number of binary strings of length n is:
But the array contains only:
Since 2^n grows very quickly and n ≤ 16, there are many possible binary strings missing from the array. Our goal is simply to construct one of those missing strings.
Thinking About the Problem
Before jumping into coding, it's useful to think about different strategies that could help us generate a binary string that does not appear in the array.
Possible Ways to Think About the Problem
When approaching this problem, several ideas may come to mind:
- Generate all possible binary strings of length
nand check which one is missing. - Store all strings in a HashSet or HashMap and construct a candidate string to verify whether it exists.
- Manipulate existing strings by flipping bits to create new combinations.
- Use a mathematical trick that guarantees the new string is different from every string in the list.
Each of these approaches leads to a different solution strategy.
In this article, we will walk through these approaches and understand how they work.
Approach 1: Brute Force – Generate All Binary Strings
Idea
The simplest idea is to generate every possible binary string of length n and check whether it exists in the given array.
Since there are:
We can generate them one by one and return the first string that does not appear in nums.
Steps
- Convert numbers from
0to(2^n - 1)into binary strings. - Pad the binary string with leading zeros so its length becomes
n. - Check if that string exists in the array.
- If not, return it.
Time Complexity
This works because n is at most 16, but it is still not the most elegant approach.
Approach 2: HashMap + Bit Flipping (My Approach)
Idea
While solving this problem, another idea is to store all given binary strings inside a HashMap for quick lookup.
Then we can try to construct a new binary string by flipping bits from the existing strings.
The intuition is simple:
- If the current character is
'0', change it to'1'. - If the current character is
'1', change it to'0'.
By flipping bits at different positions, we attempt to build a new binary combination.
Once the string is constructed, we check whether it already exists in the map.
If the generated string does not exist, we return it as our answer.
Java Implementation (My Solution)
Time Complexity
Because we iterate through the array and perform string operations.
Space Complexity
For storing the strings in the HashMap.
Approach 3: Cantor’s Diagonalization (Optimal Solution)
Idea
A clever mathematical observation allows us to construct a string that must differ from every string in the array.
We build a new string such that:
- The first character differs from the first string.
- The second character differs from the second string.
- The third character differs from the third string.
And so on.
By ensuring that the generated string differs from each string at least at one position, it is guaranteed not to exist in the array.
This technique is known as Cantor’s Diagonalization.
Java Implementation
Time Complexity
We only traverse the array once.
Space Complexity
For storing the resulting string.
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Notes |
| Brute Force | O(2^n * n) | O(n) | Simple but inefficient |
| HashMap + Bit Flipping | O(n²) | O(n) | Constructive approach |
| Cantor Diagonalization | O(n) | O(n) | Optimal and elegant |
Key Takeaways
This problem highlights an interesting concept in algorithm design:
Sometimes the best solution is not searching for the answer but constructing one directly.
By understanding the structure of the input, we can generate a result that is guaranteed to be unique.
Conclusion
The Find Unique Binary String problem can be solved using multiple strategies, ranging from brute force enumeration to clever mathematical construction.
While brute force works due to the small constraint (n ≤ 16), more elegant solutions exist. Using hashing or constructive approaches improves efficiency and demonstrates deeper algorithmic thinking.
Among all approaches, the Cantor Diagonalization technique provides the most efficient and mathematically guaranteed solution.
Understanding problems like this helps strengthen skills in string manipulation, hashing, and constructive algorithms, which are commonly tested in coding interviews.




