Day 15 LeetCode
👨‍💻

Day 15 LeetCode

Tags
Data Structures
Python
Java
Computer Science
Published
Jan 24, 2021

Day 15 Task:

Solution:

class Solution:
    def reorderSpaces(self, text: str) -> str:
        
        space = 0
        for i in range(len(text)):
            if(text[i] == " "):
                space += 1
                
        words = text.split()
        ans = ""
        if(len(words) == 1):
            return words[0]+(" "*space)
        elif(space%(len(words)-1) == 0):
            for i in range(len(words)-1):
                ans += words[i]
                ans += " "*int(space/(len(words)-1))
            ans += words[-1]
        else:
            extra = space%(len(words)-1)
            for i in range(len(words)-1):
                ans += words[i]
                ans += " "*int(space/(len(words)-1))
            ans += words[-1]
            ans += " "*int(extra)
        return ans