Leetcode 1980: Find Unique Binary String

grid47
grid47
Exploring patterns and algorithms
Apr 23, 2024 4 min read

You are given an array of unique binary strings, each of length n. Your task is to find and return a binary string of length n that is not present in the given array. There can be multiple valid solutions.
Problem
Approach
Steps
Complexity
Input: You are given an integer n representing the number of binary strings in the array nums, and a list of strings nums where each string is a binary number of length n.
Example: nums = ['00', '11']
Constraints:
• 1 <= n <= 16
• nums.length == n
• nums[i].length == n
• All strings in nums are unique binary strings.
Output: Return a binary string of length n that is not present in the array nums. If there are multiple valid answers, any valid one can be returned.
Example: Output: '10'
Constraints:
• The returned string must not be in the input array.
Goal: The goal is to identify a binary string that does not match any of the given strings in nums. The approach relies on constructing a string that is guaranteed to differ from each input string at least in one position.
Steps:
• Create an empty result string.
• Iterate through the array nums and for each index i, pick the opposite of the character at index i of the i-th string in nums.
• Return the constructed binary string as the result.
Goal: Ensure the solution is efficient even for the largest inputs.
Steps:
• The maximum number of binary strings is 16, and the maximum length of each string is also 16.
Assumptions:
• The binary strings in nums are unique and of the same length.
Input: Input: nums = ['01', '10']
Explanation: In this case, the binary strings in nums are '01' and '10'. By flipping each corresponding bit, we can create '11', which does not appear in the input array.

Input: Input: nums = ['11', '00']
Explanation: The binary strings in nums are '11' and '00'. By flipping each bit in the corresponding positions, we get '00', which is already in the list. Hence, '10' or '01' can be a valid output.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus