2109. Adding Spaces to a String  

Simple two-pointer.

Complexity

  • Time complexity: $O(N + M)$
  • Space complexity: $O(1)$

Code

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