1980. Find Unique Binary String LeetCode Solution

In this guide, you will get 1980. Find Unique Binary String LeetCode Solution with the best time and space complexity. The solution to Find Unique Binary String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Unique Binary String solution in C++
  4. Find Unique Binary String solution in Java
  5. Find Unique Binary String solution in Python
  6. Additional Resources
1980. Find Unique Binary String LeetCode Solution image

Problem Statement of Find Unique Binary String

Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.

Example 1:

Input: nums = [“01″,”10”]
Output: “11”
Explanation: “11” does not appear in nums. “00” would also be correct.

Example 2:

Input: nums = [“00″,”01”]
Output: “11”
Explanation: “11” does not appear in nums. “10” would also be correct.

Example 3:

Input: nums = [“111″,”011″,”001”]
Output: “101”
Explanation: “101” does not appear in nums. “000”, “010”, “100”, and “110” would also be correct.

Constraints:

n == nums.length
1 <= n <= 16
nums[i].length == n
nums[i] is either '0' or '1'.
All the strings of nums are unique.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1980. Find Unique Binary String LeetCode Solution in C++

class Solution {
 public:
  string findDifferentBinaryString(vector<string>& nums) {
    const int bitSize = nums[0].length();
    const int maxNum = 1 << bitSize;
    unordered_set<int> numsSet;

    for (const string& num : nums)
      numsSet.insert(stoi(num, nullptr, 2));

    for (int num = 0; num < maxNum; ++num)
      if (!numsSet.contains(num))
        return std::bitset<16>(num).to_string().substr(16 - bitSize);

    throw;
  }
};
/* code provided by PROGIEZ */

1980. Find Unique Binary String LeetCode Solution in Java

class Solution {
  public String findDifferentBinaryString(String[] nums) {
    final int bitSize = nums[0].length();
    final int maxNum = 1 << bitSize;
    Set<Integer> numsSet = Arrays.stream(nums)
                               .mapToInt(num -> Integer.parseInt(num, 2))
                               .boxed()
                               .collect(Collectors.toSet());

    for (int num = 0; num < maxNum; ++num)
      if (!numsSet.contains(num))
        return String.format("%" + bitSize + "s", Integer.toBinaryString(num)).replace(' ', '0');

    throw new IllegalArgumentException();
  }
}
// code provided by PROGIEZ

1980. Find Unique Binary String LeetCode Solution in Python

class Solution:
  def findDifferentBinaryString(self, nums: list[str]) -> str:
    bitSize = len(nums[0])
    maxNum = 1 << bitSize
    numsSet = {int(num, 2) for num in nums}

    for num in range(maxNum):
      if num not in numsSet:
        return f'{num:0>{bitSize}b}'
# code by PROGIEZ

Additional Resources

See also  2846. Minimum Edge Weight Equilibrium Queries in a Tree LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.