Formatted question description: https://leetcode.ca/all/1592.html

# 1592. Rearrange Spaces Between Words (Easy)

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.

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.

Return the string after rearranging the spaces.

Example 1:

Input: text = "  this   is  a sentence "
Output: "this   is   a   sentence"
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.


Example 2:

Input: text = " practice   makes   perfect"
Output: "practice   makes   perfect "
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.


Example 3:

Input: text = "hello   world"
Output: "hello   world"


Example 4:

Input: text = "  walks  udp package   into  bar a"
Output: "walks  udp  package  into  bar  a "


Example 5:

Input: text = "a"
Output: "a"


Constraints:

• 1 <= text.length <= 100
• text consists of lowercase English letters and ' '.
• text contains at least one word.

Related Topics:
String

Similar Questions:

## Solution 1.

// OJ: https://leetcode.com/problems/rearrange-spaces-between-words/

// Time: O(N)
// Space: O(N)
class Solution {
public:
string reorderSpaces(string s) {
int N = s.size(), cnt = 0;
for (char c : s) cnt += c == ' ';
string word;
istringstream ss(s);
vector<string> words;
while (ss >> word) words.push_back(word);
string ans;
if (words.size() == 1) {
ans += words;
for (int i = 0; i < cnt; ++i) ans += ' ';
return ans;
}
int d = cnt / (words.size() - 1), r = cnt - d * (words.size() - 1);
for (int i = 0; i < words.size(); ++i) {
ans += words[i];
int e = i < words.size() - 1 ? d : r;
for (int k = 0; k < e; ++k) ans += ' ';
}
return ans;
}
};


Or

// OJ: https://leetcode.com/problems/rearrange-spaces-between-words/

// Time: O(N)
// Space: O(1)
class Solution {
public:
string reorderSpaces(string s) {
int N = s.size(), space = 0, word = 0;
for (int i = 0; i < N; ++i) {
if (s[i] == ' ') space++;
else if (i == 0 || s[i - 1] == ' ') ++word;
}
string ans;
int d = word == 1 ? 0 : (space / (word - 1)), r = word == 1 ? space : (space - d * (word - 1));
for (int i = 0, w = 0; i < N; ++i) {
if (s[i] == ' ') continue;
while (i < N && s[i] != ' ') ans += s[i++];
int e = w < word - 1 ? d : r;
++w;
for (int k = 0; k < e; ++k) ans += ' ';
}
return ans;
}
};


Java

class Solution {
public String reorderSpaces(String text) {
List<String> wordsList = new ArrayList<String>();
int spaces = 0;
StringBuffer sb = new StringBuffer();
int length = text.length();
for (int i = 0; i < length; i++) {
char c = text.charAt(i);
if (c == ' ') {
spaces++;
if (sb.length() > 0) {
sb.setLength(0);
}
} else
sb.append(c);
}
if (sb.length() > 0)
StringBuffer reorder = new StringBuffer();
int size = wordsList.size();
if (size == 1) {
reorder.append(wordsList.get(0));
for (int i = 0; i < spaces; i++)
reorder.append(' ');
return reorder.toString();
}
int spacesEach = spaces / (size - 1);
int remain = spaces % (size - 1);
for (int i = 0; i < size; i++) {
if (i > 0) {
for (int j = 0; j < spacesEach; j++)
reorder.append(' ');
}
reorder.append(wordsList.get(i));
}
for (int i = 0; i < remain; i++)
reorder.append(' ');
return reorder.toString();
}
}