Skip to content

feat: add solutions to lc problem: No.1592 #3883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 84 additions & 59 deletions solution/1500-1599/1592.Rearrange Spaces Between Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}$ 的长度。

<!-- tabs:start -->

Expand All @@ -87,43 +87,90 @@ 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

```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<String> 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<string> split(const string& text) {
vector<string> words;
istringstream stream(text);
string word;
while (stream >> word) {
words.push_back(word);
}
return words;
}

string join(const vector<string>& 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
Expand All @@ -142,54 +189,32 @@ 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);
}
```

#### Rust

```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::<Vec<&str>>();
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)
}
}
```
Expand Down
145 changes: 87 additions & 58 deletions solution/1500-1599/1592.Rearrange Spaces Between Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ tags:

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand All @@ -65,43 +69,90 @@ 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

```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<String> 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<string> split(const string& text) {
vector<string> words;
istringstream stream(text);
string word;
while (stream >> word) {
words.push_back(word);
}
return words;
}

string join(const vector<string>& 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
Expand All @@ -120,54 +171,32 @@ 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);
}
```

#### Rust

```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::<Vec<&str>>();
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)
}
}
```
Expand Down
Loading
Loading