Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: nums = ["01","10"] Output: "11" Explanation: "11" does not appear in nums. "00" would also be correct.
Example 2:
Input: nums = ["00","01"] Output: "11" Explanation: "11" does not appear in nums. "10" would also be correct.
Example 3:
Input: nums = ["111","011","001"] Output: "101" Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
Constraints:
n == nums.length1 <= n <= 16nums[i].length == nnums[i] is either '0' or '1'.nums are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Backtracking
["01","10"]
["00","01"]
["111","011","001"]
missing-number)find-all-numbers-disappeared-in-an-array)random-pick-with-blacklist)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1980: Find Unique Binary String
class Solution {
public String findDifferentBinaryString(String[] nums) {
int mask = 0;
for (var x : nums) {
int cnt = 0;
for (int i = 0; i < x.length(); ++i) {
if (x.charAt(i) == '1') {
++cnt;
}
}
mask |= 1 << cnt;
}
for (int i = 0;; ++i) {
if ((mask >> i & 1) == 0) {
return "1".repeat(i) + "0".repeat(nums.length - i);
}
}
}
}
// Accepted solution for LeetCode #1980: Find Unique Binary String
func findDifferentBinaryString(nums []string) string {
mask := 0
for _, x := range nums {
mask |= 1 << strings.Count(x, "1")
}
for i := 0; ; i++ {
if mask>>i&1 == 0 {
return strings.Repeat("1", i) + strings.Repeat("0", len(nums)-i)
}
}
}
# Accepted solution for LeetCode #1980: Find Unique Binary String
class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
mask = 0
for x in nums:
mask |= 1 << x.count("1")
n = len(nums)
for i in range(n + 1):
if mask >> i & 1 ^ 1:
return "1" * i + "0" * (n - i)
// Accepted solution for LeetCode #1980: Find Unique Binary String
/**
* [1980] Find Unique Binary String
*
* 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.
*
* Example 1:
*
* Input: nums = ["01","10"]
* Output: "11"
* Explanation: "11" does not appear in nums. "00" would also be correct.
*
* Example 2:
*
* Input: nums = ["00","01"]
* Output: "11"
* Explanation: "11" does not appear in nums. "10" would also be correct.
*
* Example 3:
*
* Input: nums = ["111","011","001"]
* Output: "101"
* Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
*
*
* 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-unique-binary-string/
// discuss: https://leetcode.com/problems/find-unique-binary-string/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn find_different_binary_string(nums: Vec<String>) -> String {
(0..nums.len())
.map(|i| (nums[i].as_bytes()[i] ^ 1) as char)
.collect()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1980_example_1() {
let nums = vec_string!["01", "10"];
let result = "11".to_string();
assert_eq!(Solution::find_different_binary_string(nums), result);
}
#[test]
#[ignore]
fn test_1980_example_2() {
let nums = vec_string!["00", "01"];
let result = "11".to_string();
assert_eq!(Solution::find_different_binary_string(nums), result);
}
#[test]
#[ignore]
fn test_1980_example_3() {
let nums = vec_string!["111", "011", "001"];
let result = "101".to_string();
assert_eq!(Solution::find_different_binary_string(nums), result);
}
}
// Accepted solution for LeetCode #1980: Find Unique Binary String
function findDifferentBinaryString(nums: string[]): string {
let mask = 0;
for (let x of nums) {
const cnt = x.split('').filter(c => c === '1').length;
mask |= 1 << cnt;
}
for (let i = 0; ; ++i) {
if (((mask >> i) & 1) === 0) {
return '1'.repeat(i) + '0'.repeat(nums.length - i);
}
}
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.