{"id":9184,"date":"2021-12-20T18:46:34","date_gmt":"2021-12-21T02:46:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9184"},"modified":"2021-12-20T18:52:10","modified_gmt":"2021-12-21T02:52:10","slug":"leetcode-2109-adding-spaces-to-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2109-adding-spaces-to-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2109. Adding Spaces to a String"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>s<\/code>&nbsp;and a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>spaces<\/code>&nbsp;that describes the indices in the original string where spaces will be added. Each space should be inserted&nbsp;<strong>before<\/strong>&nbsp;the character at the given index.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, given&nbsp;<code>s = \"EnjoyYourCoffee\"<\/code>&nbsp;and&nbsp;<code>spaces = [5, 9]<\/code>, we place spaces before&nbsp;<code>'Y'<\/code>&nbsp;and&nbsp;<code>'C'<\/code>, which are at indices&nbsp;<code>5<\/code>&nbsp;and&nbsp;<code>9<\/code>&nbsp;respectively. Thus, we obtain&nbsp;<code>\"Enjoy&nbsp;<strong><u>Y<\/u><\/strong>our&nbsp;<u><strong>C<\/strong><\/u>offee\"<\/code>.<\/li><\/ul>\n\n\n\n<p>Return<strong>&nbsp;<\/strong><em>the modified string&nbsp;<strong>after<\/strong>&nbsp;the spaces have been added.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"LeetcodeHelpsMeLearn\", spaces = [8,13,15]\n<strong>Output:<\/strong> \"Leetcode Helps Me Learn\"\n<strong>Explanation:<\/strong> \nThe indices 8, 13, and 15 correspond to the underlined characters in \"Leetcode<strong>H<\/strong>elps<strong>M<\/strong>e<strong>L<\/strong>earn\".\nWe then place spaces before those characters.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"icodeinpython\", spaces = [1,5,7,9]\n<strong>Output:<\/strong> \"i code in py thon\"\n<strong>Explanation:<\/strong>\nThe indices 1, 5, 7, and 9 correspond to the underlined characters in \"i<strong>c<\/strong>ode<strong>i<\/strong>n<strong>p<\/strong>y<strong>t<\/strong>hon\".\nWe then place spaces before those characters.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"spacing\", spaces = [0,1,2,3,4,5,6]\n<strong>Output:<\/strong> \" s p a c i n g\"\n<strong>Explanation:<\/strong>\nWe are also able to place spaces before the first character of the string.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists only of lowercase and uppercase English letters.<\/li><li><code>1 &lt;= spaces.length &lt;= 3 * 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= spaces[i] &lt;= s.length - 1<\/code><\/li><li>All the values of&nbsp;<code>spaces<\/code>&nbsp;are&nbsp;<strong>strictly increasing<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Scan orignal string \/ Two Pointers<\/strong><\/h2>\n\n\n\n<p>Just scan the original string and insert a space if the current index matched the front space index.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(m+n) \/ O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string addSpaces(string s, vector<int>& spaces) {\n    const int m = spaces.size();\n    string ans;\n    for (int i = 0, j = 0; i < s.length(); ++i) {\n      if (j < m &#038;&#038; i == spaces[j]) {\n        ans += \" \";\n        ++j;\n      }\n      ans += s[i];      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;string&nbsp;s&nbsp;and a&nbsp;0-indexed&nbsp;integer array&nbsp;spaces&nbsp;that describes the indices in the original string where spaces will be added. Each space should be inserted&nbsp;before&nbsp;the character at&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[177,179,4],"class_list":["post-9184","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9184"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9184\/revisions"}],"predecessor-version":[{"id":9186,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9184\/revisions\/9186"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}