Skip to content

Commit 204e0bf

Browse files
authored
feat: add solutions to lc problem: No.1702 (#2565)
No.1702.Maximum Binary String After Change
1 parent 594929f commit 204e0bf

File tree

6 files changed

+133
-9
lines changed

6 files changed

+133
-9
lines changed

solution/1700-1799/1702.Maximum Binary String After Change/README.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@
6363

6464
### 方法一:脑筋急转弯
6565

66-
我们观察发现,操作 2 可以把所有的 $1$ 都移动到字符串的末尾,而操作 1 可以把所有的 `0000..000` 串变为 `111..110`
66+
我们观察发现,操作 $2$ 可以把所有的 $1$ 都移动到字符串的末尾,而操作 $1$ 可以把所有的 `0000..000` 串变为 `111..110`
6767

68-
因此,要想得到最大的二进制串,我们应该把所有不在开头的 $1$ 移动到字符串末尾,使得字符串变为 `111..11...00..11` 的形式,然后借助操作 1 把中间的 `000..00` 变为 `111..10`。这样我们最终可以得到一个最多包含一个 $0$ 的二进制字符串,这个字符串就是我们要求的最大二进制串。
68+
因此,要想得到最大的二进制串,我们应该把所有不在开头的 $1$ 移动到字符串末尾,使得字符串变为 `111..11...000..00..11` 的形式,然后借助操作 $1$ 把中间的 `000..00` 变为 `111..10`。这样我们最终可以得到一个最多包含一个 $0$ 的二进制字符串,这个字符串就是我们要求的最大二进制串。
6969

70-
时间复杂度 $O(n)$,其中 $n$ 为字符串 `binary` 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
70+
在代码实现上,我们首先判断字符串是否包含 $0$,如果不包含,直接返回原字符串。否则,我们找到第一个 $0$ 的位置 $k$,加上该位置之后的 $0$ 的个数,得到的就是修改后的字符串的 $0$ 所在的位置,其余位置都是 $1$。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串的长度。
7173

7274
<!-- tabs:start -->
7375

@@ -107,7 +109,9 @@ class Solution {
107109
public:
108110
string maximumBinaryString(string binary) {
109111
int k = binary.find('0');
110-
if (k == binary.npos) return binary;
112+
if (k == binary.npos) {
113+
return binary;
114+
}
111115
int n = binary.size();
112116
for (int i = k + 1; i < n; ++i) {
113117
if (binary[i] == '0') {
@@ -139,6 +143,47 @@ func maximumBinaryString(binary string) string {
139143
}
140144
```
141145

146+
```ts
147+
function maximumBinaryString(binary: string): string {
148+
let k = binary.indexOf('0');
149+
if (k === -1) {
150+
return binary;
151+
}
152+
k += binary.slice(k + 1).split('0').length - 1;
153+
return '1'.repeat(k) + '0' + '1'.repeat(binary.length - k - 1);
154+
}
155+
```
156+
157+
```rust
158+
impl Solution {
159+
pub fn maximum_binary_string(binary: String) -> String {
160+
if let Some(k) = binary.find('0') {
161+
let k =
162+
k +
163+
binary[k + 1..]
164+
.chars()
165+
.filter(|&c| c == '0')
166+
.count();
167+
return format!("{}{}{}", "1".repeat(k), "0", "1".repeat(binary.len() - k - 1));
168+
}
169+
binary
170+
}
171+
}
172+
```
173+
174+
```cs
175+
public class Solution {
176+
public string MaximumBinaryString(string binary) {
177+
int k = binary.IndexOf('0');
178+
if (k == -1) {
179+
return binary;
180+
}
181+
k += binary.Substring(k + 1).Count(c => c == '0');
182+
return new string('1', k) + '0' + new string('1', binary.Length - k - 1);
183+
}
184+
}
185+
```
186+
142187
<!-- tabs:end -->
143188

144189
<!-- end -->

solution/1700-1799/1702.Maximum Binary String After Change/README_EN.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@
5959

6060
### Solution 1: Quick Thinking
6161

62-
We observe that operation 2 can move all $1$s to the end of the string, and operation 1 can change all `0000..000` strings to `111..110`.
62+
We observe that operation $2$ can move all $1$s to the end of the string, and operation $1$ can change all `0000..000` strings to `111..110`.
6363

64-
Therefore, to get the maximum binary string, we should move all $1$s that are not at the beginning to the end of the string, making the string in the form of `111..11...00..11`, and then use operation 1 to change the middle `000..00` to `111..10`. In this way, we can finally get a binary string that contains at most one $0$, which is the maximum binary string we are looking for.
64+
Therefore, to get the maximum binary string, we should move all $1$s that are not at the beginning to the end of the string, making the string in the form of `111..11...000..00..11`. Then, with the help of operation $1$, we change the middle `000..00` to `111..10`. In this way, we can finally get a binary string that contains at most one $0$, which is the maximum binary string we are looking for.
6565

66-
The time complexity is $O(n)$, where $n$ is the length of the string `binary`. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
66+
In the code implementation, we first judge whether the string contains $0$. If it does not, we directly return the original string. Otherwise, we find the position $k$ of the first $0$, add the number of $0$s after this position, and the position of $0$ in the modified string is obtained. The rest of the positions are all $1$s.
67+
68+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.
6769

6870
<!-- tabs:start -->
6971

@@ -103,7 +105,9 @@ class Solution {
103105
public:
104106
string maximumBinaryString(string binary) {
105107
int k = binary.find('0');
106-
if (k == binary.npos) return binary;
108+
if (k == binary.npos) {
109+
return binary;
110+
}
107111
int n = binary.size();
108112
for (int i = k + 1; i < n; ++i) {
109113
if (binary[i] == '0') {
@@ -135,6 +139,47 @@ func maximumBinaryString(binary string) string {
135139
}
136140
```
137141

142+
```ts
143+
function maximumBinaryString(binary: string): string {
144+
let k = binary.indexOf('0');
145+
if (k === -1) {
146+
return binary;
147+
}
148+
k += binary.slice(k + 1).split('0').length - 1;
149+
return '1'.repeat(k) + '0' + '1'.repeat(binary.length - k - 1);
150+
}
151+
```
152+
153+
```rust
154+
impl Solution {
155+
pub fn maximum_binary_string(binary: String) -> String {
156+
if let Some(k) = binary.find('0') {
157+
let k =
158+
k +
159+
binary[k + 1..]
160+
.chars()
161+
.filter(|&c| c == '0')
162+
.count();
163+
return format!("{}{}{}", "1".repeat(k), "0", "1".repeat(binary.len() - k - 1));
164+
}
165+
binary
166+
}
167+
}
168+
```
169+
170+
```cs
171+
public class Solution {
172+
public string MaximumBinaryString(string binary) {
173+
int k = binary.IndexOf('0');
174+
if (k == -1) {
175+
return binary;
176+
}
177+
k += binary.Substring(k + 1).Count(c => c == '0');
178+
return new string('1', k) + '0' + new string('1', binary.Length - k - 1);
179+
}
180+
}
181+
```
182+
138183
<!-- tabs:end -->
139184

140185
<!-- end -->

solution/1700-1799/1702.Maximum Binary String After Change/Solution.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
string maximumBinaryString(string binary) {
44
int k = binary.find('0');
5-
if (k == binary.npos) return binary;
5+
if (k == binary.npos) {
6+
return binary;
7+
}
68
int n = binary.size();
79
for (int i = k + 1; i < n; ++i) {
810
if (binary[i] == '0') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Solution {
2+
public string MaximumBinaryString(string binary) {
3+
int k = binary.IndexOf('0');
4+
if (k == -1) {
5+
return binary;
6+
}
7+
k += binary.Substring(k + 1).Count(c => c == '0');
8+
return new string('1', k) + '0' + new string('1', binary.Length - k - 1);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn maximum_binary_string(binary: String) -> String {
3+
if let Some(k) = binary.find('0') {
4+
let k =
5+
k +
6+
binary[k + 1..]
7+
.chars()
8+
.filter(|&c| c == '0')
9+
.count();
10+
return format!("{}{}{}", "1".repeat(k), "0", "1".repeat(binary.len() - k - 1));
11+
}
12+
binary
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function maximumBinaryString(binary: string): string {
2+
let k = binary.indexOf('0');
3+
if (k === -1) {
4+
return binary;
5+
}
6+
k += binary.slice(k + 1).split('0').length - 1;
7+
return '1'.repeat(k) + '0' + '1'.repeat(binary.length - k - 1);
8+
}

0 commit comments

Comments
 (0)