1980: Find-Unique-Binary-String
Medium
table of contents
This question has to be misclassified.
I sorted nums
and iterated through all the elements in
nums
until I found one that was missing, and returned
it.
code
class Solution {
public:
(vector<string>& nums) {
string findDifferentBinaryString(nums.begin(), nums.end());
sort(nums.size(), '0');
string tempfor (int i = 0; i < nums.size(); ++i) {
if (temp != nums[i]) {
return temp;
}
int index = nums.size()-1;
while(temp[index] == '1') {
[index--] = '0';
temp}
[index] = '1';
temp}
return temp;
}
};
complexity
However, you can just use Cantor’s Diagonal Argument.
Just iterate through all n
elements, and generate a
string of length n
, ans
, by making the
i
th bit in ans
the opposite of the
i
th bit in nums[i]
for
0 <= i < n
. Then, you know that ans
is
unique, since there is at least 1
bit that
is different in ans
to every element in
nums
.
code
class Solution {
public:
(vector<string>& nums) {
string findDifferentBinaryString= "";
string ans for (int i = 0; i < nums.size(); ++i) {
+= (nums[i][i] == '0') ? "1" : "0";
ans }
return ans;
}
};