1592. Rearrange Spaces Between Words LeetCode Solution
In this guide, you will get 1592. Rearrange Spaces Between Words LeetCode Solution with the best time and space complexity. The solution to Rearrange Spaces Between Words problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rearrange Spaces Between Words solution in C++
- Rearrange Spaces Between Words solution in Java
- Rearrange Spaces Between Words solution in Python
- Additional Resources

Problem Statement of Rearrange Spaces Between Words
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.
Constraints:
1 <= text.length <= 100
text consists of lowercase English letters and ' '.
text contains at least one word.
Complexity Analysis
- Time Complexity: O(|\texttt{text}|)
- Space Complexity: O(|\texttt{text}|)
1592. Rearrange Spaces Between Words LeetCode Solution in C++
class Solution {
public:
string reorderSpaces(string text) {
const int spaces = ranges::count(text, ' ');
string ans;
vector<string> words;
istringstream iss(text);
string word;
while (iss >> word)
words.push_back(word);
if (words.size() == 1)
return word + string(spaces, ' ');
const int gapSize = spaces / (words.size() - 1);
const int remains = spaces % (words.size() - 1);
for (int i = 0; i < words.size() - 1; ++i)
ans += words[i] + string(gapSize, ' ');
ans += words.back() + string(remains, ' ');
return ans;
}
};
/* code provided by PROGIEZ */
1592. Rearrange Spaces Between Words LeetCode Solution in Java
class Solution {
public String reorderSpaces(String text) {
final String[] words = text.trim().split("\\s+");
final int spaces = (int) text.chars().filter(c -> c == ' ').count();
final int n = words.length;
final int gapSize = n == 1 ? 0 : spaces / (n - 1);
final int remains = n == 1 ? spaces : spaces % (n - 1);
return String.join(" ".repeat(gapSize), words) + " ".repeat(remains);
}
}
// code provided by PROGIEZ
1592. Rearrange Spaces Between Words LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.