|
59 | 59 |
|
60 | 60 | ### Solution 1: Quick Thinking
|
61 | 61 |
|
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`. |
63 | 63 |
|
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. |
65 | 65 |
|
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. |
67 | 69 |
|
68 | 70 | <!-- tabs:start -->
|
69 | 71 |
|
@@ -103,7 +105,9 @@ class Solution {
|
103 | 105 | public:
|
104 | 106 | string maximumBinaryString(string binary) {
|
105 | 107 | int k = binary.find('0');
|
106 |
| - if (k == binary.npos) return binary; |
| 108 | + if (k == binary.npos) { |
| 109 | + return binary; |
| 110 | + } |
107 | 111 | int n = binary.size();
|
108 | 112 | for (int i = k + 1; i < n; ++i) {
|
109 | 113 | if (binary[i] == '0') {
|
@@ -135,6 +139,47 @@ func maximumBinaryString(binary string) string {
|
135 | 139 | }
|
136 | 140 | ```
|
137 | 141 |
|
| 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 | + |
138 | 183 | <!-- tabs:end -->
|
139 | 184 |
|
140 | 185 | <!-- end -->
|
0 commit comments