2109. Adding Spaces to a String

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

On This Page

LeetCode problem 2109

class Solution:
    def addSpaces(self, s: str, spaces: List[int]) -> str:
        res = []
        i, j = len(s) - 1, len(spaces) - 1
        while i >= 0:
            res.append(s[i])
            if j >= 0 and i == spaces[j]:
                res.append(' ')
                j -= 1
            i -= 1
        return ''.join(res[::-1])