|
| 1 | +<h2><a href="https://leetcode.com/problems/adding-spaces-to-a-string/">2109. Adding Spaces to a String</a></h2><h3>Medium</h3><hr><div><p>You are given a <strong>0-indexed</strong> string <code>s</code> and a <strong>0-indexed</strong> integer array <code>spaces</code> that describes the indices in the original string where spaces will be added. Each space should be inserted <strong>before</strong> the character at the given index.</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>For example, given <code>s = "EnjoyYourCoffee"</code> and <code>spaces = [5, 9]</code>, we place spaces before <code>'Y'</code> and <code>'C'</code>, which are at indices <code>5</code> and <code>9</code> respectively. Thus, we obtain <code>"Enjoy <strong><u>Y</u></strong>our <u><strong>C</strong></u>offee"</code>.</li> |
| 5 | +</ul> |
| 6 | + |
| 7 | +<p>Return<strong> </strong><em>the modified string <strong>after</strong> the spaces have been added.</em></p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> s = "LeetcodeHelpsMeLearn", spaces = [8,13,15] |
| 13 | +<strong>Output:</strong> "Leetcode Helps Me Learn" |
| 14 | +<strong>Explanation:</strong> |
| 15 | +The indices 8, 13, and 15 correspond to the underlined characters in "Leetcode<u><strong>H</strong></u>elps<u><strong>M</strong></u>e<u><strong>L</strong></u>earn". |
| 16 | +We then place spaces before those characters. |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | + |
| 21 | +<pre><strong>Input:</strong> s = "icodeinpython", spaces = [1,5,7,9] |
| 22 | +<strong>Output:</strong> "i code in py thon" |
| 23 | +<strong>Explanation:</strong> |
| 24 | +The indices 1, 5, 7, and 9 correspond to the underlined characters in "i<u><strong>c</strong></u>ode<u><strong>i</strong></u>n<u><strong>p</strong></u>y<u><strong>t</strong></u>hon". |
| 25 | +We then place spaces before those characters. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 3:</strong></p> |
| 29 | + |
| 30 | +<pre><strong>Input:</strong> s = "spacing", spaces = [0,1,2,3,4,5,6] |
| 31 | +<strong>Output:</strong> " s p a c i n g" |
| 32 | +<strong>Explanation:</strong> |
| 33 | +We are also able to place spaces before the first character of the string. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li> |
| 41 | + <li><code>s</code> consists only of lowercase and uppercase English letters.</li> |
| 42 | + <li><code>1 <= spaces.length <= 3 * 10<sup>5</sup></code></li> |
| 43 | + <li><code>0 <= spaces[i] <= s.length - 1</code></li> |
| 44 | + <li>All the values of <code>spaces</code> are <strong>strictly increasing</strong>.</li> |
| 45 | +</ul> |
| 46 | +</div> |
0 commit comments