Skip to content

Commit 9f5ba3b

Browse files
committed
feat: add solutions to lc problem: No.1980
No.1980.Find Unique Binary String
1 parent eaa3afd commit 9f5ba3b

File tree

6 files changed

+298
-2
lines changed

6 files changed

+298
-2
lines changed

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

+105-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,126 @@
4848

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

51+
"1" 在长度为 n 的二进制字符串中出现的次数可为 0, 1, 2, ..., n (共有 n + 1 种可能)。
52+
53+
由于 nums 的长度为 n (n 种可能),因此我们一定可以找出一个新的二进制字符串,满足 "1" 在字符串中出现次数与 nums 中每个字符串不同。
54+
5155
<!-- tabs:start -->
5256

5357
### **Python3**
5458

5559
<!-- 这里可写当前语言的特殊实现逻辑 -->
5660

5761
```python
58-
62+
class Solution:
63+
def findDifferentBinaryString(self, nums: List[str]) -> str:
64+
s = set(num.count("1") for num in nums)
65+
n = len(nums)
66+
for i in range(n + 1):
67+
if i not in s:
68+
return "1" * i + "0" * (n - i)
69+
return ""
5970
```
6071

6172
### **Java**
6273

6374
<!-- 这里可写当前语言的特殊实现逻辑 -->
6475

6576
```java
77+
class Solution {
78+
public String findDifferentBinaryString(String[] nums) {
79+
Set<Integer> s = count(nums);
80+
int n = nums.length;
81+
for (int i = 0; i < n + 1; ++i) {
82+
if (!s.contains(i)) {
83+
return "1".repeat(i) + "0".repeat(n - i);
84+
}
85+
}
86+
return "";
87+
}
88+
89+
private Set<Integer> count(String[] nums) {
90+
Set<Integer> s = new HashSet<>();
91+
for (String num : nums) {
92+
int t = 0;
93+
for (char c : num.toCharArray()) {
94+
if (c == '1') {
95+
++t;
96+
}
97+
}
98+
s.add(t);
99+
}
100+
return s;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string findDifferentBinaryString(vector<string> &nums) {
111+
auto s = count(nums);
112+
for (int i = 0, n = nums.size(); i < n + 1; ++i)
113+
{
114+
if (!s.count(i))
115+
return repeat("1", i) + repeat("0", n - i);
116+
}
117+
return "";
118+
}
119+
120+
unordered_set<int> count(vector<string> &nums) {
121+
unordered_set<int> s;
122+
for (auto &num : nums)
123+
{
124+
int t = 0;
125+
for (char c : num)
126+
{
127+
if (c == '1')
128+
++t;
129+
}
130+
s.insert(t);
131+
}
132+
return s;
133+
}
134+
135+
string repeat(string s, int n) {
136+
string res = "";
137+
for (int i = 0; i < n; ++i)
138+
{
139+
res += s;
140+
}
141+
return res;
142+
}
143+
};
144+
```
66145
146+
### **Go**
147+
148+
```go
149+
func findDifferentBinaryString(nums []string) string {
150+
count := func() []bool {
151+
s := make([]bool, 17)
152+
for _, num := range nums {
153+
t := 0
154+
for _, c := range num {
155+
if c == '1' {
156+
t++
157+
}
158+
}
159+
s[t] = true
160+
}
161+
return s
162+
}
163+
s := count()
164+
for i, n := 0, len(nums); i <= n; i++ {
165+
if !s[i] {
166+
return strings.Repeat("1", i) + strings.Repeat("0", n-i)
167+
}
168+
}
169+
return ""
170+
}
67171
```
68172

69173
### **...**

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

+101-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,113 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def findDifferentBinaryString(self, nums: List[str]) -> str:
53+
s = set(num.count("1") for num in nums)
54+
n = len(nums)
55+
for i in range(n + 1):
56+
if i not in s:
57+
return "1" * i + "0" * (n - i)
58+
return ""
5259
```
5360

5461
### **Java**
5562

5663
```java
64+
class Solution {
65+
public String findDifferentBinaryString(String[] nums) {
66+
Set<Integer> s = count(nums);
67+
int n = nums.length;
68+
for (int i = 0; i < n + 1; ++i) {
69+
if (!s.contains(i)) {
70+
return "1".repeat(i) + "0".repeat(n - i);
71+
}
72+
}
73+
return "";
74+
}
75+
76+
private Set<Integer> count(String[] nums) {
77+
Set<Integer> s = new HashSet<>();
78+
for (String num : nums) {
79+
int t = 0;
80+
for (char c : num.toCharArray()) {
81+
if (c == '1') {
82+
++t;
83+
}
84+
}
85+
s.add(t);
86+
}
87+
return s;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
string findDifferentBinaryString(vector<string> &nums) {
98+
auto s = count(nums);
99+
for (int i = 0, n = nums.size(); i < n + 1; ++i)
100+
{
101+
if (!s.count(i))
102+
return repeat("1", i) + repeat("0", n - i);
103+
}
104+
return "";
105+
}
106+
107+
unordered_set<int> count(vector<string> &nums) {
108+
unordered_set<int> s;
109+
for (auto &num : nums)
110+
{
111+
int t = 0;
112+
for (char c : num)
113+
{
114+
if (c == '1')
115+
++t;
116+
}
117+
s.insert(t);
118+
}
119+
return s;
120+
}
121+
122+
string repeat(string s, int n) {
123+
string res = "";
124+
for (int i = 0; i < n; ++i)
125+
{
126+
res += s;
127+
}
128+
return res;
129+
}
130+
};
131+
```
57132
133+
### **Go**
134+
135+
```go
136+
func findDifferentBinaryString(nums []string) string {
137+
count := func() []bool {
138+
s := make([]bool, 17)
139+
for _, num := range nums {
140+
t := 0
141+
for _, c := range num {
142+
if c == '1' {
143+
t++
144+
}
145+
}
146+
s[t] = true
147+
}
148+
return s
149+
}
150+
s := count()
151+
for i, n := 0, len(nums); i <= n; i++ {
152+
if !s[i] {
153+
return strings.Repeat("1", i) + strings.Repeat("0", n-i)
154+
}
155+
}
156+
return ""
157+
}
58158
```
59159

60160
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string findDifferentBinaryString(vector<string> &nums) {
4+
auto s = count(nums);
5+
for (int i = 0, n = nums.size(); i < n + 1; ++i)
6+
{
7+
if (!s.count(i))
8+
return repeat("1", i) + repeat("0", n - i);
9+
}
10+
return "";
11+
}
12+
13+
unordered_set<int> count(vector<string> &nums) {
14+
unordered_set<int> s;
15+
for (auto &num : nums)
16+
{
17+
int t = 0;
18+
for (char c : num)
19+
{
20+
if (c == '1')
21+
++t;
22+
}
23+
s.insert(t);
24+
}
25+
return s;
26+
}
27+
28+
string repeat(string s, int n) {
29+
string res = "";
30+
for (int i = 0; i < n; ++i)
31+
{
32+
res += s;
33+
}
34+
return res;
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
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
14+
}
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)
19+
}
20+
}
21+
return ""
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
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);
8+
}
9+
}
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+
}
21+
}
22+
s.add(t);
23+
}
24+
return s;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findDifferentBinaryString(self, nums: List[str]) -> str:
3+
s = set(num.count("1") for num in nums)
4+
n = len(nums)
5+
for i in range(n + 1):
6+
if i not in s:
7+
return "1" * i + "0" * (n - i)
8+
return ""

0 commit comments

Comments
 (0)