adding-spaces-to-a-string

December 30, 2022

array-and-hashmap

Problem URL: adding-spaces-to-a-string

We will iterate over the string and add the spaces to the result.

class Solution:
    def addSpaces(self, s: str, spaces: List[int]) -> str:
        spaces = set(spaces)
        res = ''
        for i in range(len(s)):
            if i in spaces:
                res += ' ' + s[i]
            else:
                res += s[i]
        return res

Time complexity: O(n)
Space complexity: O(n)