-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathFindUniqueBinaryString.java
More file actions
47 lines (43 loc) · 1.18 KB
/
FindUniqueBinaryString.java
File metadata and controls
47 lines (43 loc) · 1.18 KB
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
//O(2^N)
class Solution {
StringBuilder res;
public String findDifferentBinaryString(String[] nums) {
int n = nums.length;
HashSet<String> set = new HashSet<>();
for(String num : nums){
set.add(num);
}
res=new StringBuilder("");
backtrack(nums,n,set,res);
return res.toString();
}
public boolean backtrack(String nums[], int n,HashSet<String> set, StringBuilder res){
//base case
if(res.length() == n){
if(!set.contains(res.toString())){
return true;
}
return false;
}
for(char ch='0';ch<='1';ch++){
res.append(ch);
if(backtrack(nums,n,set,res)){
return true;
}
res.deleteCharAt(res.length()-1);
}
return false;
}
}
//O(N)
class Solution {
public String findDifferentBinaryString(String[] nums) {
int n = nums.length;
StringBuilder res = new StringBuilder("");
for(int i=0;i<n;i++){
char ch = (nums[i].charAt(i)=='0'?'1':'0');
res.append(ch);
}
return res.toString();
}
}