{"id":7399,"date":"2020-09-20T23:54:11","date_gmt":"2020-09-21T06:54:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7399"},"modified":"2020-09-20T23:54:59","modified_gmt":"2020-09-21T06:54:59","slug":"leetcode-1592-rearrange-spaces-between-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1592-rearrange-spaces-between-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1592. Rearrange Spaces Between Words"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>text<\/code>&nbsp;of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It&#8217;s guaranteed that&nbsp;<code>text<\/code>&nbsp;<strong>contains at least one word<\/strong>.<\/p>\n\n\n\n<p>Rearrange the spaces so that there is an&nbsp;<strong>equal<\/strong>&nbsp;number of spaces between every pair of adjacent words and that number is&nbsp;<strong>maximized<\/strong>. If you cannot redistribute all the spaces equally, place the&nbsp;<strong>extra spaces at the end<\/strong>, meaning the returned string should be the same length as&nbsp;<code>text<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the string after rearranging the spaces<\/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> text = \"  this   is  a sentence \"\n<strong>Output:<\/strong> \"this   is   a   sentence\"\n<strong>Explanation: <\/strong>There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 \/ (4-1) = 3 spaces.\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> text = \" practice   makes   perfect\"\n<strong>Output:<\/strong> \"practice   makes   perfect \"\n<strong>Explanation:<\/strong>&nbsp;There are a total of 7 spaces and 3 words. 7 \/ (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.\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> text = \"hello   world\"\n<strong>Output:<\/strong> \"hello   world\"\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text = \"  walks  udp package   into  bar a\"\n<strong>Output:<\/strong> \"walks  udp  package  into  bar  a \"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text = \"a\"\n<strong>Output:<\/strong> \"a\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= text.length &lt;= 100<\/code><\/li><li><code>text<\/code>&nbsp;consists of lowercase English letters and&nbsp;<code>' '<\/code>.<\/li><li><code>text<\/code>&nbsp;contains at least one word.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; 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 reorderSpaces(string text) {\n    vector<string> words;\n    string word;\n    int t = 0;\n    for (char c : text) {\n      if (c == ' ') {\n        ++t;\n        if (!word.empty()) {      \n          words.push_back(word);\n          word.clear();\n        }\n      } else {\n        word += c;\n      }\n    }\n    if (!word.empty()) words.push_back(word);\n    const int n = words.size();    \n    if (n == 1) return words[0] + string(t, ' ');\n    const int s = t \/ (n - 1);\n    const int r = t % (n - 1);\n    string ans;\n    for (int i = 0; i < words.size(); ++i) {\n      if (i) ans.append(s, ' ');\n      ans += words[i];\n    }\n    ans.append(r, ' ');\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;text&nbsp;of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and&#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":[222,4],"class_list":["post-7399","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7399","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=7399"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7399\/revisions"}],"predecessor-version":[{"id":7401,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7399\/revisions\/7401"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}