{"id":4415,"date":"2018-12-09T01:15:20","date_gmt":"2018-12-09T09:15:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4415"},"modified":"2018-12-09T01:24:59","modified_gmt":"2018-12-09T09:24:59","slug":"leetcode-953-verifying-an-alien-dictionary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-953-verifying-an-alien-dictionary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 953. Verifying an Alien Dictionary"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>In an alien language, surprisingly they also use english lowercase letters, but possibly\u00a0in a different\u00a0<code>order<\/code>. The\u00a0<code>order<\/code>\u00a0of the alphabet\u00a0is some permutation\u00a0of lowercase letters.<\/p>\n<p>Given a sequence of\u00a0<code>words<\/code>\u00a0written in the alien language,\u00a0and the\u00a0<code>order<\/code>\u00a0of the alphabet,\u00a0return\u00a0<code>true<\/code>\u00a0if and only if the given\u00a0<code>words<\/code>\u00a0are sorted lexicographicaly in this alien language.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>words = <span id=\"example-input-1-1\">[\"hello\",\"leetcode\"]<\/span>, order = <span id=\"example-input-1-2\">\"hlabcdefgijkmnopqrstuvwxyz\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">true<\/span>\r\n<strong>Explanation: <\/strong><span id=\"example-output-1\">As 'h' comes before 'l' in this language, then the sequence is sorted.<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>words = <span id=\"example-input-2-1\">[\"word\",\"world\",\"row\"]<\/span>, order = <span id=\"example-input-2-2\">\"worldabcefghijkmnpqstuvxyz\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">false<\/span>\r\n<strong>Explanation: <\/strong><span id=\"example-output-1\">As 'd' comes after 'l' in this language, then words[0] &gt; words[1], hence the sequence is unsorted.<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>words = <span id=\"example-input-3-1\">[\"apple\",\"app\"]<\/span>, order = <span id=\"example-input-3-2\">\"abcdefghijklmnopqrstuvwxyz\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">false\r\n<\/span><strong>Explanation: <\/strong>The first three characters \"app\" match, and the second string is shorter (in size.) According to lexicographical rules \"apple\" &gt; \"app\", because 'l' &gt; '\u2205', where '\u2205' is defined as the blank character which is less than any other character (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lexicographical_order\" target=\"_blank\" rel=\"noopener\">More info<\/a>).\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= words.length &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= words[i].length &lt;= 20<\/code><\/li>\n<li><code>order.length == 26<\/code><\/li>\n<li>All characters in\u00a0<code>words[i]<\/code>\u00a0and\u00a0<code>order<\/code>\u00a0are english lowercase letters.<\/li>\n<\/ol>\n<h1><strong>Solution: Hashtable<\/strong><\/h1>\n<p>Time complexity: O(sum(len(words[i])))<\/p>\n<p>Space complexity: O(26)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 8 ms\r\nclass Solution {\r\npublic:\r\n  bool isAlienSorted(vector&lt;string&gt;&amp; words, string order) {\r\n    vector&lt;char&gt; m(26);\r\n    for (int i = 0; i &lt; 26; ++i)\r\n      m[order[i] - 'a'] = 'a' + i;\r\n    for (int i = 0; i &lt; words.size(); ++i) {      \r\n      for (int j = 0; j &lt; words[i].length(); ++j)\r\n        words[i][j] = m[words[i][j] - 'a'];      \r\n      if (i &gt; 0 &amp;&amp; words[i] &lt; words[i - 1]) return false;      \r\n    }\r\n    return true;        \r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem In an alien language, surprisingly they also use english lowercase letters, but possibly\u00a0in a different\u00a0order. The\u00a0order\u00a0of the alphabet\u00a0is some permutation\u00a0of lowercase letters. Given a&#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],"tags":[222,82,218,4],"class_list":["post-4415","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-mapping","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4415","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=4415"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4415\/revisions"}],"predecessor-version":[{"id":4419,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4415\/revisions\/4419"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}