[LeetCode] 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
int toInt(string& s) {
int res = 0;
for(int i = s.length() - 1; i >= 0; i--) {
if(s[i] == '1')
res |= 1<<i;
}
return res;
}
public:
string findDifferentBinaryString(vector<string>& nums) {
unordered_set<int> us;
for(auto& n : nums) {
us.insert(toInt(n));
}
int len = nums[0].length();
for(int i = 0; ; i++) {
if(us.count(i)) continue;
string res(len, '0');
for(int j = 0; j < len; j++) {
if(i & (1<<j)) res[j] = '1';
}
return res;
}
return "-1";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/13/PS/LeetCode/find-unique-binary-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.