You're working with a messy text string that contains words separated by various numbers of spaces. Your task is to redistribute the spaces evenly between all words to create a clean, uniformly formatted string.
The Challenge: Given a string with words and spaces, rearrange the spaces so that there's an equal number of spaces between every pair of adjacent words, and this number should be maximized. If you can't distribute all spaces equally, place any leftover spaces at the end.
Example: " this is a sentence " becomes "this is a sentence"
Goal: Return a string of the same length where spaces are optimally redistributed between words.
Input & Output
Constraints
- 1 โค text.length โค 100
- text consists of lowercase English letters and spaces ' '
- text contains at least one word
- Words are separated by at least one space