Day 15 Task:
3. Rearrange Spaces Between Words: https://leetcode.com/problems/rearrange-spaces-between-words/
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