Skip to content

Commit d881de6

Browse files
authored
feat: add solutions for lc No.1980 (#5053)
1 parent cbdf232 commit d881de6

13 files changed

Lines changed: 294 additions & 137 deletions

File tree

solution/1900-1999/1980.Find Unique Binary String/README.md

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ tags:
6868

6969
### 方法一:计数 + 枚举
7070

71-
由于 `'1'` 在长度为 $n$ 的二进制字符串中出现的次数可以为 $0, 1, 2, \cdots, n$(共有 $n + 1$ 种可能),因此我们一定可以找出一个新的二进制字符串,满足 `'1'` 在字符串中出现次数与 `nums` 中每个字符串不同。
71+
由于 `'1'` 在长度为 $n$ 的二进制字符串中出现的次数可以为 $0, 1, 2, \cdots, n$(共有 $n + 1$ 种可能),因此我们一定可以找出一个新的二进制字符串,满足 `'1'` 在字符串中出现次数与 $\textit{nums}$ 中每个字符串不同。
7272

73-
我们可以用一个整数 $mask$ 记录所有字符串中 `'1'` 出现次数的情况,即 $mask$ 的第 $i$ 位为 $1$ 表示长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串存在,否则不存在。
73+
我们可以用一个整数 $\textit{mask}$ 记录所有字符串中 `'1'` 出现次数的情况,即 $\textit{mask}$ 的第 $i$ 位为 $1$ 表示长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串存在,否则不存在。
7474

75-
然后我们从 $0$ 开始枚举长度为 $n$ 的二进制字符串中 `'1'` 出现的次数 $i$,如果 $mask$ 的第 $i$ 位为 $0$,则说明长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串不存在,我们可以将这个字符串作为答案返回。
75+
然后我们从 $0$ 开始枚举长度为 $n$ 的二进制字符串中 `'1'` 出现的次数 $i$,如果 $\textit{mask}$ 的第 $i$ 位为 $0$,则说明长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串不存在,我们可以将这个字符串作为答案返回。
7676

77-
时间复杂度 $O(L)$,其中 $L$ 为 `nums` 中字符串的总长度。空间复杂度 $O(1)$。
77+
时间复杂度 $O(L)$,其中 $L$ 为 $\textit{nums}$ 中字符串的总长度。空间复杂度 $O(1)$。
7878

7979
<!-- tabs:start -->
8080

@@ -86,10 +86,9 @@ class Solution:
8686
mask = 0
8787
for x in nums:
8888
mask |= 1 << x.count("1")
89-
n = len(nums)
90-
for i in range(n + 1):
89+
for i in count(0):
9190
if mask >> i & 1 ^ 1:
92-
return "1" * i + "0" * (n - i)
91+
return "1" * i + "0" * (len(nums) - i)
9392
```
9493

9594
#### Java
@@ -169,6 +168,27 @@ function findDifferentBinaryString(nums: string[]): string {
169168
}
170169
```
171170

171+
#### JavaScript
172+
173+
```js
174+
/**
175+
* @param {string[]} nums
176+
* @return {string}
177+
*/
178+
var findDifferentBinaryString = function (nums) {
179+
let mask = 0;
180+
for (let x of nums) {
181+
const cnt = x.split('').filter(c => c === '1').length;
182+
mask |= 1 << cnt;
183+
}
184+
for (let i = 0; ; ++i) {
185+
if (((mask >> i) & 1) === 0) {
186+
return '1'.repeat(i) + '0'.repeat(nums.length - i);
187+
}
188+
}
189+
};
190+
```
191+
172192
#### C#
173193

174194
```cs
@@ -194,77 +214,114 @@ public class Solution {
194214

195215
<!-- solution:start -->
196216

197-
### 方法二
217+
### 方法二:构造
198218

199-
<!-- tabs:start -->
219+
我们可以构造一个长度为 $n$ 的二进制字符串 $\textit{ans}$,其中 $\textit{ans}$ 的第 $i$ 位与 $\textit{nums}[i]$ 的第 $i$ 位不同。由于 $\textit{nums}$ 中的字符串互不相同,因此 $\textit{ans}$ 不会出现在 $\textit{nums}$ 中。
200220

201-
#### TypeScript
221+
时间复杂度 $O(n)$,其中 $n$ 是 $\textit{nums}$ 中字符串的长度。忽略答案字符串的空间复杂度,空间复杂度 $O(1)$。
202222

203-
```ts
204-
function findDifferentBinaryString(nums: string[]): string {
205-
const set = new Set(nums.map(x => Number.parseInt(x, 2)));
206-
let res = 0;
223+
<!-- tabs:start -->
207224

208-
while (set.has(res)) {
209-
res++;
210-
}
225+
#### Python3
211226

212-
return res.toString(2).padStart(nums[0].length, '0');
213-
}
227+
```python
228+
class Solution:
229+
def findDifferentBinaryString(self, nums: List[str]) -> str:
230+
ans = [None] * len(nums)
231+
for i, s in enumerate(nums):
232+
ans[i] = "1" if s[i] == "0" else "0"
233+
return "".join(ans)
214234
```
215235

216-
#### JavaScript
217-
218-
```js
219-
function findDifferentBinaryString(nums) {
220-
const set = new Set(nums.map(x => Number.parseInt(x, 2)));
221-
let res = 0;
236+
#### Java
222237

223-
while (set.has(res)) {
224-
res++;
238+
```java
239+
class Solution {
240+
public String findDifferentBinaryString(String[] nums) {
241+
int n = nums.length;
242+
char[] ans = new char[n];
243+
for (int i = 0; i < n; i++) {
244+
ans[i] = nums[i].charAt(i) == '0' ? '1' : '0';
245+
}
246+
return new String(ans);
225247
}
226-
227-
return res.toString(2).padStart(nums[0].length, '0');
228248
}
229249
```
230250

231-
<!-- solution:end -->
232-
233-
<!-- tabs:end -->
251+
#### C++
234252

235-
<!-- solution:start -->
253+
```cpp
254+
class Solution {
255+
public:
256+
string findDifferentBinaryString(vector<string>& nums) {
257+
int n = nums.size();
258+
string ans(n, '0');
259+
for (int i = 0; i < n; i++) {
260+
ans[i] = nums[i][i] == '0' ? '1' : '0';
261+
}
262+
return ans;
263+
}
264+
};
265+
```
236266
237-
### 方法三
267+
#### Go
238268
239-
<!-- tabs:start -->
269+
```go
270+
func findDifferentBinaryString(nums []string) string {
271+
ans := make([]byte, len(nums))
272+
for i, s := range nums {
273+
if s[i] == '0' {
274+
ans[i] = '1'
275+
} else {
276+
ans[i] = '0'
277+
}
278+
}
279+
return string(ans)
280+
}
281+
```
240282

241283
#### TypeScript
242284

243285
```ts
244286
function findDifferentBinaryString(nums: string[]): string {
245-
const res: string[] = [];
246-
247-
for (let i = 0; i < nums.length; i++) {
248-
const x = nums[i][i];
249-
res.push(x === '0' ? '1' : '0');
287+
const n = nums.length;
288+
const ans: string[] = new Array(n);
289+
for (let i = 0; i < n; i++) {
290+
ans[i] = nums[i][i] === '0' ? '1' : '0';
250291
}
251-
252-
return res.join('');
292+
return ans.join('');
253293
}
254294
```
255295

256296
#### JavaScript
257297

258298
```js
259-
function findDifferentBinaryString(nums) {
260-
const res = [];
261-
262-
for (let i = 0; i < nums.length; i++) {
263-
const x = nums[i][i];
264-
res.push(x === '0' ? '1' : '0');
299+
/**
300+
* @param {string[]} nums
301+
* @return {string}
302+
*/
303+
var findDifferentBinaryString = function (nums) {
304+
const n = nums.length;
305+
const ans = new Array(n);
306+
for (let i = 0; i < n; i++) {
307+
ans[i] = nums[i][i] === '0' ? '1' : '0';
265308
}
309+
return ans.join('');
310+
};
311+
```
312+
313+
#### C#
266314

267-
return res.join('');
315+
```cs
316+
public class Solution {
317+
public string FindDifferentBinaryString(string[] nums) {
318+
int n = nums.Length;
319+
char[] ans = new char[n];
320+
for (int i = 0; i < n; i++) {
321+
ans[i] = nums[i][i] == '0' ? '1' : '0';
322+
}
323+
return new string(ans);
324+
}
268325
}
269326
```
270327

0 commit comments

Comments
 (0)