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 words are separated by at least one space. It is 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.
text = " this is a sentence ""this is a sentence"text = " practice makes perfect""practice makes perfect "Constraints
- 1 <= text.length <= 100
- text consists of lowercase English letters and ' '.
- text contains at least one word.
Asked at 1 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.