<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stable sort Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/stable-sort/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/stable-sort/</link>
	<description></description>
	<lastBuildDate>Sun, 17 May 2020 04:31:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>stable sort Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/stable-sort/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1451. Rearrange Words in a Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1451-rearrange-words-in-a-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1451-rearrange-words-in-a-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 04:31:04 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[stable sort]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6764</guid>

					<description><![CDATA[<p>Given a sentence&#160;text&#160;(A&#160;sentence&#160;is a string of space-separated words) in the following format: First letter is in upper case. Each word in&#160;text&#160;are separated by a single&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1451-rearrange-words-in-a-sentence/">花花酱 LeetCode 1451. Rearrange Words in a Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a sentence&nbsp;<code>text</code>&nbsp;(A&nbsp;<em>sentence</em>&nbsp;is a string of space-separated words) in the following format:</p>



<ul><li>First letter is in upper case.</li><li>Each word in&nbsp;<code>text</code>&nbsp;are separated by a single space.</li></ul>



<p>Your task is to rearrange the words in text such that&nbsp;all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order.</p>



<p>Return the new text&nbsp;following the format shown above.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "Leetcode is cool"
<strong>Output:</strong> "Is cool leetcode"
<strong>Explanation: </strong>There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4.
Output is ordered by length and the new first word starts with capital letter.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "Keep calm and code on"
<strong>Output:</strong> "On and keep calm code"
<strong>Explanation: </strong>Output is ordered as follows:
"On" 2 letters.
"and" 3 letters.
"keep" 4 letters in case of tie order by position in original text.
"calm" 4 letters.
"code" 4 letters.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "To be or not to be"
<strong>Output:</strong> "To be or to be not"
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>text</code>&nbsp;begins with a capital letter and then contains lowercase letters and single space between words.</li><li><code>1 &lt;= text.length &lt;= 10^5</code></li></ul>



<h2><strong>Solution: Stable sort</strong></h2>



<p>Time complexity: O(nlogn)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string arrangeWords(string text) {
    vector&lt;string&gt; words{&quot;&quot;};
    for (char c : text) {
      if (c == ' ') 
        words.push_back(&quot;&quot;);
      else
        words.back() += c;
    }
    
    words[0][0] = tolower(words[0][0]);
    
    stable_sort(begin(words), end(words), [](const auto&amp; w1, const auto&amp; w2){
      return w1.length() &lt; w2.length();
    });
    
    string ans;
    for (const string&amp; word : words) {
      if (!ans.empty()) ans += ' ';
      ans += word;
    }
    ans[0] = toupper(ans[0]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1451-rearrange-words-in-a-sentence/">花花酱 LeetCode 1451. Rearrange Words in a Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-1451-rearrange-words-in-a-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
