class Solution {
* If k is the length of the String and there are n Strings
* Time Complexity = O(2^k)
* Space Complexity = n*k (for set) + k (for the current sequence)
*/
public String findDifferentBinaryString(String[] nums) {
Set<String> uniqueNums = Set.of(nums);
return helper(uniqueNums, uniqueNums.size(), new StringBuffer());
}
String helper(Set<String> uniqueStr, int size, StringBuffer currentSeq) {
if (currentSeq.length() == size) {
if (!uniqueStr.contains(currentSeq.toString())) {
return currentSeq.toString();
}
return null;
}
currentSeq.append("0");
String result = helper(uniqueStr, size, currentSeq);
currentSeq.deleteCharAt(currentSeq.length() - 1);
if (result != null) {
return result;
}
currentSeq.append("1");
result = helper(uniqueStr, size, currentSeq);
currentSeq.deleteCharAt(currentSeq.length() - 1);
return result;
}
}