Find Unique Binary String — LeetCode 1980 Python Solution
- Problem
- #1980
- Pattern
- Backtracking
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- Input
- nums = ["01","10"]
- Output
- "11"
- Explanation
- "11" does not appear in nums. "00" would also be correct.
Python solution
class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
mask = 0
for x in nums:
mask |= 1 << x.count("1")
n = len(nums)
for i in range(n + 1):
if mask >> i & 1 ^ 1:
return "1" * i + "0" * (n - i)Complexity
| Measure | Complexity |
|---|---|
| Time | O(L), where L is the total length of the strings in `nums` |
| Space | O(1) auxiliary |
Pattern: Backtracking
Build candidates one choice at a time and abandon a branch the moment it cannot work. LeetCode 1980. Find Unique Binary String is filed here because LeetCode tags it Backtracking, which is the vocabulary this hub collects.
The backtracking guide has the Python template for the pattern and the 105 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1980. Find Unique Binary String?
- LeetCode 1980. Find Unique Binary String is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1980. Find Unique Binary String?
- The Python solution on this page runs in O(L), where L is the total length of the strings in `nums`.
- What is the space complexity of LeetCode 1980. Find Unique Binary String?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1980. Find Unique Binary String cover?
- LeetCode 1980. Find Unique Binary String is tagged Array, Hash Table, String and Backtracking on LeetCode.