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.
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.
Solution 1
public string FindDifferentBinaryString(string[] nums) {
var length = nums[0].Length;
for (int i = 0; i < nums.Length; i++) {
nums[i] = Convert.ToInt32(nums[i], 2).ToString();
}
// find number which is not in this
int j = 0;
for (j = 0; j < nums.Length; j++) {
if (!nums.Contains(j.ToString())) {
break;
}
}
// print the output
var output = Convert.ToString(j, 2);
while (output.Length < length) {
output = "0" + output;
}
return output;
}
Solution 2
public string FindDifferentBinaryString(string[] nums) {
var length = nums[0].Length;
var newStr = new int[length];
for (int i = 0; i < length; i++)
{
newStr[i] = 0;
}
// find number which is not in this
for (int i = length - 1; i > -1; i--)
{
for (short k = 0; k < 2; k++)
{
newStr[i] = k;
var joinedString = string.Join("", newStr);
// check number
if (!nums.Contains(joinedString))
{
return joinedString;
}
for (int j = length - 1; j > i; j--)
{
for (int l = 0; l < 2; l++)
{
// replace 0 and 1 each time at j
newStr[j] = l;
joinedString = string.Join("", newStr);
// check number
if (!nums.Contains(joinedString))
{
return joinedString;
}
}
}
}
}
return string.Join("", newStr);
}