From 3704f9c9334ad33e77183e1d52cb6e33f0c44e72 Mon Sep 17 00:00:00 2001 From: jusexton Date: Fri, 26 Jul 2024 00:56:51 -0500 Subject: [PATCH] Added maximum odd binary number solution. --- README.md | 1 + src/leetcode/max_binary_number.rs | 17 +++++++++++++++++ src/leetcode/mod.rs | 1 + 3 files changed, 19 insertions(+) create mode 100644 src/leetcode/max_binary_number.rs diff --git a/README.md b/README.md index 06cf2bd..380f70c 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ other languages. - Sort the People (https://leetcode.com/problems/sort-the-people) - Max Repeating Substring (https://leetcode.com/problems/maximum-repeating-substring/) - Sort by Increasing Frequency (https://leetcode.com/problems/sort-array-by-increasing-frequency) +- Maximum Odd Binary Number (https://leetcode.com/problems/maximum-odd-binary-number) #### Medium diff --git a/src/leetcode/max_binary_number.rs b/src/leetcode/max_binary_number.rs new file mode 100644 index 0000000..e8f50d7 --- /dev/null +++ b/src/leetcode/max_binary_number.rs @@ -0,0 +1,17 @@ +pub fn maximum_odd_binary_number(s: String) -> String { + let one_count = s.chars().filter(|c| *c == '1').count(); + let ones: String = "1".repeat(one_count - 1); + let zeroes: String = "0".repeat(s.len() - one_count); + format!("{ones}{zeroes}1") +} + +#[cfg(test)] +mod tests { + use super::maximum_odd_binary_number; + + #[test] + fn test_maximum_odd_binary_is_produced() { + let actual = maximum_odd_binary_number("101101".to_string()); + assert_eq!("111001", actual) + } +} diff --git a/src/leetcode/mod.rs b/src/leetcode/mod.rs index a109244..248b19f 100644 --- a/src/leetcode/mod.rs +++ b/src/leetcode/mod.rs @@ -49,3 +49,4 @@ mod sort_people; mod max_repeating_substr; mod freq_sort; mod sort_jumbled_numbers; +mod max_binary_number;