Skip to content

Commit 50b0cb2

Browse files
authored
Create Find Unique Binary String.java
1 parent 5fb999c commit 50b0cb2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Medium/Find Unique Binary String.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
3+
private static final char[] BINARY_CHARS = {'1', '0'};
4+
5+
public String findDifferentBinaryString(String[] nums) {
6+
int n = nums.length;
7+
Set<String> binaryStrings = new HashSet<>();
8+
backtrack(new StringBuilder(), n, binaryStrings);
9+
Set<String> binaryStringsPresent = Arrays.stream(nums).collect(Collectors.toSet());
10+
binaryStrings.removeAll(binaryStringsPresent);
11+
return binaryStrings.isEmpty() ? "" : binaryStrings.iterator().next();
12+
}
13+
14+
private void backtrack(StringBuilder sb, int n, Set<String> binaryStrings) {
15+
if (sb.length() == n) {
16+
binaryStrings.add(sb.toString());
17+
return;
18+
}
19+
for (char c : BINARY_CHARS) {
20+
sb.append(c);
21+
backtrack(sb, n, binaryStrings);
22+
sb.deleteCharAt(sb.length() - 1);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)