Skip to content

Commit

Permalink
Added maximum odd binary number solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 26, 2024
1 parent 9fe38b0 commit 3704f9c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/leetcode/max_binary_number.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ mod sort_people;
mod max_repeating_substr;
mod freq_sort;
mod sort_jumbled_numbers;
mod max_binary_number;

0 comments on commit 3704f9c

Please sign in to comment.