Skip to content

Commit

Permalink
Add problem 1592: Rearrange Spaces Between Words
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jul 3, 2023
1 parent b81cb94 commit fa93583
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ pub mod problem_1578_minimum_time_to_make_rope_colorful;
pub mod problem_1582_special_positions_in_a_binary_matrix;
pub mod problem_1588_sum_of_all_odd_length_subarrays;
pub mod problem_1590_make_sum_divisible_by_p;
pub mod problem_1592_rearrange_spaces_between_words;
pub mod problem_1593_split_a_string_into_the_max_number_of_unique_substrings;
pub mod problem_1594_maximum_non_negative_product_in_a_matrix;
pub mod problem_1598_crawler_log_folder;
Expand Down
80 changes: 80 additions & 0 deletions src/problem_1592_rearrange_spaces_between_words/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::iter;

impl Solution {
fn calculate_spaces_between_words(text: &[u8]) -> usize {
let mut words = 0;
let mut spaces = 0;
let mut is_space = true;

for &c in text {
let new_is_space = c == b' ';

spaces += usize::from(new_is_space);
words += usize::from(is_space && !new_is_space);
is_space = new_is_space;
}

if words < 2 {
0
} else {
spaces / (words - 1)
}
}

pub fn reorder_spaces(text: String) -> String {
let text = text.as_bytes();
let separator = iter::repeat(b' ').take(Self::calculate_spaces_between_words(text));
let mut result = Vec::with_capacity(text.len());
let mut word_start = None;

for (i, &c) in text.iter().enumerate() {
if c == b' ' {
if let Some(start) = word_start {
word_start = None;

if !result.is_empty() {
result.extend(separator.clone());
}

result.extend(&text[start..i]);
}
} else if word_start.is_none() {
word_start = Some(i);
}
}

if let Some(start) = word_start {
if !result.is_empty() {
result.extend(separator);
}

result.extend(&text[start..]);
}

let missing = text.len() - result.len();

result.extend(iter::repeat(b' ').take(missing));

String::from_utf8(result).unwrap()
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn reorder_spaces(text: String) -> String {
Self::reorder_spaces(text)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
22 changes: 22 additions & 0 deletions src/problem_1592_rearrange_spaces_between_words/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod iterative;

pub trait Solution {
fn reorder_spaces(text: String) -> String;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(" this is a sentence ", "this is a sentence"),
(" practice makes perfect", "practice makes perfect "),
("a", "a"),
];

for (text, expected) in test_cases {
assert_eq!(S::reorder_spaces(text.to_string()), expected);
}
}
}

0 comments on commit fa93583

Please sign in to comment.