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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 91ms, memory 53.3MB, accepted 2025-12-31.
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)