/
FindUniqueBinaryString.java
48 lines (44 loc) · 1.1 KB
/
FindUniqueBinaryString.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// https://leetcode.com/problems/find-unique-binary-string
class Solution {
String[] zeroes = {
"",
"0",
"00",
"000",
"0000",
"00000",
"000000",
"0000000",
"00000000",
"000000000",
"0000000000",
"00000000000",
"000000000000",
"0000000000000",
"00000000000000",
"000000000000000",
"0000000000000000",
};
public String findDifferentBinaryString(String[] nums) {
int n = nums.length;
boolean[] tabill = new boolean[1 << n];
int last = -1, start = 0;
Arrays.sort(nums);
for (String num: nums) {
int x = Integer.parseInt(num, 2);
if (x - last > 1) {
start = x - 1;
break;
}
tabill[x] = true;
last = x;
}
for (int i = start; i < (1 << n); i++) {
if (!tabill[i]) {
String res = Integer.toBinaryString(i);
return zeroes[n - res.length()] + res;
}
}
return "";
}
}