> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/2109.-adding-spaces-to-a-string.md).

# 2109. Adding Spaces to a String

T: O(s len+ space array size)

S: O(s len+ space array size)

```java
class Solution {
    public String addSpaces(String s, int[] spaces) {
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for (int space : spaces) {
            sb.append(s.substring(start, space)).append(" ");
            start = space;
        }
        sb.append(s.substring(start, s.length()));
        return sb.toString();
    }
}ja
```
