|
| 1 | +/** |
| 2 | + * [1592] Rearrange Spaces Between Words |
| 3 | + * |
| 4 | + * You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word. |
| 5 | + * Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text. |
| 6 | + * Return the string after rearranging the spaces. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: text = " this is a sentence " |
| 11 | + * Output: "this is a sentence" |
| 12 | + * Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces. |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * |
| 16 | + * Input: text = " practice makes perfect" |
| 17 | + * Output: "practice makes perfect " |
| 18 | + * Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string. |
| 19 | + * |
| 20 | + * |
| 21 | + * Constraints: |
| 22 | + * |
| 23 | + * 1 <= text.length <= 100 |
| 24 | + * text consists of lowercase English letters and ' '. |
| 25 | + * text contains at least one word. |
| 26 | + * |
| 27 | + */ |
| 28 | +pub struct Solution {} |
| 29 | + |
| 30 | +// problem: https://leetcode.com/problems/rearrange-spaces-between-words/ |
| 31 | +// discuss: https://leetcode.com/problems/rearrange-spaces-between-words/discuss/?currentPage=1&orderBy=most_votes&query= |
| 32 | + |
| 33 | +// submission codes start here |
| 34 | + |
| 35 | +impl Solution { |
| 36 | + pub fn reorder_spaces(text: String) -> String { |
| 37 | + let count_whitespace = text.chars().filter(|x| *x == ' ').count(); |
| 38 | + if count_whitespace == 0 { |
| 39 | + return text; |
| 40 | + } |
| 41 | + let div_mod = |x, y| (x / y, x % y); |
| 42 | + let words = text.split_whitespace().collect::<Vec<_>>(); |
| 43 | + |
| 44 | + if words.len() == 1 { |
| 45 | + words[0].to_owned() + " ".repeat(count_whitespace).as_str() |
| 46 | + } else { |
| 47 | + let (div_sep, mod_sep) = div_mod(count_whitespace, words.len() - 1); |
| 48 | + words.join(" ".repeat(div_sep).as_str()) + " ".repeat(mod_sep).as_str() |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// submission codes end |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_1592_example_1() { |
| 61 | + let text = " this is a sentence ".to_string(); |
| 62 | + |
| 63 | + let result = "this is a sentence".to_string(); |
| 64 | + |
| 65 | + assert_eq!(Solution::reorder_spaces(text), result); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn test_1592_example_2() { |
| 70 | + let text = " practice makes perfect".to_string(); |
| 71 | + |
| 72 | + let result = "practice makes perfect ".to_string(); |
| 73 | + |
| 74 | + assert_eq!(Solution::reorder_spaces(text), result); |
| 75 | + } |
| 76 | +} |
0 commit comments