Finding a Unique Binary String in Python

IntroductionImagine you are given a list of binary strings, and your task is to find a new binary string that is not present in the list. This might seem tricky at first, but Python makes it easy to solve. In this guide, we'll break down the problem step by step and understand how to implement a simple and efficient solution.

Understanding the ProblemYou are given a list of n binary strings, each of length n. Your goal is to find a new n-bit binary string that is not present in the list.

Example:nums = ["00", "01", "10"]

Output: "11" (since "11" is not in the list)

There can be multiple valid answers. The key is to ensure that the generated binary string is unique.

here is my approach to solve 

from typing import List

class Solution:
    def findDifferentBinaryString(self, nums: List[str]) -> str:
        n = len(nums)
        num_set = set(nums)

        for i in range(2**n):
            binary_str = format(i, f'0{n}b')
            if binary_str not in num_set:
                return binary_str

        return ""



Comments

Popular posts from this blog

Mastering the Difference Array Technique: A Simple Guide with Examples

Leetcode 2594. Minimum Time to Repair Cars explained clearly