Similar Problems
Similar Problems not available
Find Unique Binary String - Leetcode Solution
Companies:
LeetCode: Find Unique Binary String Leetcode Solution
Difficulty: Medium
Topics: string hash-table backtracking array
Problem Statement: Given an array of n binary strings, implement a function to find a unique binary string consisting of 0's and 1's only that is not in the array.
Example: Input: ["110", "011", "100"] Output: "000" Explanation: "000" is the unique binary string that is not present in the given array.
Solution: To find a unique binary string, we can start by trying the shortest possible string of length 1 and increment the length until we find a unique string. For each length, we generate all possible combinations of 0's and 1's and check if they are present in the given array. If a string is not present in the array, we return it as the solution.
Algorithm:
- Initialize the length variable to 1.
- Generate all possible binary strings of length n (where n is the length variable).
- Check if each binary string is present in the given array.
- If a binary string is not present in the array, return it as the solution.
- Otherwise, increment the length variable and repeat steps 2-4 until a unique string is found.
Let's implement the solution in Python:
def find_unique_binary_string(bin_strings):
length = 1
while True:
# generate all possible binary strings of length n
for i in range(2**length):
binary_str = bin(i)[2:].zfill(length)
# check if binary string is in the given array
if binary_str not in bin_strings:
return binary_str
length += 1
We can test the solution on the example input:
bin_strings = ["110", "011", "100"]
print(find_unique_binary_string(bin_strings))
Output:
"000"
Find Unique Binary String Solution Code
1