Saturday, February 27, 2021

[LeetCode] Rearrange Spaces Between Words

Problem: 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.

Example:

Input: text = "  this   is  a sentence "
Output: "this   is   a   sentence"
Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
Input: text = " practice   makes   perfect"
Output: "practice   makes   perfect "
Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
Input: text = "hello   world"
Output: "hello   world"
Input: text = "  walks  udp package   into  bar a"
Output: "walks  udp  package  into  bar  a "
Input: text = "a"
Output: "a"


Approach: An easy problem to solve. We can traverse the given text and count the number of words and spaces. Now our task becomes easy:

Number_Of_Spaces_Between_Words = Number_Of_Spaces / Number_Of_Words

Number_Of_Spaces_At_End = Number_Of_Spaces % Number_Of_Words

We just need to take care of some boundary conditions and we are done.


Implementation in C#:

    public string ReorderSpaces(string text) 

    {

        int numOfSpaces = 0;

        List<string> words = new List<string>(); 

        for (int i = 0; i < text.Length; )

        {

            if (text[i] == ' ')

            {

                ++numOfSpaces;

                ++i;

            }

            else

            {

                StringBuilder stringBuilder = new StringBuilder();

                while (i < text.Length && text[i] != ' ')

                {

                    stringBuilder.Append(text[i++]);

                }

                words.Add(stringBuilder.ToString());

            }

        }

        int spacesBetweenWords = 0;

        int spacesRemaining = numOfSpaces;

        if (words.Count > 1)

        {

            spacesBetweenWords = numOfSpaces / (words.Count - 1);

            spacesRemaining = numOfSpaces % (words.Count - 1);

        }

        StringBuilder spaceStringBuilder = new StringBuilder();

        for (int i = 0; i < spacesBetweenWords; ++i)

        {

            spaceStringBuilder.Append(' ');

        }

        StringBuilder spaceRemainingBuilder = new StringBuilder();

        for (int i = 0; i < spacesRemaining; ++i)

        {

            spaceRemainingBuilder.Append(' ');

        }

        return string.Join(spaceStringBuilder.ToString(), words) + spaceRemainingBuilder.ToString();

    }


Complexity: O(n)

No comments:

Post a Comment