Skip to content

2864. Maximum Odd Binary Number 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  string maximumOddBinaryNumber(string s) {
    const int zeros = ranges::count(s, '0');
    const int ones = s.length() - zeros;
    return string(ones - 1, '1') + string(zeros, '0') + '1';
  }
};
1
2
3
4
5
6
7
class Solution {
  public String maximumOddBinaryNumber(String s) {
    final int zeros = (int) s.chars().filter(c -> c == '0').count();
    final int ones = s.length() - zeros;
    return "1".repeat(ones - 1) + "0".repeat(zeros) + "1";
  }
}
1
2
3
class Solution:
  def maximumOddBinaryNumber(self, s: str) -> str:
    return '1' * (s.count('1') - 1) + '0' * s.count('0') + '1'