|
| 1 | +# [2864. Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number) |
| 2 | + |
| 3 | +[中文文档](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a <strong>binary</strong> string <code>s</code> that contains at least one <code>'1'</code>.</p> |
| 8 | + |
| 9 | +<p>You have to <strong>rearrange</strong> the bits in such a way that the resulting binary number is the <strong>maximum odd binary number</strong> that can be created from this combination.</p> |
| 10 | + |
| 11 | +<p>Return <em>a string representing the maximum odd binary number that can be created from the given combination.</em></p> |
| 12 | + |
| 13 | +<p><strong>Note </strong>that the resulting string <strong>can</strong> have leading zeros.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> s = "010" |
| 20 | +<strong>Output:</strong> "001" |
| 21 | +<strong>Explanation:</strong> Because there is just one '1', it must be in the last position. So the answer is "001". |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> s = "0101" |
| 28 | +<strong>Output:</strong> "1001" |
| 29 | +<strong>Explanation: </strong>One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001". |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= s.length <= 100</code></li> |
| 37 | + <li><code>s</code> consists only of <code>'0'</code> and <code>'1'</code>.</li> |
| 38 | + <li><code>s</code> contains at least one <code>'1'</code>.</li> |
| 39 | +</ul> |
| 40 | + |
| 41 | +## Solutions |
| 42 | + |
| 43 | +<!-- tabs:start --> |
| 44 | + |
| 45 | +### **Python3** |
| 46 | + |
| 47 | +```python |
| 48 | +class Solution: |
| 49 | + def maximumOddBinaryNumber(self, s: str) -> str: |
| 50 | + cnt = s.count("1") |
| 51 | + return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1" |
| 52 | +``` |
| 53 | + |
| 54 | +### **Java** |
| 55 | + |
| 56 | +```java |
| 57 | +class Solution { |
| 58 | + public String maximumOddBinaryNumber(String s) { |
| 59 | + int cnt = 0; |
| 60 | + for (char c : s.toCharArray()) { |
| 61 | + if (c == '1') { |
| 62 | + ++cnt; |
| 63 | + } |
| 64 | + } |
| 65 | + return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1"; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### **C++** |
| 71 | + |
| 72 | +```cpp |
| 73 | +class Solution { |
| 74 | +public: |
| 75 | + string maximumOddBinaryNumber(string s) { |
| 76 | + int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }); |
| 77 | + string ans; |
| 78 | + for (int i = 1; i < cnt; ++i) { |
| 79 | + ans.push_back('1'); |
| 80 | + } |
| 81 | + for (int i = 0; i < s.size() - cnt; ++i) { |
| 82 | + ans.push_back('0'); |
| 83 | + } |
| 84 | + ans.push_back('1'); |
| 85 | + return ans; |
| 86 | + } |
| 87 | +}; |
| 88 | +``` |
| 89 | +
|
| 90 | +### **Go** |
| 91 | +
|
| 92 | +```go |
| 93 | +func maximumOddBinaryNumber(s string) string { |
| 94 | + cnt := strings.Count(s, "1") |
| 95 | + return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### **TypeScript** |
| 100 | + |
| 101 | +```ts |
| 102 | +function maximumOddBinaryNumber(s: string): string { |
| 103 | + let cnt = 0; |
| 104 | + for (const c of s) { |
| 105 | + cnt += c === '1' ? 1 : 0; |
| 106 | + } |
| 107 | + return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1'; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### **...** |
| 112 | + |
| 113 | +``` |
| 114 | +
|
| 115 | +``` |
| 116 | + |
| 117 | +<!-- tabs:end --> |
0 commit comments