Skip to main content

Rearrange Spaces Between Words

In this page, we will solve the Rearrange Spaces Between Words problem using different approaches. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more.

Problem Description​

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 distribute all the spaces equally, place the extra spaces at the end, including between the last word and the end of the string.

Examples​

Example 1:

Input: text = "  this   is  a sentence "
Output: "this is a sentence"

Example 2:

Input: text = " practice   makes   perfect"
Output: "practice makes perfect "

Constraints​

  • 1<=text.length<=1001 <= text.length <= 100
  • text consists of lowercase English letters and spaces.
  • text contains at least one word.

Solution for Rearrange Spaces Between Words​

Intuition and Approach​

To solve this problem, we can follow these steps:

  1. Count the total number of spaces in the input string.
  2. Split the input string into words.
  3. Calculate the number of spaces that should be placed between words and the number of remaining spaces.
  4. Construct the result string by joining the words with the calculated spaces and appending the remaining spaces at the end if needed.

Approach 1: Splitting and Joining​

We first split the text into words and count the spaces. Then, we calculate the spaces to distribute between words and the remaining spaces. Finally, we join the words with the calculated spaces and add the remaining spaces at the end if necessary.

Implementation​

Live Editor
function rearrangeSpacesBetweenWords() {
  const text = "  this   is  a sentence ";

  const rearrangeSpaces = function(text) {
    const totalSpaces = text.split('').filter(c => c === ' ').length;
    const words = text.trim().split(/\s+/);
    const numWords = words.length;

    if (numWords === 1) {
      return words[0] + ' '.repeat(totalSpaces);
    }

    const spacesBetweenWords = Math.floor(totalSpaces / (numWords - 1));
    const remainingSpaces = totalSpaces % (numWords - 1);

    return words.join(' '.repeat(spacesBetweenWords)) + ' '.repeat(remainingSpaces);
  };

  const result = rearrangeSpaces(text);
  return (
    <div>
      <p>
        <b>Input:</b> text = "{text}"
      </p>
      <p>
        <b>Output:</b> "{result}"
      </p>
    </div>
  );
}
Result
Loading...

Code in Different Languages​

Written by @manishh12
 function rearrangeSpaces(text) {
const totalSpaces = text.split('').filter(c => c === ' ').length;
const words = text.trim().split(/\s+/);
const numWords = words.length;

if (numWords === 1) {
return words[0] + ' '.repeat(totalSpaces);
}

const spacesBetweenWords = Math.floor(totalSpaces / (numWords - 1));
const remainingSpaces = totalSpaces % (numWords - 1);

return words.join(' '.repeat(spacesBetweenWords)) + ' '.repeat(remainingSpaces);
}

Complexity Analysis​

  • Time Complexity: O(n)O(n) where n is the length of the input string.
  • Space Complexity: O(n)O(n) for storing the words and the result string.

tip

By using the above approach, we can efficiently solve the Rearrange Spaces Between Words problem. The choice of implementation language depends on the specific requirements and constraints of the problem.

References​