Skip to content

Commit d2dbc7b

Browse files
Merge pull request #219 from satyam878/satyam1
Unique Binary String
2 parents 83ff911 + 45673cb commit d2dbc7b

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

LeetCode/uniqueBString.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Given an array of strings nums containing n unique binary strings each of length n,
2+
return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.
3+
https://leetcode.com/problems/find-unique-binary-string/*/
4+
class Solution {
5+
public String findDifferentBinaryString(String[] nums) {
6+
7+
int len=nums[0].length();
8+
String ans="",ans1="";
9+
for(int i=0;i<=Math.pow(2,len)-1;i++)
10+
{
11+
ans=Integer.toBinaryString(i); //converting to binary sting equivalent to number
12+
int l=ans.length();
13+
int a=len-l;
14+
if(a>0)
15+
ans1=String.format( "%0" +a+ "d%s" , 0, ans); //adding zeros before the conferted string
16+
else
17+
ans1=ans;
18+
int cnt=0;
19+
for(int j=0;j<len;j++)
20+
{
21+
if(!nums[j].equals(ans1)) //checking if the string is present in array or not
22+
cnt++;
23+
}
24+
if(cnt==len) //checking condition and returning the answer
25+
return ans1;
26+
}
27+
28+
return ans1;
29+
30+
}
31+
}

0 commit comments

Comments
 (0)