Skip to content

Commit f2ca206

Browse files
committed
feat: add solutions to lc problem: No.1980
No.1980.Find Unique Binary String
1 parent 67fdde9 commit f2ca206

File tree

6 files changed

+92
-188
lines changed

6 files changed

+92
-188
lines changed

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

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
"1" 在长度为 n 的二进制字符串中出现的次数可为 0, 1, 2, ..., n (共有 n + 1 种可能)。
52+
**方法一:计数 + 枚举**
5353

54-
由于 nums 的长度为 n (n 种可能),因此我们一定可以找出一个新的二进制字符串,满足 "1" 在字符串中出现次数与 nums 中每个字符串不同。
54+
由于 `'1'` 在长度为 $n$ 的二进制字符串中出现的次数可以为 $0, 1, 2, \cdots, n$(共有 $n + 1$ 种可能),因此我们一定可以找出一个新的二进制字符串,满足 `'1'` 在字符串中出现次数与 `nums` 中每个字符串不同。
55+
56+
我们可以用一个整数 $mask$ 记录所有字符串中 `'1'` 出现次数的情况,即 $mask$ 的第 $i$ 位为 $1$ 表示长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串存在,否则不存在。
57+
58+
然后我们从 $0$ 开始枚举长度为 $n$ 的二进制字符串中 `'1'` 出现的次数 $i$,如果 $mask$ 的第 $i$ 位为 $0$,则说明长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串不存在,我们可以将这个字符串作为答案返回。
59+
60+
时间复杂度 $O(L)$,空间复杂度 $O(1)$。其中 $L$ 为 `nums` 中字符串的总长度。
5561

5662
<!-- tabs:start -->
5763

@@ -62,12 +68,13 @@
6268
```python
6369
class Solution:
6470
def findDifferentBinaryString(self, nums: List[str]) -> str:
65-
s = set(num.count("1") for num in nums)
71+
mask = 0
72+
for x in nums:
73+
mask |= 1 << x.count("1")
6674
n = len(nums)
6775
for i in range(n + 1):
68-
if i not in s:
76+
if mask >> i & 1 ^ 1:
6977
return "1" * i + "0" * (n - i)
70-
return ""
7178
```
7279

7380
### **Java**
@@ -77,28 +84,21 @@ class Solution:
7784
```java
7885
class Solution {
7986
public String findDifferentBinaryString(String[] nums) {
80-
Set<Integer> s = count(nums);
81-
int n = nums.length;
82-
for (int i = 0; i < n + 1; ++i) {
83-
if (!s.contains(i)) {
84-
return "1".repeat(i) + "0".repeat(n - i);
87+
int mask = 0;
88+
for (var x : nums) {
89+
int cnt = 0;
90+
for (int i = 0; i < x.length(); ++i) {
91+
if (x.charAt(i) == '1') {
92+
++cnt;
93+
}
8594
}
95+
mask |= 1 << cnt;
8696
}
87-
return "";
88-
}
89-
90-
private Set<Integer> count(String[] nums) {
91-
Set<Integer> s = new HashSet<>();
92-
for (String num : nums) {
93-
int t = 0;
94-
for (char c : num.toCharArray()) {
95-
if (c == '1') {
96-
++t;
97-
}
97+
for (int i = 0; ; ++i) {
98+
if ((mask >> i & 1) == 0) {
99+
return "1".repeat(i) + "0".repeat(nums.length - i);
98100
}
99-
s.add(t);
100101
}
101-
return s;
102102
}
103103
}
104104
```
@@ -109,33 +109,16 @@ class Solution {
109109
class Solution {
110110
public:
111111
string findDifferentBinaryString(vector<string>& nums) {
112-
auto s = count(nums);
113-
for (int i = 0, n = nums.size(); i < n + 1; ++i) {
114-
if (!s.count(i))
115-
return repeat("1", i) + repeat("0", n - i);
112+
int mask = 0;
113+
for (auto& x : nums) {
114+
int cnt = count(x.begin(), x.end(), '1');
115+
mask |= 1 << cnt;
116116
}
117-
return "";
118-
}
119-
120-
unordered_set<int> count(vector<string>& nums) {
121-
unordered_set<int> s;
122-
for (auto& num : nums) {
123-
int t = 0;
124-
for (char c : num) {
125-
if (c == '1')
126-
++t;
117+
for (int i = 0; ; ++i) {
118+
if (mask >> i & 1 ^ 1) {
119+
return string(i, '1') + string(nums.size() - i, '0');
127120
}
128-
s.insert(t);
129-
}
130-
return s;
131-
}
132-
133-
string repeat(string s, int n) {
134-
string res = "";
135-
for (int i = 0; i < n; ++i) {
136-
res += s;
137121
}
138-
return res;
139122
}
140123
};
141124
```
@@ -144,26 +127,15 @@ public:
144127
145128
```go
146129
func findDifferentBinaryString(nums []string) string {
147-
count := func() []bool {
148-
s := make([]bool, 17)
149-
for _, num := range nums {
150-
t := 0
151-
for _, c := range num {
152-
if c == '1' {
153-
t++
154-
}
155-
}
156-
s[t] = true
157-
}
158-
return s
130+
mask := 0
131+
for _, x := range nums {
132+
mask |= 1 << strings.Count(x, "1")
159133
}
160-
s := count()
161-
for i, n := 0, len(nums); i <= n; i++ {
162-
if !s[i] {
163-
return strings.Repeat("1", i) + strings.Repeat("0", n-i)
134+
for i := 0; ; i++ {
135+
if mask>>i&1 == 0 {
136+
return strings.Repeat("1", i) + strings.Repeat("0", len(nums)-i)
164137
}
165138
}
166-
return ""
167139
}
168140
```
169141

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

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,35 @@
5151
```python
5252
class Solution:
5353
def findDifferentBinaryString(self, nums: List[str]) -> str:
54-
s = set(num.count("1") for num in nums)
54+
mask = 0
55+
for x in nums:
56+
mask |= 1 << x.count("1")
5557
n = len(nums)
5658
for i in range(n + 1):
57-
if i not in s:
59+
if mask >> i & 1 ^ 1:
5860
return "1" * i + "0" * (n - i)
59-
return ""
6061
```
6162

6263
### **Java**
6364

6465
```java
6566
class Solution {
6667
public String findDifferentBinaryString(String[] nums) {
67-
Set<Integer> s = count(nums);
68-
int n = nums.length;
69-
for (int i = 0; i < n + 1; ++i) {
70-
if (!s.contains(i)) {
71-
return "1".repeat(i) + "0".repeat(n - i);
68+
int mask = 0;
69+
for (var x : nums) {
70+
int cnt = 0;
71+
for (int i = 0; i < x.length(); ++i) {
72+
if (x.charAt(i) == '1') {
73+
++cnt;
74+
}
7275
}
76+
mask |= 1 << cnt;
7377
}
74-
return "";
75-
}
76-
77-
private Set<Integer> count(String[] nums) {
78-
Set<Integer> s = new HashSet<>();
79-
for (String num : nums) {
80-
int t = 0;
81-
for (char c : num.toCharArray()) {
82-
if (c == '1') {
83-
++t;
84-
}
78+
for (int i = 0; ; ++i) {
79+
if ((mask >> i & 1) == 0) {
80+
return "1".repeat(i) + "0".repeat(nums.length - i);
8581
}
86-
s.add(t);
8782
}
88-
return s;
8983
}
9084
}
9185
```
@@ -96,33 +90,16 @@ class Solution {
9690
class Solution {
9791
public:
9892
string findDifferentBinaryString(vector<string>& nums) {
99-
auto s = count(nums);
100-
for (int i = 0, n = nums.size(); i < n + 1; ++i) {
101-
if (!s.count(i))
102-
return repeat("1", i) + repeat("0", n - i);
93+
int mask = 0;
94+
for (auto& x : nums) {
95+
int cnt = count(x.begin(), x.end(), '1');
96+
mask |= 1 << cnt;
10397
}
104-
return "";
105-
}
106-
107-
unordered_set<int> count(vector<string>& nums) {
108-
unordered_set<int> s;
109-
for (auto& num : nums) {
110-
int t = 0;
111-
for (char c : num) {
112-
if (c == '1')
113-
++t;
98+
for (int i = 0; ; ++i) {
99+
if (mask >> i & 1 ^ 1) {
100+
return string(i, '1') + string(nums.size() - i, '0');
114101
}
115-
s.insert(t);
116102
}
117-
return s;
118-
}
119-
120-
string repeat(string s, int n) {
121-
string res = "";
122-
for (int i = 0; i < n; ++i) {
123-
res += s;
124-
}
125-
return res;
126103
}
127104
};
128105
```
@@ -131,26 +108,15 @@ public:
131108
132109
```go
133110
func findDifferentBinaryString(nums []string) string {
134-
count := func() []bool {
135-
s := make([]bool, 17)
136-
for _, num := range nums {
137-
t := 0
138-
for _, c := range num {
139-
if c == '1' {
140-
t++
141-
}
142-
}
143-
s[t] = true
144-
}
145-
return s
111+
mask := 0
112+
for _, x := range nums {
113+
mask |= 1 << strings.Count(x, "1")
146114
}
147-
s := count()
148-
for i, n := 0, len(nums); i <= n; i++ {
149-
if !s[i] {
150-
return strings.Repeat("1", i) + strings.Repeat("0", n-i)
115+
for i := 0; ; i++ {
116+
if mask>>i&1 == 0 {
117+
return strings.Repeat("1", i) + strings.Repeat("0", len(nums)-i)
151118
}
152119
}
153-
return ""
154120
}
155121
```
156122

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
class Solution {
22
public:
33
string findDifferentBinaryString(vector<string>& nums) {
4-
auto s = count(nums);
5-
for (int i = 0, n = nums.size(); i < n + 1; ++i) {
6-
if (!s.count(i))
7-
return repeat("1", i) + repeat("0", n - i);
4+
int mask = 0;
5+
for (auto& x : nums) {
6+
int cnt = count(x.begin(), x.end(), '1');
7+
mask |= 1 << cnt;
88
}
9-
return "";
10-
}
11-
12-
unordered_set<int> count(vector<string>& nums) {
13-
unordered_set<int> s;
14-
for (auto& num : nums) {
15-
int t = 0;
16-
for (char c : num) {
17-
if (c == '1')
18-
++t;
9+
for (int i = 0; ; ++i) {
10+
if (mask >> i & 1 ^ 1) {
11+
return string(i, '1') + string(nums.size() - i, '0');
1912
}
20-
s.insert(t);
21-
}
22-
return s;
23-
}
24-
25-
string repeat(string s, int n) {
26-
string res = "";
27-
for (int i = 0; i < n; ++i) {
28-
res += s;
2913
}
30-
return res;
3114
}
3215
};
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
func findDifferentBinaryString(nums []string) string {
2-
count := func() []bool {
3-
s := make([]bool, 17)
4-
for _, num := range nums {
5-
t := 0
6-
for _, c := range num {
7-
if c == '1' {
8-
t++
9-
}
10-
}
11-
s[t] = true
12-
}
13-
return s
2+
mask := 0
3+
for _, x := range nums {
4+
mask |= 1 << strings.Count(x, "1")
145
}
15-
s := count()
16-
for i, n := 0, len(nums); i <= n; i++ {
17-
if !s[i] {
18-
return strings.Repeat("1", i) + strings.Repeat("0", n-i)
6+
for i := 0; ; i++ {
7+
if mask>>i&1 == 0 {
8+
return strings.Repeat("1", i) + strings.Repeat("0", len(nums)-i)
199
}
2010
}
21-
return ""
2211
}
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
class Solution {
22
public String findDifferentBinaryString(String[] nums) {
3-
Set<Integer> s = count(nums);
4-
int n = nums.length;
5-
for (int i = 0; i < n + 1; ++i) {
6-
if (!s.contains(i)) {
7-
return "1".repeat(i) + "0".repeat(n - i);
3+
int mask = 0;
4+
for (var x : nums) {
5+
int cnt = 0;
6+
for (int i = 0; i < x.length(); ++i) {
7+
if (x.charAt(i) == '1') {
8+
++cnt;
9+
}
810
}
11+
mask |= 1 << cnt;
912
}
10-
return "";
11-
}
12-
13-
private Set<Integer> count(String[] nums) {
14-
Set<Integer> s = new HashSet<>();
15-
for (String num : nums) {
16-
int t = 0;
17-
for (char c : num.toCharArray()) {
18-
if (c == '1') {
19-
++t;
20-
}
13+
for (int i = 0; ; ++i) {
14+
if ((mask >> i & 1) == 0) {
15+
return "1".repeat(i) + "0".repeat(nums.length - i);
2116
}
22-
s.add(t);
2317
}
24-
return s;
2518
}
2619
}

0 commit comments

Comments
 (0)