01.03.2024
2864. Maximum Odd Binary Number easy
blog post
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/525
Problem TLDR
Max odd number string rearrangement.
Intuition
Count zeros and ones and build a string.
Approach
Let’s try to find the shortest version of code.
Complexity
Time complexity:
O(n)$Space complexity:
O(n)
Code
fun maximumOddBinaryNumber(s: String) =
s.count { it == '0' }.let {
"1".repeat(s.length - it - 1) + "0".repeat(it) + "1"
}
pub fn maximum_odd_binary_number(s: String) -> String {
let c0 = s.bytes().filter(|b| *b == b'0').count();
format!("{}{}1", "1".repeat(s.len() - c0 - 1), "0".repeat(c0))
}