LeetCode solutions

2232. Adding Spaces to a String

My accepted Python solution to LeetCode problem 2232, Adding Spaces to a String, running in 91ms.

  • Difficulty: Medium
  • Python
  • Runtime 91ms
  • Memory 53.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 91ms, memory 53.3MB, accepted 2025-12-31.

python
class Solution:
    def addSpaces(self, s: str, spaces: List[int]) -> str:
        result = []
        space_set = set(spaces)
        for i, c in enumerate(s):
            if i in space_set:
                result.append(' ')
            result.append(c)
        return ''.join(result)

Source