{"id":2570,"date":"2018-05-24T21:29:47","date_gmt":"2018-05-24T13:29:47","guid":{"rendered":"https:\/\/boweihe.me\/?p=2570"},"modified":"2018-05-24T21:29:47","modified_gmt":"2018-05-24T13:29:47","slug":"leetcode-269-alien-dictionary","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2570","title":{"rendered":"LeetCode 269. Alien Dictionary"},"content":{"rendered":"<div class=\"question-description__2cX5\">\n<div>\nThere is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of\u00a0<b>non-empty<\/b>\u00a0words from the dictionary, where\u00a0<b>words are sorted lexicographically by the rules of this new language<\/b>. Derive the order of letters in this language.<br \/>\n<b>Example 1:<\/b><\/p>\n<pre class=\"lang:default decode:true\">Input:\n[\n  \"wrt\",\n  \"wrf\",\n  \"er\",\n  \"ett\",\n  \"rftt\"\n]\nOutput:\"wertf\"<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"lang:default decode:true\">[\n  \"z\",\n  \"x\"\n]\n&gt;Output: \"zx\"<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"lang:default decode:true\">[\n  \"z\",\n  \"x\",\n  \"z\"\n]\nOutput:\"\"<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>You may assume all letters are in lowercase.<\/li>\n<li>You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.<\/li>\n<li>If the order is invalid, return an empty string.<\/li>\n<li>There may be multiple valid order of letters, return any one of them is fine.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h1>Solution:<\/h1>\n<p>The solution is to build a directed map and find the topological ordering of the characters. Once the map is built, we can always find the &#8220;terminal&#8221; character which has no succeeding, i.e. the out-degree of that node would be zero. The we remove the character (the node and its edges) from the map and try to find the next such character, so on so forth.<\/p>\n<pre class=\"lang:default decode:true\">[\n  \"wrt\",\n  \"wrf\",\n  \"er\",\n  \"ett\",\n  \"rftt\"\n]<\/pre>\n<p>For example,\u00a0 the map built will be:<\/p>\n<pre class=\"lang:default decode:true \">t: {f}\nw: {e}\nr: {t}\ne: {r}\nf: {}<\/pre>\n<pre class=\"lang:c++ decode:true\">class Solution {\nprivate:\n    map&lt;char, set&lt;char&gt;&gt; ch_map;\n    void build(const string&amp; w1, const string&amp; w2)\n    {\n        const int len = std::min(w1.size(), w2.size());\n        int i = 0;\n        for (i = 0; i &lt; len; ++i)\n        {\n            if (ch_map.count(w1[i]) == 0)\n                ch_map[w1[i]] = set&lt;char&gt;();\n            if (ch_map.count(w2[i]) == 0)\n                ch_map[w2[i]] = set&lt;char&gt;();\n            if (w1[i] != w2[i])\n            {\n                ch_map[w1[i]].insert(w2[i]);\n                break;\n            }\n        }\n        for (; i &lt; w1.size() || i &lt; w2.size(); ++i)\n        {\n            if (i &lt; w1.size() &amp;&amp; ch_map.count(w1[i]) == 0)\n                ch_map[w1[i]] = set&lt;char&gt;();\n            if (i &lt; w2.size() &amp;&amp; ch_map.count(w2[i]) == 0)\n                ch_map[w2[i]] = set&lt;char&gt;();\n        }\n    }\npublic:\n    string alienOrder(vector&lt;string&gt;&amp; words) {\n        if (words.empty()) return string();\n        if (words.size() == 1) return words[0];\n        for (int i = 0; i &lt; words.size() - 1; ++i)\n        {\n            build(words[i], words[i+1]);\n        }\n        string result;\n        result.reserve(26);\n        while (!ch_map.empty())\n        {\n            bool found = false;\n            for (auto it : ch_map)\n            {\n                if (it.second.empty())\n                {\n                    found = true;\n                    result = it.first + result;\n                    break;\n                }\n            }\n            ch_map.erase(result[0]);\n            if (!found) return string();  \/\/not found\n            for (auto&amp; it : ch_map)\n            {\n                it.second.erase(result[0]);\n            }\n        }\n        return result;\n    }\n};<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of\u00a0non-empty\u00a0words from the dictionary, where\u00a0words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language. Example 1: Input: [ &#8220;wrt&#8221;, &#8220;wrf&#8221;, &#8220;er&#8221;, &#8220;ett&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[66],"class_list":["post-2570","post","type-post","status-publish","format-standard","hentry","category-technical","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2570"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2570\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}