1592. Rearrange Spaces Between Words

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1592

class Solution:
    def reorderSpaces(self, text: str) -> str:
        cnt = text.count(' ')
        words = text.split()
        m = len(words) - 1
        if m == 0:
            return words[0] + ' ' * cnt
        return (' ' * (cnt // m)).join(words) + ' ' * (cnt % m)