1592. Rearrange Spaces Between Words Easy
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 */
28pub 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
35impl Solution {
36 pub fn reorder_spaces(text: String) -> String {
37 String::new()
38 }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_1592() {
49 }
50}
51
Back
© 2025 bowen.ge All Rights Reserved.