{"id":8456,"date":"2021-05-29T15:47:12","date_gmt":"2021-05-29T22:47:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8456"},"modified":"2021-05-29T15:47:52","modified_gmt":"2021-05-29T22:47:52","slug":"leetcode-1859-sorting-the-sentence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1859-sorting-the-sentence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1859. Sorting the 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 word consists of lowercase and uppercase English letters.<\/p>\n\n\n\n<p>A sentence can be&nbsp;<strong>shuffled<\/strong>&nbsp;by appending the&nbsp;<strong>1-indexed word position<\/strong>&nbsp;to each word then rearranging the words in the sentence.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the sentence&nbsp;<code>\"This is a sentence\"<\/code>&nbsp;can be shuffled as&nbsp;<code>\"sentence4 a3 is2 This1\"<\/code>&nbsp;or&nbsp;<code>\"is2 sentence4 This1 a3\"<\/code>.<\/li><\/ul>\n\n\n\n<p>Given a&nbsp;<strong>shuffled sentence<\/strong>&nbsp;<code>s<\/code>&nbsp;containing no more than&nbsp;<code>9<\/code>&nbsp;words, reconstruct and return&nbsp;<em>the original sentence<\/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 = \"is2 sentence4 This1 a3\"\n<strong>Output:<\/strong> \"This is a sentence\"\n<strong>Explanation:<\/strong> Sort the words in s to their original positions \"This1 is2 a3 sentence4\", then remove the numbers.\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 = \"Myself2 Me1 I4 and3\"\n<strong>Output:<\/strong> \"Me Myself and I\"\n<strong>Explanation:<\/strong> Sort the words in s to their original positions \"Me1 Myself2 and3 I4\", then remove the numbers.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 200<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase and uppercase English letters, spaces, and digits from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>9<\/code>.<\/li><li>The number of words in&nbsp;<code>s<\/code>&nbsp;is between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>9<\/code>.<\/li><li>The words in&nbsp;<code>s<\/code>&nbsp;are separated by a single space.<\/li><li><code>s<\/code>&nbsp;contains no leading or trailing spaces.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: String<\/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\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">class Solution:\n  def sortSentence(self, s: str) -> str:\n    p = [(w[:-1], int(w[-1])) for w in s.split()]\n    p.sort(key=lambda x : x[1])\n    return \" \".join((w for w, _ in p))\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 word consists of lowercase and uppercase&#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,23,4,168],"class_list":["post-8456","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-sort","tag-string","tag-words","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8456","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=8456"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8456\/revisions"}],"predecessor-version":[{"id":8458,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8456\/revisions\/8458"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}