Skip to content

Commit d7c1ca3

Browse files
committed
feat: solve No.1980
1 parent ceb98d2 commit d7c1ca3

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1980. Find Unique Binary String
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, String, Backtracking.
5+
- Similar Questions: Missing Number, Find All Numbers Disappeared in an Array, Random Pick with Blacklist.
6+
7+
## Problem
8+
9+
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**.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: nums = ["01","10"]
16+
Output: "11"
17+
Explanation: "11" does not appear in nums. "00" would also be correct.
18+
```
19+
20+
Example 2:
21+
22+
```
23+
Input: nums = ["00","01"]
24+
Output: "11"
25+
Explanation: "11" does not appear in nums. "10" would also be correct.
26+
```
27+
28+
Example 3:
29+
30+
```
31+
Input: nums = ["111","011","001"]
32+
Output: "101"
33+
Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct.
34+
```
35+
36+
 
37+
**Constraints:**
38+
39+
40+
41+
- `n == nums.length`
42+
43+
- `1 <= n <= 16`
44+
45+
- `nums[i].length == n`
46+
47+
- `nums[i] `is either `'0'` or `'1'`.
48+
49+
- All the strings of `nums` are **unique**.
50+
51+
52+
53+
## Solution
54+
55+
```javascript
56+
/**
57+
* @param {string[]} nums
58+
* @return {string}
59+
*/
60+
var findDifferentBinaryString = function(nums) {
61+
var str = '';
62+
for (var i = 0; i <= nums.length; i++) {
63+
str = i.toString(2);
64+
str = '0'.repeat(nums.length - str.length) + str;
65+
if (!nums.includes(str)) {
66+
return str;
67+
}
68+
}
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
Since array `nums` only contains `n` numbers, if we got `n + 1` numbers, there mush have at lease one number not in that array.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(n).
79+
* Space complexity : O(1).

0 commit comments

Comments
 (0)