Skip to content

Commit ee39d6b

Browse files
author
rkayan
committed
File(s) Modified: 1980-find-unique-binary-string.java
Language(s) Used: java Submission URL: https://leetcode.com/problems/find-unique-binary-string/submissions/1042550864/
1 parent 7a18c24 commit ee39d6b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
/**
3+
* If k is the length of the String and there are n Strings
4+
* Time Complexity = O(2^k)
5+
* Space Complexity = n*k (for set) + k (for the current sequence)
6+
*/
7+
public String findDifferentBinaryString(String[] nums) {
8+
Set<String> uniqueNums = Set.of(nums);
9+
return helper(uniqueNums, uniqueNums.size(), new StringBuffer());
10+
}
11+
12+
String helper(Set<String> uniqueStr, int size, StringBuffer currentSeq) {
13+
//Check if current sequence has reached to required length
14+
if (currentSeq.length() == size) {
15+
//Check if current sequence exists in the provided list , we can keep track of it as global flag too
16+
if (!uniqueStr.contains(currentSeq.toString())) {
17+
return currentSeq.toString();
18+
}
19+
//current sequence is not unique
20+
return null;
21+
}
22+
23+
//Only if current sequence length is smaller than required length
24+
currentSeq.append("0");
25+
String result = helper(uniqueStr, size, currentSeq);
26+
currentSeq.deleteCharAt(currentSeq.length() - 1);
27+
28+
//Check if we can find an ans with "0"
29+
if (result != null) {
30+
return result;
31+
}
32+
//If appending "0" didn't work then try with "1"
33+
currentSeq.append("1");
34+
result = helper(uniqueStr, size, currentSeq);
35+
currentSeq.deleteCharAt(currentSeq.length() - 1);
36+
37+
return result;
38+
}
39+
}

0 commit comments

Comments
 (0)