Ctrl K

Even Gaps Between Words

Easy
The string line is made of words (lowercase English letters) and spaces; words are separated by at least one space, and there may be spaces at either end. The line contains at least one word. Move the spaces around so that the words stay in their original order, every two neighboring words have the same number of spaces between them, and that number is as large as possible. Any spaces that cannot be shared out evenly go to the end of the line, so the result has the same length as line. Return the resulting string.
Example 1
line="  red  fox jumps" → 'red  fox  jumps '

There are 5 spaces, 3 words and 2 gaps: 2 spaces per gap, and the one left over goes to the end.

Example 2
line=" solo  " → 'solo   '

There is only one word and no gaps, so all 3 spaces go to the end.

Constraints
  • 1 ≤ len(line) ≤ 100
  • line consists of lowercase English letters and spaces
  • line contains at least one word
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
line="  red  fox jumps"
Expected
'red  fox  jumps '

Run checks the first 3 tests in your browser. Submit checks all 133 tests on the server, hidden ones included.