{"id":2377,"date":"2018-03-24T19:56:48","date_gmt":"2018-03-25T02:56:48","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2377"},"modified":"2018-03-24T20:02:11","modified_gmt":"2018-03-25T03:02:11","slug":"leetcode-804-unique-morse-code-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-804-unique-morse-code-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 804. Unique Morse Code Words"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u5355\u8bcd\uff0c\u95ee\u8fd9\u4e9b\u5355\u8bcd\u7684\u83ab\u65af\u5bc6\u7801\u5171\u6709\u51e0\u79cd\u5f62\u5f0f\u3002<\/p>\n<p>International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:\u00a0<code>\"a\"<\/code>\u00a0maps to\u00a0<code>\".-\"<\/code>,\u00a0<code>\"b\"<\/code>\u00a0maps to\u00a0<code>\"-...\"<\/code>,\u00a0<code>\"c\"<\/code>\u00a0maps to\u00a0<code>\"-.-.\"<\/code>, and so on.<\/p>\n<p>For convenience, the full table for the 26 letters of the English alphabet is given below:<\/p>\n<pre class=\"crayon:false\">[\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"]<\/pre>\n<p>Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, &#8220;cab&#8221; can be written as &#8220;-.-.-&#8230;.-&#8220;, (which is the concatenation &#8220;-.-.&#8221; + &#8220;-&#8230;&#8221; + &#8220;.-&#8220;). We&#8217;ll call such a concatenation, the transformation\u00a0of a word.<\/p>\n<p>Return the number of different transformations among all words we have.<\/p>\n<pre class=\"crayon:false\"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> words = [\"gin\", \"zen\", \"gig\", \"msg\"]\r\n<strong>Output:<\/strong> 2\r\n<strong>Explanation: <\/strong>\r\nThe transformation of each word is:\r\n\"gin\" -&gt; \"--...-.\"\r\n\"zen\" -&gt; \"--...-.\"\r\n\"gig\" -&gt; \"--...--.\"\r\n\"msg\" -&gt; \"--...--.\"\r\n\r\nThere are 2 different transformations, \"--...-.\" and \"--...--.\".\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The length of\u00a0<code>words<\/code>\u00a0will be at most\u00a0<code>100<\/code>.<\/li>\n<li>Each\u00a0<code>words[i]<\/code>\u00a0will have length in range\u00a0<code>[1, 12]<\/code>.<\/li>\n<li><code>words[i]<\/code>\u00a0will only consist of lowercase letters.<\/li>\n<\/ul>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>Time Complexity: O(n)<\/p>\n<p>Space Complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int uniqueMorseRepresentations(vector&lt;string&gt;&amp; words) {\r\n    vector&lt;string&gt; m{\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"};\r\n    unordered_set&lt;string&gt; s;\r\n    for (const string&amp; word : words) {\r\n      string code;\r\n      for (char c : word)\r\n        code += m[c - 'a'];\r\n      s.insert(code);\r\n    }\r\n    return s.size();\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u5355\u8bcd\uff0c\u95ee\u8fd9\u4e9b\u5355\u8bcd\u7684\u83ab\u65af\u5bc6\u7801\u5171\u6709\u51e0\u79cd\u5f62\u5f0f\u3002 International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:\u00a0&#8220;a&#8221;\u00a0maps to\u00a0&#8220;.-&#8220;,\u00a0&#8220;b&#8221;\u00a0maps to\u00a0&#8220;-&#8230;&#8221;,\u00a0&#8220;c&#8221;\u00a0maps&#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":[222,82,4],"class_list":["post-2377","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2377","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=2377"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2377\/revisions"}],"predecessor-version":[{"id":2385,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2377\/revisions\/2385"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}