Find Unique Binary String
LeetCode 2107 | Difficulty: Mediumβ
MediumProblem Descriptionβ
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**.
Topics: Array, Hash Table, String, Backtracking
Approachβ
Backtrackingβ
Explore all candidates by building solutions incrementally. At each step, choose an option, explore further, then unchoose (backtrack) to try the next option. Prune branches that can't lead to valid solutions.
Generate all combinations/permutations, or find solutions that satisfy constraints.
Hash Mapβ
Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?
Need fast lookups, counting frequencies, finding complements/pairs.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 138 ms)β
| Metric | Value |
|---|---|
| Runtime | 138 ms |
| Memory | 37.5 MB |
| Date | 2022-01-21 |
public class Solution {
public string FindDifferentBinaryString(string[] nums) {
string result = "";
for(int i=0;i<nums.Length;i++)
{
result += (nums[i][i]=='0') ? '1' : '0';
}
return result;
}
}
π 1 more C# submission(s)
Submission (2022-01-21) β 159 ms, 37.5 MBβ
public class Solution {
public string FindDifferentBinaryString(string[] nums) {
List<long> seen = nums.Select(x=>GetNumber(x)).ToList();
double len = Math.Pow(2, nums[0].Length);
for (long i = 0; i < len; i++)
{
if(!seen.Contains(i)) return GetBinaryString(i,nums[0].Length);
}
return string.Empty;
}
private string GetBinaryString(long n, int len)
{
char[] result = new char[len];
for (int i = 0; i < len; i++)
{
result[i] = '0';
}
int index = len-1;
while(n>0)
{
result[index--] = (char)(n%2+'0');
n = n/2;
}
return new string(result);
}
private long GetNumber(string num)
{
long result = 0;
foreach(var c in num)
{
result = result*2 + (c-'0');
}
return result;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Backtracking | $O(n! or 2^n)$ | $O(n)$ |
| Hash Map | $O(n)$ | $O(n)$ |
Interview Tipsβ
- Discuss the brute force approach first, then optimize. Explain your thought process.
- Hash map gives O(1) lookup β think about what to use as key vs value.
- Identify pruning conditions early to avoid exploring invalid branches.
- LeetCode provides 2 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: We can convert the given strings into base 10 integers.
Hint 2: Can we use recursion to generate all possible strings?