<?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>join Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/join/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/join/</link>
	<description></description>
	<lastBuildDate>Tue, 06 Apr 2021 23:55:40 +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>join Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/join/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1816. Truncate Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Apr 2021 23:55:15 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8320</guid>

					<description><![CDATA[<p>A&#160;sentence&#160;is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of&#160;only&#160;uppercase and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/">花花酱 LeetCode 1816. Truncate 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>A&nbsp;<strong>sentence</strong>&nbsp;is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of&nbsp;<strong>only</strong>&nbsp;uppercase and lowercase English letters (no punctuation).</p>



<ul><li>For example,&nbsp;<code>"Hello World"</code>,&nbsp;<code>"HELLO"</code>, and&nbsp;<code>"hello world hello world"</code>&nbsp;are all sentences.</li></ul>



<p>You are given a sentence&nbsp;<code>s</code>​​​​​​ and an integer&nbsp;<code>k</code>​​​​​​. You want to&nbsp;<strong>truncate</strong>&nbsp;<code>s</code>​​​​​​ such that it contains only the&nbsp;<strong>first</strong>&nbsp;<code>k</code>​​​​​​ words. Return&nbsp;<code>s</code>​​​​<em>​​ after&nbsp;<strong>truncating</strong>&nbsp;it.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "Hello how are you Contestant", k = 4
<strong>Output:</strong> "Hello how are you"
<strong>Explanation:</strong>
The words in s are ["Hello", "how" "are", "you", "Contestant"].
The first 4 words are ["Hello", "how", "are", "you"].
Hence, you should return "Hello how are you".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "What is the solution to this problem", k = 4
<strong>Output:</strong> "What is the solution"
<strong>Explanation:</strong>
The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
The first 4 words are ["What", "is", "the", "solution"].
Hence, you should return "What is the solution".</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "chopper is not a tanuki", k = 5
<strong>Output:</strong> "chopper is not a tanuki"
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 500</code></li><li><code>k</code>&nbsp;is in the range&nbsp;<code>[1, the number of words in s]</code>.</li><li><code>s</code>&nbsp;consist of only lowercase and uppercase English letters and spaces.</li><li>The words in&nbsp;<code>s</code>&nbsp;are separated by a single space.</li><li>There are no leading or trailing spaces.</li></ul>



<h2><strong>Solution: </strong></h2>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  string truncateSentence(string s, int k) {
    string ans;
    stringstream ss(s);
    for (int i = 0; i &lt; k &amp;&amp; ss; ++i) {
      string word;
      ss &gt;&gt; word;
      ans += (ans.empty() ? &quot;&quot; : &quot; &quot;) + word;
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def truncateSentence(self, s: str, k: int) -&gt; str:
    return &quot; &quot;.join(s.split()[:k])</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1816-truncate-sentence/">花花酱 LeetCode 1816. Truncate 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-1816-truncate-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
