Find Unique Binary String - 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.

Input & Output

Example 1 — Basic Case
$ Input: nums = ["01","10"]
Output: "00"
💡 Note: Using diagonal approach: flip bits at positions [0,1] from strings ["01","10"] → flip '0' to '1' and '1' to '0' → "10". Or we can return "00" or "11" as they're not in the input.
Example 2 — Single Element
$ Input: nums = ["0"]
Output: "1"
💡 Note: Only one string "0" exists, so we return the other possible string "1".
Example 3 — Three Strings
$ Input: nums = ["111","011","001"]
Output: "101"
💡 Note: Using diagonal: flip bits at positions [0,1,2] from ["111","011","001"] → flip '1','1','1' → "000". Any missing string like "101" is valid.

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

Visualization

Tap to expand
Find Unique Binary StringInput: n=2 strings"01""10"All possible 2-bit strings:00011011AvailableGivenGivenAvailableMissing strings:"00""11"Return any missing string: "00" or "11"
Understanding the Visualization
1
Input Array
n binary strings of length n
2
Find Missing
One string must be missing from all 2^n possibilities
3
Return Any Valid
Any missing string is a correct answer
Key Takeaway
🎯 Key Insight: With n unique strings of length n, exactly 2^n - n strings are missing. We can use Cantor's diagonal argument to construct one efficiently.
Asked in
Google 12 Facebook 8 Amazon 6
32.1K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen