Problem
Given an array of strings nums
containing n
unique binary strings each of length n
, return a binary string of length n
that does not appear in nums
. If there are multiple answers, you may return any of them.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints
n == nums.length
1 <= n <= 16
nums[i].length == n
nums[i]
is either'0'
or'1'
.- All the strings of
nums
are unique.
Solution
We are tasked with generating a binary string of length n
that does not exist in the input array nums
.
Video explanation
Here is the video explaining below methods in detail. Please check it out:
Method 1 - Convert to number and find first not in set
By converting binary strings to integers, we can work with numbers and significantly simplify operations like determining what’s missing. Let’s break this approach step by step:
Here is the approach:
- Convert Binary Strings to Integer Values:
- Each binary string in
nums
is converted to an integer using the binary-to-decimal conversion. - For example, the binary string
"001"
becomes the integer1
(in base 10).
- Each binary string in
- Store Integer Values in a HashSet:
- Add all integer representations of the binary strings to a
HashSet
for quick lookups.
- Add all integer representations of the binary strings to a
- Find the First Missing Integer:
- Since the total number of binary strings of length
n
is2^n
, iterate from0
to2^n - 1
. The first number not found in the HashSet is the desired result.
- Since the total number of binary strings of length
- Convert the Missing Number Back to a Binary String:
- Convert the integer back to a binary string of length
n
using built-in libraries, ensuring leading zeroes are preserved.
- Convert the integer back to a binary string of length
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- Converting each binary string in
nums
to its integer representation (O(n)
per string for parsing, assumingnums[i]
is of lengthn
) and adding it to theHashSet
occurs forn
strings. Total cost:O(n²)
(O(n)
parsing cost ×n
strings). - The missing number is found by iterating over integers from
0
to2^n - 1
, performing anO(1)
membership lookup in theHashSet
.- However, due to the optimization, we exit the loop as soon as we find the first missing number:
- At worst, we perform n iterations in practice because there is always at least one missing binary string when
nums
is of sizen
. - Membership checks in a
HashSet
run inO(1)
.
- Converting the missing number (an integer) back to a binary string of length
n
usesO(n)
logic.
- Converting each binary string in
- 🧺 Space complexity:
O(n^2)
. - Contains up ton
integers (integer representations of input binary strings), which isO(n)
in space.
Method 2 - Recursion and Backtracking
Here is the approach:
- Convert the array
nums
into a HashSet:- HashSet makes it efficient (
O(1)
lookup time) to check if a particular binary string exists innums
.
- HashSet makes it efficient (
- Use Backtracking to Generate Binary Strings:
- Start from an empty string and iteratively try to build every possible binary string of length
n
using “0” and “1”. - As soon as a binary string is generated that doesn’t exist in the HashSet, return it immediately.
- Start from an empty string and iteratively try to build every possible binary string of length
- This approach systematically generates binary strings and leverages the HashSet to ensure we don’t return a string that’s already in
nums
.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- We need to convert
nums
, a list ofn
binary strings each of lengthn
, into a HashSet to enableO(1)
lookups.- The cost of adding a single string of length
n
to a HashSet isO(n)
. - For
n
strings, the total cost isO(n * n)
, i.e.,O(n²)
.
- The cost of adding a single string of length
- We have n binary strings in
nums
, and since we search only for missing strings, at most we perform up toO(n)
iterations (as there is one missing binary string). - At each recursive call:
- A string concatenation operation (
curr + "0"
orcurr + "1"
) occurs. This operation requires creating a new string of size up ton
in Java, which costsO(n)
.
- A string concatenation operation (
- Given that string concatenations occur for each recursive call, the overall cost of the backtracking process is bounded by
O(n * n)
, since we generate up toO(n)
binary strings during the recursion, and each operation costsO(n)
.
- We need to convert
- 🧺 Space complexity:
O(n^2)
- The dominating costs are the
O(n²)
for HashSet construction andO(n²)
for backtracking.
- The dominating costs are the
Method 3 - Genius Solution using Cantor’s Diagonal Argument
Since all strings are unique so we just need to check ith
bit of ith
string and put its opposite in our answer
One of the clean and effective strategies to ensure uniquely generating such a binary string is leveraging the diagonal approach (related to the Cantor Diagonalisation argument). Here’s the reasoning for the approach:
- Constructing the Diagonal String:
- For every index
i
(0-indexed), take the complement (flip) of the binary value ('0'
->'1'
and'1'
->'0'
) from the string located at indexi
and positioni
in the arraynums
.
- For every index
- The constructed string is guaranteed not to exist in
nums
because it differs from the string at indexi
specifically in thei-th
position. - Return this constructed (complemented diagonal) string.
Here is how the dry run will work:
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
as we iterate overnums
exactly once, extracting and flipping the diagonal elements to construct the new binary string. - 🧺 Space complexity:
O(n)
, as the space is used to store the result stringans
of lengthn
.