java.lang.Object
g2101_2200.s2109_adding_spaces_to_a_string.Solution

public class Solution extends Object
2109 - Adding Spaces to a String.<p>Medium</p> <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> <ul> <li>For example, given <code>s = &quot;EnjoyYourCoffee&quot;</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>&quot;Enjoy **Y**our **C**offee&quot;</code>.</li> </ul> <p>Return <em>the modified string <strong>after</strong> the spaces have been added.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;LeetcodeHelpsMeLearn&rdquo;, spaces = [8,13,15]</p> <p><strong>Output:</strong> &ldquo;Leetcode Helps Me Learn&rdquo;</p> <p><strong>Explanation:</strong></p> <p>The indices 8, 13, and 15 correspond to the underlined characters in &ldquo;Leetcode<strong>H</strong>elps<strong>M</strong>e<strong>L</strong>earn&rdquo;.</p> <p>We then place spaces before those characters.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;icodeinpython&rdquo;, spaces = [1,5,7,9]</p> <p><strong>Output:</strong> &ldquo;i code in py thon&rdquo;</p> <p><strong>Explanation:</strong></p> <p>The indices 1, 5, 7, and 9 correspond to the underlined characters in &ldquo;i<strong>c</strong>ode<strong>i</strong>n<strong>p</strong>y<strong>t</strong>hon&rdquo;.</p> <p>We then place spaces before those characters.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;spacing&rdquo;, spaces = [0,1,2,3,4,5,6]</p> <p><strong>Output:</strong> &quot; s p a c i n g&quot;</p> <p><strong>Explanation:</strong> We are also able to place spaces before the first character of the string.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase and uppercase English letters.</li> <li><code>1 <= spaces.length <= 3 * 10<sup>5</sup></code></li> <li><code>0 <= spaces[i] <= s.length - 1</code></li> <li>All the values of <code>spaces</code> are <strong>strictly increasing</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • addSpaces

      public String addSpaces(String string, int[] spaces)