Given a string text
consisting of words and spaces, your task is to rearrange the spaces between the words so that the spaces are evenly distributed between the words. If there are extra spaces left after even distribution, they should be placed at the end of the string. The rearranged string must have the same length as the original text
.
text
.
Example: If text = " this is a sentence "
, the output should be "this is a sentence"
(with evenly distributed spaces).
When approaching this problem, the first instinct might be to split the string into words and then simply join them with a single space. However, the challenge is to distribute all the original spaces as evenly as possible between the words, with any leftover spaces going to the end. This means we need to:
A brute-force approach might involve repeatedly adding one space at a time between words, but that's inefficient. Instead, we can use division and modulo operations to quickly determine how many spaces go between each word and how many are left for the end.
Here’s a step-by-step guide to solving the problem efficiently:
split()
in most languages) to extract all the words, ignoring extra spaces.number_of_words - 1
.This approach is efficient because it only requires a few passes through the string and uses simple arithmetic to distribute the spaces.
Let’s walk through the example text = " practice makes perfect"
:
The final result is "practice makes perfect "
.
class Solution:
def reorderSpaces(self, text: str) -> str:
spaces = text.count(' ')
words = text.split()
if len(words) == 1:
# All spaces go to the end if only one word
return words[0] + ' ' * spaces
gaps = len(words) - 1
spaces_per_gap = spaces // gaps
extra_spaces = spaces % gaps
return (' ' * spaces_per_gap).join(words) + ' ' * extra_spaces
class Solution {
public:
string reorderSpaces(string text) {
int spaces = 0;
vector<string> words;
string word;
for (char c : text) {
if (c == ' ') {
spaces++;
if (!word.empty()) {
words.push_back(word);
word.clear();
}
} else {
word += c;
}
}
if (!word.empty()) words.push_back(word);
if (words.size() == 1) {
return words[0] + string(spaces, ' ');
}
int gaps = words.size() - 1;
int spaces_per_gap = spaces / gaps;
int extra_spaces = spaces % gaps;
string result;
for (int i = 0; i < words.size(); ++i) {
result += words[i];
if (i != words.size() - 1)
result += string(spaces_per_gap, ' ');
}
result += string(extra_spaces, ' ');
return result;
}
};
class Solution {
public String reorderSpaces(String text) {
int spaces = 0;
List<String> words = new ArrayList<>();
StringBuilder word = new StringBuilder();
for (char c : text.toCharArray()) {
if (c == ' ') {
spaces++;
if (word.length() > 0) {
words.add(word.toString());
word.setLength(0);
}
} else {
word.append(c);
}
}
if (word.length() > 0) words.add(word.toString());
if (words.size() == 1) {
return words.get(0) + " ".repeat(spaces);
}
int gaps = words.size() - 1;
int spacesPerGap = spaces / gaps;
int extraSpaces = spaces % gaps;
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.size(); i++) {
result.append(words.get(i));
if (i != words.size() - 1) {
result.append(" ".repeat(spacesPerGap));
}
}
result.append(" ".repeat(extraSpaces));
return result.toString();
}
}
var reorderSpaces = function(text) {
let spaces = 0;
let words = [];
let word = '';
for (let c of text) {
if (c === ' ') {
spaces++;
if (word.length > 0) {
words.push(word);
word = '';
}
} else {
word += c;
}
}
if (word.length > 0) words.push(word);
if (words.length === 1) {
return words[0] + ' '.repeat(spaces);
}
let gaps = words.length - 1;
let spacesPerGap = Math.floor(spaces / gaps);
let extraSpaces = spaces % gaps;
let result = words.join(' '.repeat(spacesPerGap));
result += ' '.repeat(extraSpaces);
return result;
};
Brute-force approach: If we attempted to insert spaces one at a time between words, the time complexity could be O(N²), where N is the length of the string, due to repeated string concatenation.
Optimized approach (as above):
text
. We make a constant number of passes over the input: one to count spaces and extract words, another to build the output.These bounds are optimal for this problem, as every character must be examined at least once.
The key to solving "Rearrange Spaces Between Words" efficiently is to recognize that the problem is about counting and distributing spaces, not about complex string manipulation. By counting spaces and words, then using simple arithmetic to determine the distribution, we can construct the result in linear time. This approach is both concise and robust, ensuring all original spaces are used and the output string matches the required length and format.