{"id":2760,"date":"2018-04-24T00:45:28","date_gmt":"2018-04-24T07:45:28","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2760"},"modified":"2018-04-24T00:47:40","modified_gmt":"2018-04-24T07:47:40","slug":"leetcode-820-short-encoding-of-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-820-short-encoding-of-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 820. Short Encoding of Words"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a list of words, we may encode it by writing a reference string\u00a0<code>S<\/code>\u00a0and a list of indexes\u00a0<code>A<\/code>.<\/p>\n<p>For example, if the list of words is\u00a0<code>[\"time\", \"me\", \"bell\"]<\/code>, we can write it as\u00a0<code>S = \"time#bell#\"<\/code>\u00a0and\u00a0<code>indexes = [0, 2, 5]<\/code>.<\/p>\n<p>Then for each index, we will recover the word by reading from the reference string from that index until we reach a &#8220;#&#8221; character.<\/p>\n<p>What is the length of the shortest reference string S possible that encodes the given words?<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> words = <code>[\"time\", \"me\", \"bell\"]<\/code> <strong>Output:<\/strong> 10 <strong>Explanation:<\/strong> S = <code>\"time#bell#\" and indexes = [0, 2, 5<\/code>].<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= words.length\u00a0&lt;= 2000.<\/code><\/li>\n<li><code>1 &lt;=\u00a0words[i].length\u00a0&lt;= 7.<\/code><\/li>\n<li>Each word\u00a0has only\u00a0lowercase letters.<\/li>\n<\/ol>\n<h1><strong>Idea<\/strong><\/h1>\n<p>Remove all the words that are suffix of other words.<\/p>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n*l^2)<\/p>\n<p>Space complexity: O(n*l)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 43 ms\r\nclass Solution {\r\npublic:\r\n  int minimumLengthEncoding(vector&lt;string&gt;&amp; words) {\r\n    unordered_set&lt;string&gt; s(words.begin(), words.end());\r\n    for (const string&amp; w : words) {\r\n      for (int i = w.length() - 1; i &gt; 0; --i)        \r\n        s.erase(w.substr(i));\r\n    }\r\n    int ans = 0;\r\n    for (const string&amp; w : s)\r\n      ans += w.length() + 1;\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a list of words, we may encode it by writing a reference string\u00a0S\u00a0and a list of indexes\u00a0A. For example, if the list of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[299,4,186],"class_list":["post-2760","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-hashset","tag-string","tag-suffix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2760","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=2760"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2760\/revisions"}],"predecessor-version":[{"id":2765,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2760\/revisions\/2765"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}