Rearrange the bits of a binary string to form the largest possible odd binary number.
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. scontains only characters'0'and'1'.- At least one character in
sis'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.lengths[i] ∈ {'0','1'}- The string contains at least one
'1'. - All characters must be used exactly once.
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.