# Rearranging spaces between words in Python

In this tutorial, we will be solving a Leet code weekly contest problem in which we are required to rearrange the spaces between a given string text using Python programming.

Assume that we are given a string that has a bit of text and in between those words we find that there are a number of unhealthy spaces between them. This means that instead of conventionally having a single space character in between words, the spaces may be more than that.

Our goal can be thought of as something similar to text formatting. We will be required to parse through the text and split the spaces equally between the words such that the number of space characters between any two words in a given text will be equal.

### Procedure:

If we are mathematically unable to perform such an operation, we are allowed to put the extra spaces in the end. I have solved the problem in the manner given below and used Python for its implementation.

In a variable a, we extract the words alone and store it in a list which is achieved by the line:

`a=text.split()`

#### Counting spaces:

Our first goal would be to count the number of spaces in the given text. I handle texts with only one word in an easier fashion since all you’ve got to do it just add those extra spaces at the front to the back. This done using this following block of code:

```for i in list(text):
if(i==" "):
spaces+=1```

#### Splitting the spaces:

If the text contains more than one words, again the first thing I do is to count the number of total number of spaces. Next step is to count if the even split is possible. If there are enough words to split the spaces between the words, we can easily reconstruct the correct string with the number of spaces between each word calculated as spaces / (len(a)-1), where len(a) represents the number of words.

```if(spaces%(len(a)-1)==0):
for j in a:
mystr+=j + int((spaces/(len(a)-1)))*" "
return (mystr[0:len(mystr)-int((spaces/(len(a)-1)))])```

The format of every word should be

word + equal number of spaces.

The number of spaces after every word should be n =  (spaces/(len(a)-1)). This space must not be added after the last word.

The above block of code does the job when spaces can be equally split between the words. We check for the condition if it’s possible to split spaces among words equally. This is carried out using the if statement. After that, we append to the empty string, each word and the number of spaces that need to follow it. The variable j represents the word and (spaces/(len(a)-1)) indicates the number of spaces. We use len(a)-1 because the spaces have to be split between the words. For eg., if there are 2 words, there is only 1 position in between 2 words for the spaces to fit in.

After that, we see that for the last word also we have added the spacing which needs to be removed before returning. Therefore in the return statement, we exclude the last n spaces using string slicing.

#### Extraspaces:

If this is not the case, we will have to add the extra spaces towards the end which can be calculated as the reminder of the above operation. The new string is reconstructed as per this format i.e. by splitting the spaces equally and finally adding the remaining spaces towards the end. This logic has been implemented in the Python code below:

```extraspaces=spaces%(len(a)-1)
spaces=spaces-extraspaces
print(extraspaces, spaces)
for j in a:
mystr+=j + int((spaces/(len(a)-1)))*" "
print(len(text), len(mystr[0:len(mystr)-int((spaces/(len(a)-1)))] + extraspaces*" "))
return (mystr[0:len(mystr)-int((spaces/(len(a)-1)))] + extraspaces*" ")
```

In the above block of code, we first calculate the number of spaces we need to add towards the end as the reminder of the total number of spaces when divided by the no. of positions between words. Now the total number of spaces that we need to split equally between the words comes down to spaces – extraspaces. As usual we split the new spaces equally and construct the string. We are not done yet. We need to add the extraspaces towards the end as extraspaces*” “. This is done in the return statement.

### Examples:

Input:

`welcome    everyone`

Output:

`welcome    everyone`

Input:

`"  this   is  a sentence "`

Output:

`"this   is   a   sentence"`

We see that we have split the spaces (9) equally between the three words.

Input:

`"Code   Speedy  Tech"`

Output:

`"Code  Speedy  Tech "`

Here, we have 5 spaces and 3 words. We can not equally split them. There is one extra space. So we places two spaces after every word and after the last word, place the extra space.

#### Here is the complete Python code to the above logic:

```class Solution:
def reorderSpaces(self, text: str) -> str:
spaces=0
print(list(text))

a=text.split()
print(a)
if(len(a)==1):
if(text==" "):
for k in text:
if(k==" "):
spaces+=1
return str(a)+ spaces*" "
else:
for i in list(text):
if(i==" "):
spaces+=1
mystr=""
if(spaces%(len(a)-1)==0):                                  #Condition when spaces can be equally split between the words
for j in a:
mystr+=j + int((spaces/(len(a)-1)))*" "            #For every word, add the required number of spaces after it
return (mystr[0:len(mystr)-int((spaces/(len(a)-1)))])
else:
extraspaces=spaces%(len(a)-1)
spaces=spaces-extraspaces
print(extraspaces, spaces)
for j in a:
mystr+=j + int((spaces/(len(a)-1)))*" "
print(len(text), len(mystr[0:len(mystr)-int((spaces/(len(a)-1)))] + extraspaces*" "))
return (mystr[0:len(mystr)-int((spaces/(len(a)-1)))] + extraspaces*" ")
```