> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Adding Spaces to a String Python Solution

> Tested Python solution for LeetCode 2109 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2109, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/adding-spaces-to-a-string/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2109   # by problem number
lcpy gen -s adding_spaces_to_a_string   # by problem name
```

## Problem

You are given a **0-indexed** string `s` and a **0-indexed** integer array `spaces` that describes the indices in the original string where spaces will be added. Each space should be inserted **before** the character at the given index.

* For example, given `s = "EnjoyYourCoffee"` and `spaces = [5, 9]`, we place spaces before `'Y'` and `'C'`, which are at indices `5` and `9` respectively. Thus, we obtain `"Enjoy Your Coffee"`.

Return the modified string after the spaces have been added.

### Examples

```
Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
Output: "Leetcode Helps Me Learn"
```

**Explanation:** The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn". We then place spaces before those characters.

```
Input: s = "icodeinpython", spaces = [1,5,7,9]
Output: "i code in py thon"
```

**Explanation:** The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython". We then place spaces before those characters.

```
Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
Output: " s p a c i n g"
```

**Explanation:** We are also able to place spaces before the first character of the string.

### Constraints

* `1 <= s.length <= 3 * 10^5`
* `s` consists only of lowercase and uppercase English letters.
* `1 <= spaces.length <= 3 * 10^5`
* `0 <= spaces[i] <= s.length - 1`
* All the values of `spaces` are **strictly increasing**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/adding_spaces_to_a_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/adding_spaces_to_a_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) where n = len(s), m = len(spaces)
    # Space: O(n + m) for the output
    def add_spaces(self, s: str, spaces: list[int]) -> str:
        parts: list[str] = []
        prev = 0
        for idx in spaces:
            parts.append(s[prev:idx])
            prev = idx
        parts.append(s[prev:])
        return " ".join(parts)
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where n = len(s), m = len(spaces) | O(n + m) for the output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.