Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Bit Manipulation
Maximum Odd Binary Number

Rearrange the bits of a binary string to form the largest possible odd binary number.

Acceptance 0%
Problem Statement

Given a binary string s, rearrange its characters to form the numerically largest possible binary number that is odd.

A binary number is odd only if its last bit is 1. You may reorder the characters in any way, but you must use all characters exactly once.

Return one such maximum odd binary string.

Input Format

  • A single binary string s.
  • s contains only characters '0' and '1'.
  • At least one character in s is '1' so that an odd result is possible.

Output Format

  • Return a binary string representing the largest possible odd number that can be formed by rearranging s.

Constraints

  • 1 <= s.length
  • s[i] ∈ {'0','1'}
  • The string contains at least one '1'.
  • All characters must be used exactly once.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "0101"

Output

"1001"

Explanation

The string has two 1s and two 0s. To make the largest odd number, place one 1 at the end and the other 1 as far left as possible: 1001.

Example 2

Input

s = "111"

Output

"111"

Explanation

The string is already the largest possible odd binary number.

Show 1 more example

Example 3

Input

s = "1010"

Output

"1001"

Explanation

Using all bits, the maximum odd arrangement is 1001.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.