{"id":8320,"date":"2021-04-06T16:55:15","date_gmt":"2021-04-06T23:55:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8320"},"modified":"2021-04-06T16:55:40","modified_gmt":"2021-04-06T23:55:40","slug":"leetcode-1816-truncate-sentence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1816-truncate-sentence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1816. Truncate Sentence"},"content":{"rendered":"\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<p>You are given a sentence&nbsp;<code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b and an integer&nbsp;<code>k<\/code>\u200b\u200b\u200b\u200b\u200b\u200b. You want to&nbsp;<strong>truncate<\/strong>&nbsp;<code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b such that it contains only the&nbsp;<strong>first<\/strong>&nbsp;<code>k<\/code>\u200b\u200b\u200b\u200b\u200b\u200b words. Return&nbsp;<code>s<\/code>\u200b\u200b\u200b\u200b<em>\u200b\u200b after&nbsp;<strong>truncating<\/strong>&nbsp;it.<\/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 = \"Hello how are you Contestant\", k = 4\n<strong>Output:<\/strong> \"Hello how are you\"\n<strong>Explanation:<\/strong>\nThe words in s are [\"Hello\", \"how\" \"are\", \"you\", \"Contestant\"].\nThe first 4 words are [\"Hello\", \"how\", \"are\", \"you\"].\nHence, you should return \"Hello how are you\".\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 = \"What is the solution to this problem\", k = 4\n<strong>Output:<\/strong> \"What is the solution\"\n<strong>Explanation:<\/strong>\nThe words in s are [\"What\", \"is\" \"the\", \"solution\", \"to\", \"this\", \"problem\"].\nThe first 4 words are [\"What\", \"is\", \"the\", \"solution\"].\nHence, you should return \"What is the solution\".<\/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 = \"chopper is not a tanuki\", k = 5\n<strong>Output:<\/strong> \"chopper is not a tanuki\"\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;= 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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string truncateSentence(string s, int k) {\n    string ans;\n    stringstream ss(s);\n    for (int i = 0; i < k &#038;&#038; ss; ++i) {\n      string word;\n      ss >> word;\n      ans += (ans.empty() ? \"\" : \" \") + word;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def truncateSentence(self, s: str, k: int) -> str:\n    return \" \".join(s.split()[:k])\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;sentence&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;only&nbsp;uppercase 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,703,276,4],"class_list":["post-8320","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-join","tag-split","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8320","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=8320"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8320\/revisions"}],"predecessor-version":[{"id":8322,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8320\/revisions\/8322"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}