diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md b/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md index a1e4737e1557b..ca1f6f2553724 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/README.md @@ -76,9 +76,9 @@ tags: ### 方法一:字符串模拟 -统计字符串 `text` 中的空格数,记为 `cnt`。将 `text` 按空格分割成字符串数组 `words`。然后计算相邻字符串之间需要拼接的空格数,进行拼接。最后将剩余的空格拼接在末尾。 +我们先统计字符串 $\textit{text}$ 中的空格数,记为 $\textit{spaces}$。将 $\textit{text}$ 按空格分割成字符串数组 $\textit{words}$。然后计算相邻字符串之间需要拼接的空格数,进行拼接。最后将剩余的空格拼接在末尾。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 表示字符串 `text` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 表示字符串 $\textit{text}$ 的长度。 @@ -87,12 +87,12 @@ tags: ```python class Solution: def reorderSpaces(self, text: str) -> str: - cnt = text.count(' ') + spaces = text.count(" ") words = text.split() - m = len(words) - 1 - if m == 0: - return words[0] + ' ' * cnt - return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m) + if len(words) == 1: + return words[0] + " " * spaces + cnt, mod = divmod(spaces, len(words) - 1) + return (" " * cnt).join(words) + " " * mod ``` #### Java @@ -100,30 +100,77 @@ class Solution: ```java class Solution { public String reorderSpaces(String text) { - int cnt = 0; + int spaces = 0; for (char c : text.toCharArray()) { if (c == ' ') { - ++cnt; + ++spaces; } } - String[] words = text.split("\\s+"); - List res = new ArrayList<>(); - for (String w : words) { - if (!"".equals(w)) { - res.add(w); - } + String[] words = text.trim().split("\\s+"); + if (words.length == 1) { + return words[0] + " ".repeat(spaces); } - int m = res.size() - 1; - if (m == 0) { - return res.get(0) + " ".repeat(cnt); + int cnt = spaces / (words.length - 1); + int mod = spaces % (words.length - 1); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < words.length; ++i) { + sb.append(words[i]); + if (i < words.length - 1) { + sb.append(" ".repeat(cnt)); + } } - String ans = String.join(" ".repeat(cnt / m), res); - ans += " ".repeat(cnt % m); - return ans; + sb.append(" ".repeat(mod)); + return sb.toString(); } } ``` +#### C++ + +```cpp +class Solution { +public: + string reorderSpaces(string text) { + int spaces = ranges::count(text, ' '); + auto words = split(text); + + if (words.size() == 1) { + return words[0] + string(spaces, ' '); + } + + int cnt = spaces / (words.size() - 1); + int mod = spaces % (words.size() - 1); + + string result = join(words, string(cnt, ' ')); + result += string(mod, ' '); + + return result; + } + +private: + vector split(const string& text) { + vector words; + istringstream stream(text); + string word; + while (stream >> word) { + words.push_back(word); + } + return words; + } + + string join(const vector& words, const string& separator) { + ostringstream result; + for (size_t i = 0; i < words.size(); ++i) { + result << words[i]; + if (i < words.size() - 1) { + result << separator; + } + } + return result.str(); + } +}; +``` + #### Go ```go @@ -142,22 +189,15 @@ func reorderSpaces(text string) string { ```ts function reorderSpaces(text: string): string { - let count = 0; - for (const c of text) { - if (c === ' ') { - count++; - } - } - - const words = text.trim().split(/\s+/g); - const n = words.length; - if (n === 1) { - return words.join('') + ''.padStart(count); + const spaces = (text.match(/ /g) || []).length; + const words = text.split(/\s+/).filter(Boolean); + if (words.length === 1) { + return words[0] + ' '.repeat(spaces); } - - const rest = count % (words.length - 1); - const per = (count - rest) / (words.length - 1); - return words.join(''.padStart(per)) + ''.padStart(rest); + const cnt = Math.floor(spaces / (words.length - 1)); + const mod = spaces % (words.length - 1); + const result = words.join(' '.repeat(cnt)); + return result + ' '.repeat(mod); } ``` @@ -165,31 +205,16 @@ function reorderSpaces(text: string): string { ```rust impl Solution { - fn create_spaces(n: usize) -> String { - let mut res = String::new(); - for _ in 0..n { - res.push(' '); - } - res - } - pub fn reorder_spaces(text: String) -> String { - let count = { - let mut res = 0; - for c in text.as_bytes() { - if c == &b' ' { - res += 1; - } - } - res - }; - - let works = text.split_whitespace().collect::>(); - let n = works.len(); - if n == 1 { - return works[0].to_string() + &Self::create_spaces(count); + let spaces = text.chars().filter(|&c| c == ' ').count(); + let words: Vec<&str> = text.split_whitespace().collect(); + if words.len() == 1 { + return format!("{}{}", words[0], " ".repeat(spaces)); } - works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) + let cnt = spaces / (words.len() - 1); + let mod_spaces = spaces % (words.len() - 1); + let result = words.join(&" ".repeat(cnt)); + result + &" ".repeat(mod_spaces) } } ``` diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md b/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md index 5884315d27f16..b05a527f93c7f 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md @@ -56,7 +56,11 @@ tags: -### Solution 1 +### Solution 1: String Simulation + +First, we count the number of spaces in the string $\textit{text}$, denoted as $\textit{spaces}$. Then, we split $\textit{text}$ by spaces into an array of strings $\textit{words}$. Next, we calculate the number of spaces that need to be inserted between adjacent words and perform the concatenation. Finally, we append the remaining spaces to the end. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ represents the length of the string $\textit{text}$. @@ -65,12 +69,12 @@ tags: ```python class Solution: def reorderSpaces(self, text: str) -> str: - cnt = text.count(' ') + spaces = text.count(" ") words = text.split() - m = len(words) - 1 - if m == 0: - return words[0] + ' ' * cnt - return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m) + if len(words) == 1: + return words[0] + " " * spaces + cnt, mod = divmod(spaces, len(words) - 1) + return (" " * cnt).join(words) + " " * mod ``` #### Java @@ -78,30 +82,77 @@ class Solution: ```java class Solution { public String reorderSpaces(String text) { - int cnt = 0; + int spaces = 0; for (char c : text.toCharArray()) { if (c == ' ') { - ++cnt; + ++spaces; } } - String[] words = text.split("\\s+"); - List res = new ArrayList<>(); - for (String w : words) { - if (!"".equals(w)) { - res.add(w); - } + String[] words = text.trim().split("\\s+"); + if (words.length == 1) { + return words[0] + " ".repeat(spaces); } - int m = res.size() - 1; - if (m == 0) { - return res.get(0) + " ".repeat(cnt); + int cnt = spaces / (words.length - 1); + int mod = spaces % (words.length - 1); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < words.length; ++i) { + sb.append(words[i]); + if (i < words.length - 1) { + sb.append(" ".repeat(cnt)); + } } - String ans = String.join(" ".repeat(cnt / m), res); - ans += " ".repeat(cnt % m); - return ans; + sb.append(" ".repeat(mod)); + return sb.toString(); } } ``` +#### C++ + +```cpp +class Solution { +public: + string reorderSpaces(string text) { + int spaces = ranges::count(text, ' '); + auto words = split(text); + + if (words.size() == 1) { + return words[0] + string(spaces, ' '); + } + + int cnt = spaces / (words.size() - 1); + int mod = spaces % (words.size() - 1); + + string result = join(words, string(cnt, ' ')); + result += string(mod, ' '); + + return result; + } + +private: + vector split(const string& text) { + vector words; + istringstream stream(text); + string word; + while (stream >> word) { + words.push_back(word); + } + return words; + } + + string join(const vector& words, const string& separator) { + ostringstream result; + for (size_t i = 0; i < words.size(); ++i) { + result << words[i]; + if (i < words.size() - 1) { + result << separator; + } + } + return result.str(); + } +}; +``` + #### Go ```go @@ -120,22 +171,15 @@ func reorderSpaces(text string) string { ```ts function reorderSpaces(text: string): string { - let count = 0; - for (const c of text) { - if (c === ' ') { - count++; - } + const spaces = (text.match(/ /g) || []).length; + const words = text.split(/\s+/).filter(Boolean); + if (words.length === 1) { + return words[0] + ' '.repeat(spaces); } - - const words = text.trim().split(/\s+/g); - const n = words.length; - if (n === 1) { - return words.join('') + ''.padStart(count); - } - - const rest = count % (words.length - 1); - const per = (count - rest) / (words.length - 1); - return words.join(''.padStart(per)) + ''.padStart(rest); + const cnt = Math.floor(spaces / (words.length - 1)); + const mod = spaces % (words.length - 1); + const result = words.join(' '.repeat(cnt)); + return result + ' '.repeat(mod); } ``` @@ -143,31 +187,16 @@ function reorderSpaces(text: string): string { ```rust impl Solution { - fn create_spaces(n: usize) -> String { - let mut res = String::new(); - for _ in 0..n { - res.push(' '); - } - res - } - pub fn reorder_spaces(text: String) -> String { - let count = { - let mut res = 0; - for c in text.as_bytes() { - if c == &b' ' { - res += 1; - } - } - res - }; - - let works = text.split_whitespace().collect::>(); - let n = works.len(); - if n == 1 { - return works[0].to_string() + &Self::create_spaces(count); + let spaces = text.chars().filter(|&c| c == ' ').count(); + let words: Vec<&str> = text.split_whitespace().collect(); + if words.len() == 1 { + return format!("{}{}", words[0], " ".repeat(spaces)); } - works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) + let cnt = spaces / (words.len() - 1); + let mod_spaces = spaces % (words.len() - 1); + let result = words.join(&" ".repeat(cnt)); + result + &" ".repeat(mod_spaces) } } ``` diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.cpp b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.cpp new file mode 100644 index 0000000000000..17f31c9265859 --- /dev/null +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + string reorderSpaces(string text) { + int spaces = ranges::count(text, ' '); + auto words = split(text); + + if (words.size() == 1) { + return words[0] + string(spaces, ' '); + } + + int cnt = spaces / (words.size() - 1); + int mod = spaces % (words.size() - 1); + + string result = join(words, string(cnt, ' ')); + result += string(mod, ' '); + + return result; + } + +private: + vector split(const string& text) { + vector words; + istringstream stream(text); + string word; + while (stream >> word) { + words.push_back(word); + } + return words; + } + + string join(const vector& words, const string& separator) { + ostringstream result; + for (size_t i = 0; i < words.size(); ++i) { + result << words[i]; + if (i < words.size() - 1) { + result << separator; + } + } + return result.str(); + } +}; diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.java b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.java index d26575d9f77b8..58b1b1876a19d 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.java +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.java @@ -1,24 +1,25 @@ class Solution { public String reorderSpaces(String text) { - int cnt = 0; + int spaces = 0; for (char c : text.toCharArray()) { if (c == ' ') { - ++cnt; + ++spaces; } } - String[] words = text.split("\\s+"); - List res = new ArrayList<>(); - for (String w : words) { - if (!"".equals(w)) { - res.add(w); - } + String[] words = text.trim().split("\\s+"); + if (words.length == 1) { + return words[0] + " ".repeat(spaces); } - int m = res.size() - 1; - if (m == 0) { - return res.get(0) + " ".repeat(cnt); + int cnt = spaces / (words.length - 1); + int mod = spaces % (words.length - 1); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < words.length; ++i) { + sb.append(words[i]); + if (i < words.length - 1) { + sb.append(" ".repeat(cnt)); + } } - String ans = String.join(" ".repeat(cnt / m), res); - ans += " ".repeat(cnt % m); - return ans; + sb.append(" ".repeat(mod)); + return sb.toString(); } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.py b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.py index 3e4fcc3f08448..8449d913f8cf2 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.py +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.py @@ -1,8 +1,8 @@ class Solution: def reorderSpaces(self, text: str) -> str: - cnt = text.count(' ') + spaces = text.count(" ") words = text.split() - m = len(words) - 1 - if m == 0: - return words[0] + ' ' * cnt - return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m) + if len(words) == 1: + return words[0] + " " * spaces + cnt, mod = divmod(spaces, len(words) - 1) + return (" " * cnt).join(words) + " " * mod diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs index 488a04e21b7a3..4f1465c73e00c 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.rs @@ -1,28 +1,13 @@ impl Solution { - fn create_spaces(n: usize) -> String { - let mut res = String::new(); - for _ in 0..n { - res.push(' '); - } - res - } - pub fn reorder_spaces(text: String) -> String { - let count = { - let mut res = 0; - for c in text.as_bytes() { - if c == &b' ' { - res += 1; - } - } - res - }; - - let works = text.split_whitespace().collect::>(); - let n = works.len(); - if n == 1 { - return works[0].to_string() + &Self::create_spaces(count); + let spaces = text.chars().filter(|&c| c == ' ').count(); + let words: Vec<&str> = text.split_whitespace().collect(); + if words.len() == 1 { + return format!("{}{}", words[0], " ".repeat(spaces)); } - works.join(&Self::create_spaces(count / (n - 1))) + &Self::create_spaces(count % (n - 1)) + let cnt = spaces / (words.len() - 1); + let mod_spaces = spaces % (words.len() - 1); + let result = words.join(&" ".repeat(cnt)); + result + &" ".repeat(mod_spaces) } } diff --git a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.ts b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.ts index d3d5898e8efdc..72a29df9531de 100644 --- a/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.ts +++ b/solution/1500-1599/1592.Rearrange Spaces Between Words/Solution.ts @@ -1,18 +1,11 @@ function reorderSpaces(text: string): string { - let count = 0; - for (const c of text) { - if (c === ' ') { - count++; - } + const spaces = (text.match(/ /g) || []).length; + const words = text.split(/\s+/).filter(Boolean); + if (words.length === 1) { + return words[0] + ' '.repeat(spaces); } - - const words = text.trim().split(/\s+/g); - const n = words.length; - if (n === 1) { - return words.join('') + ''.padStart(count); - } - - const rest = count % (words.length - 1); - const per = (count - rest) / (words.length - 1); - return words.join(''.padStart(per)) + ''.padStart(rest); + const cnt = Math.floor(spaces / (words.length - 1)); + const mod = spaces % (words.length - 1); + const result = words.join(' '.repeat(cnt)); + return result + ' '.repeat(mod); }