Practice Rearrange Spaces Between Words
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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.
Source: Rearrange Spaces Between Words on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
text = " this is a sentence "
"this is a sentence"
# Example 2
text = " practice makes perfect"
"practice makes perfect "
Constraints
Related LeetCode Problems
Practice more problems similar to Rearrange Spaces Between Words.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.