forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1980.java
36 lines (34 loc) · 1.1 KB
/
_1980.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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class _1980 {
public static class Solution1 {
public String findDifferentBinaryString(String[] nums) {
Set<String> set = new HashSet<>(Arrays.asList(nums));
int len = nums[0].length();
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < len) {
sb.append(1);
i++;
}
int max = Integer.parseInt(sb.toString(), 2);
for (int num = 0; num <= max; num++) {
String binary = Integer.toBinaryString(num);
if (binary.length() < len) {
sb.setLength(0);
sb.append(binary);
while (sb.length() < len) {
sb.insert(0, "0");
}
binary = sb.toString();
}
if (!set.contains(binary)) {
return binary;
}
}
return null;
}
}
}