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

Example 2:
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.

Example 3:
Input: text = "hello   world"
Output: "hello   world"

Example 4:
Input: text = "  walks  udp package   into  bar a"
Output: "walks  udp  package  into  bar  a "

Example 5:
Input: text = "a"
Output: "a"

 
Constraints:

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

Difficulty: introductory

Starter code:

class Solution:
    def reorderSpaces(self, text: str) -> str:
        

Solutions:
class Solution:
    def reorderSpaces(self, text: str) -> str:
        s = text.split()
        if len(s) == 1: 
            text = ''.join(s) + ' '*text.count(' ')
            return text
        count = text.count(' ')//(len(s)-1)
        extra = text.count(' ')%(len(s)-1)
        result = ''
        num = 0
        for c in s:
            result += c
            num += 1
            if num <= (len(s)-1):
                result += ' '*count
        qqqqqqqqq = [3]*100000
        if extra != 0:
            result += ' '*extra
        return result

class Solution:
    def reorderSpaces(self, text: str) -> str:

        # separate words and spaces
        words = text.split(' ')
        words = list(filter(lambda x: len(x) > 0, words))
        # print(words)
        
        # get their counts so we can do some math
        wordsLen = len(words)
        spacesLen = text.count(' ')
        
        # print(wordsLen, spacesLen)
        if(wordsLen == 1):
            return words[0] + ' ' * spacesLen
        evenDistSpaces = spacesLen // (wordsLen-1)
        endDistSpaces = spacesLen % (wordsLen-1)
        
        # print(evenDistSpaces, endDistSpaces)
        
        space = ' ' * evenDistSpaces
        end = ' ' *  endDistSpaces
        
        print(len(space), len(end))
        
        resultString = space.join(words)
        resultString += end
        
        return resultString

class Solution:
    def reorderSpaces(self, text: str) -> str:
        # count the number of space
        cnt_space = 0
        cnt_words = 0
        for i in range(len(text)):
            if text[i] == ' ':
                cnt_space += 1
            elif cnt_words == 0 or text[i - 1] == ' ':
                cnt_words += 1
        # calculate extra space nad calculate avg space
        extra_space = 0
        avg_space = 0
        if cnt_words < 2:
            extra_space = cnt_space
        else:
            extra_space = cnt_space % (cnt_words - 1)
            avg_space = cnt_space // (cnt_words - 1)
        
        #
        result = ''
        word_appeared = False
        for i in range(len(text)):
            if text[i] != ' ':
                if word_appeared and text[i - 1] == ' ':
                    result += ' ' * avg_space
                word_appeared = True
                result += text[i]
                
                
        result += ' ' * extra_space
        return result

class Solution:
    def reorderSpaces(self, text: str) -> str:
        words = text.split() # split(sep=None) will discard empty strings.
        cnt = len(words)
        spaces = text.count(' ')
        gap = 0 if cnt == 1 else spaces // (cnt - 1)
        trailing_spaces = spaces if gap == 0 else spaces % (cnt - 1)
        return (' ' * gap).join(words) + ' ' * trailing_spaces

class Solution:
    def reorderSpaces(self, text: str) -> str:
        wds = text.split()
        sc = text.count(' ')
        if len(wds) == 1:
            return text.strip(' ')+' '*sc
        nwds = len(wds)
        s = nwds-1
        res = sc//s
        rem = sc-res*s
        ans = ''
        for i in wds[:-1]:
            ans += i
            ans += ' '*res
        ans += wds[-1]
        ans += ' '*rem
        return ans