{"id":323,"date":"2020-06-20T17:20:18","date_gmt":"2020-06-20T17:20:18","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=323"},"modified":"2020-06-20T22:10:12","modified_gmt":"2020-06-20T22:10:12","slug":"leetcode-269-alien-dictionary-solution","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/","title":{"rendered":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5"},"content":{"rendered":"\n<p>\u8fd9\u9898\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u62d3\u6251\u6392\u5e8ftopological sort\uff0c\u9700\u8981\u5224\u65ad\u6709\u6ca1\u6709\u73af\u6765\u5904\u7406\u65e0\u6548input\uff0c\u96be\u5ea6\u4e3b\u8981\u6765\u81ea\u8fd9\u90e8\u5206\u3002Topological sort\u53ef\u4ee5\u7528dfs\u89e3\u51b3\uff0c\u4f46\u662f\u6ca1\u7ec3\u4e60\u8fc7\u8fd8\u4e0d\u4e0d\u5bb9\u6613\u5199\u5bf9\u7684\u3002\u5148\u4e0a\u4e00\u4e2a\u6211\u6bd4\u8f83\u6ee1\u610f\u7684python\u89e3\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def alienOrder(self, words: List[str]) -> str:\n        edges = {c: [] for word in words for c in word}\n        for word1, word2 in zip(words, words[1:]):\n            if len(word1) > len(word2) and word1.startswith(word2):\n                return ''\n            for c1, c2 in zip(word1, word2):\n                if c1 != c2:\n                    edges[c1].append(c2)\n                    break\n        \n        seen = {}\n        order = []\n        def dfs(start):\n            if start in seen:\n                return seen[start]\n            seen[start] = False\n            for nxt in edges[start]:\n                if not dfs(nxt):\n                    return False\n            order.insert(0, start)\n            seen[start] = True\n            return True\n        \n        if not all(dfs(start) for start in edges):\n            return ''\n        return ''.join(order)<\/code><\/pre>\n\n\n\n<p>\u603b\u7ed3\u4e00\u4e0b\u8981\u70b9\u3002<br>1.     edges = {c: [] for word in words for c in word} \u8fd9\u79cd\u4e00\u884c\u4e24\u4e2a\u5faa\u73af\u5efamap\u7684\u65b9\u5f0f\u6211\u4e5f\u662f\u65b0\u5b66\u7684\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u4e2amap key\u662f\u6392\u524d\u9762\u7684\u5b57\u6bcd\uff0cvalue\u662f\u6392\u540e\u9762\u7684\u5b57\u6bcd\u3002\u800c\u4e14\u4e0d\u7ba1\u6709\u6ca1\u6709\u540e\u9762\u7684\uff0ckey\u90fd\u52a0\u4e0a\u4e86\uff0c\u8fd9\u6837\u7701\u4e86\u53e6\u4e00\u4e2a\u53d8\u91cf\u5b58\u5b57\u6bcd\u8868\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u6211\u4e4b\u524d\u4e00\u4e2a\u5199\u5bf9\u4e0d\u90a3\u4e48\u597d\u7684\u89e3\u6cd5\u3002<br>2. \u7528zip\u904d\u5386pair\u5f88\u597d\u7528\uff0c\u51cf\u5c11\u4e86\u51faindex bug\u7684\u53ef\u80fd<br>3. len(word1) > len(word2) and word1.startswith(word2)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684case\u3002\u6bd4\u5982abc\u6392\u5728ab\u524d\u9762\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u5b57\u6bcd\u8868\u987a\u5e8f\uff0c\u5c31\u662fc\u6392\u5728\u7a7a\u524d\u9762\uff0c\u90a3\u662f\u65e0\u6548\u7684\u3002<br>4. seen\u7684\u5229\u7528\uff0c\u8fd9\u662f\u6700\u5de7\u5999\u7684\u5730\u65b9\u3002\u5199\u4e86\u51e0\u79cd\u5c1d\u8bd5\uff0c\u53d1\u73b0\u73af\u4e0d\u592a\u5bb9\u6613\u3002\u5982\u679c\u53ea\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u5c31\u65e0\u6cd5\u533a\u5206\u662f\u4e00\u6761\u5df2\u7ecf\u8d70\u901a\u7684\u8def\u8fd8\u662f\u5f53\u524d\u8fd9\u6761\u8def\u53c8\u56de\u5230\u67d0\u4e2a\u4e4b\u524d\u7ecf\u8fc7\u7684\u4e00\u70b9\u3002\u89e3\u6cd5\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u53ea\u52a0\u4e0d\u51cf\uff0c\u53e6\u4e00\u4e2aset\u8bb0\u5f55\u5f53\u524d\u8fd9\u6761\u8def\u7684\u8def\u5f84\uff0c\u56de\u6eaf\u65f6\u8981\u66f4\u65b0\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd9\u79cd\u89e3\u6cd5\u5229\u7528\u4e86False\u548cTrue\u4e24\u79cdstates\uff0cFalse\u4ee3\u8868\u6b63\u5728\u8d70\u7684\u8def\uff0cTrue\u4ee3\u8868\u4ee5\u524d\u5df2\u7ecf\u8d70\u5230\u5e95\u80fd\u8d70\u901a\u7684\u8def\u3002\u975e\u5e38\u6df1\u523b\uff0c\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u7b97\u5927\u6982\u7406\u89e3\u3002<br>5. if not all(dfs(start) for start in edges)\uff0call\u662f\u4e2a\u6211\u65b0\u5b66\u7684function\uff0cinput\u662fcollection\u6216\u8005\u66f4\u51c6\u786e\u8bf4\u662f\u53ef\u4ee5iterable\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u8981\u6bcf\u4e2a\u5143\u7d20\u90fd\u662ftrue\u5c31\u662ftrue\u3002\u6362\u53e5\u8bdd\u8bf4\u5c31\u50cf\u5f00\u5173\u4e32\u8054\u3002\u672c\u6765\u6211\u5199\u5f97\u662fall([dfs(start) for start in edges])\uff0c\u540e\u6765\u53d1\u73b0\u4ee3\u8868list\u7684\u65b9\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\uff0c\u4f30\u8ba1\u662f\u4e9b\u8bed\u6cd5\u7cd6\u3002Python\u91cc\u9762\u786e\u5b9e\u6709\u5f88\u591a\u6211\u4e0d\u77e5\u9053\u7684\u8bed\u6cd5\u7cd6\u3002<br>6. dfs\u662f\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u4e86self\u548c\u7528\u5185\u51fd\u6570\u5916\u7684\u53d8\u91cf\u4fdd\u5b58\u72b6\u6001\uff0c\u4e0d\u7528\u6bcf\u4e2arecursion call\u90fdpass\u4e00\u6b21\u3002<\/p>\n\n\n\n<p>\u505a\u5b8c\u8fd9\u9898\u540e\uff0c\u91cd\u65b0\u505a<a href=\"http:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-207-course-schedule-solution\/?preview_id=334&amp;preview_nonce=af1424952c&amp;preview=true\">course schedule<\/a>\u5c31\u5bb9\u6613\u591a\u4e86\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6211\u4e4b\u524d\u5199\u5f97\u6bd4\u8f83\u957f\uff0c\u5f88\u5bb9\u6613\u51fabug\u7684\u7248\u672c\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def alienOrder(self, words):\n        \"\"\"\n        :type words: List[str]\n        :rtype: str\n        \"\"\"\n        if len(words) == 0:\n            return ''\n        if len(words) == 1:\n            return ''.join(set([c for c in words]))\n        edges = {}\n        voca = set()\n        for i in xrange(len(words) - 1):\n            for j in xrange(i + 1, len(words)):\n                if len(words[i]) > len(words[j]) and words[i].startswith(words[j]):\n                    return ''\n                self.addEdge(words[i], words[j], edges, voca)\n        \n        visited = set()\n        order = []\n        for c in voca:\n            # c = voca.pop()\n            this_visit = set()\n            noloop = self.dfs(c, edges, visited, this_visit, order)\n            if not noloop:\n                return ''\n        return ''.join(order)\n    \n    def dfs(self, start, edges, visited, this_visited, order):\n        if start in this_visited:\n            return False\n\n        if start in visited:\n            return True\n\n        visited.add(start)\n        this_visited.add(start)\n\n        if start not in edges:\n            order.insert(0, start)\n            this_visited.remove(start)\n            return True\n\n        for nxt in edges[start]:\n            noloop = self.dfs(nxt, edges, visited, this_visited, order)\n            if not noloop:\n                return False\n        order.insert(0, start)\n        this_visited.remove(start)\n        return True\n\n    def addEdge(self, word1, word2, edges, voca):\n        voca.update([c for c in word1])\n        voca.update([c for c in word2])\n        for i in xrange(min(len(word1), len(word2))):\n            if word1[i] != word2[i]:\n                if word1[i] in edges:\n                    edges[word1[i]].add(word2[i])\n                else:\n                    edges[word1[i]] = set([word2[i]])\n                break<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9898\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u62d3\u6251\u6392\u5e8ftopological sort\uff0c\u9700\u8981\u5224\u65ad\u6709\u6ca1\u6709\u73af\u6765\u5904\u7406\u65e0\u6548input\uff0c\u96be\u5ea6\u4e3b\u8981\u6765\u81ea\u8fd9\u90e8\u5206\u3002Topological sort\u53ef\u4ee5\u7528dfs\u89e3\u51b3\uff0c\u4f46\u662f\u6ca1\u7ec3\u4e60\u8fc7\u8fd8\u4e0d\u4e0d\u5bb9\u6613\u5199\u5bf9\u7684\u3002\u5148\u4e0a\u4e00\u4e2a\u6211\u6bd4\u8f83\u6ee1\u610f\u7684python\u89e3\u6cd5\u3002 \u603b\u7ed3\u4e00\u4e0b\u8981\u70b9\u30021. edges = {c: [] for word in words for c in word} \u8fd9\u79cd\u4e00\u884c\u4e24\u4e2a\u5faa\u73af\u5efamap\u7684\u65b9\u5f0f\u6211\u4e5f\u662f\u65b0\u5b66\u7684\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u4e2amap key\u662f\u6392\u524d\u9762\u7684\u5b57\u6bcd\uff0cvalue\u662f\u6392\u540e\u9762\u7684\u5b57\u6bcd\u3002\u800c\u4e14\u4e0d\u7ba1\u6709\u6ca1\u6709\u540e\u9762\u7684\uff0ckey\u90fd\u52a0\u4e0a\u4e86\uff0c\u8fd9\u6837\u7701\u4e86\u53e6\u4e00\u4e2a\u53d8\u91cf\u5b58\u5b57\u6bcd\u8868\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u6211\u4e4b\u524d\u4e00\u4e2a\u5199\u5bf9\u4e0d\u90a3\u4e48\u597d\u7684\u89e3\u6cd5\u30022. \u7528zip\u904d\u5386pair\u5f88\u597d\u7528\uff0c\u51cf\u5c11\u4e86\u51faindex bug\u7684\u53ef\u80fd3. len(word1) > len(word2) and word1.startswith(word2)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684case\u3002\u6bd4\u5982abc\u6392\u5728ab\u524d\u9762\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u5b57\u6bcd\u8868\u987a\u5e8f\uff0c\u5c31\u662fc\u6392\u5728\u7a7a\u524d\u9762\uff0c\u90a3\u662f\u65e0\u6548\u7684\u30024. seen\u7684\u5229\u7528\uff0c\u8fd9\u662f\u6700\u5de7\u5999\u7684\u5730\u65b9\u3002\u5199\u4e86\u51e0\u79cd\u5c1d\u8bd5\uff0c\u53d1\u73b0\u73af\u4e0d\u592a\u5bb9\u6613\u3002\u5982\u679c\u53ea\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u5c31\u65e0\u6cd5\u533a\u5206\u662f\u4e00\u6761\u5df2\u7ecf\u8d70\u901a\u7684\u8def\u8fd8\u662f\u5f53\u524d\u8fd9\u6761\u8def\u53c8\u56de\u5230\u67d0\u4e2a\u4e4b\u524d\u7ecf\u8fc7\u7684\u4e00\u70b9\u3002\u89e3\u6cd5\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u53ea\u52a0\u4e0d\u51cf\uff0c\u53e6\u4e00\u4e2aset\u8bb0\u5f55\u5f53\u524d\u8fd9\u6761\u8def\u7684\u8def\u5f84\uff0c\u56de\u6eaf\u65f6\u8981\u66f4\u65b0\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd9\u79cd\u89e3\u6cd5\u5229\u7528\u4e86False\u548cTrue\u4e24\u79cdstates\uff0cFalse\u4ee3\u8868\u6b63\u5728\u8d70\u7684\u8def\uff0cTrue\u4ee3\u8868\u4ee5\u524d\u5df2\u7ecf\u8d70\u5230\u5e95\u80fd\u8d70\u901a\u7684\u8def\u3002\u975e\u5e38\u6df1\u523b\uff0c\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u7b97\u5927\u6982\u7406\u89e3\u30025. if not all(dfs(start) for start in edges)\uff0call\u662f\u4e2a\u6211\u65b0\u5b66\u7684function\uff0cinput\u662fcollection\u6216\u8005\u66f4\u51c6\u786e\u8bf4\u662f\u53ef\u4ee5iterable\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u8981\u6bcf\u4e2a\u5143\u7d20\u90fd\u662ftrue\u5c31\u662ftrue\u3002\u6362\u53e5\u8bdd\u8bf4\u5c31\u50cf\u5f00\u5173\u4e32\u8054\u3002\u672c\u6765\u6211\u5199\u5f97\u662fall([dfs(start) for start in edges])\uff0c\u540e\u6765\u53d1\u73b0\u4ee3\u8868list\u7684\u65b9\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\uff0c\u4f30\u8ba1\u662f\u4e9b\u8bed\u6cd5\u7cd6\u3002Python\u91cc\u9762\u786e\u5b9e\u6709\u5f88\u591a\u6211\u4e0d\u77e5\u9053\u7684\u8bed\u6cd5\u7cd6\u30026. dfs\u662f\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u4e86self\u548c\u7528\u5185\u51fd\u6570\u5916\u7684\u53d8\u91cf\u4fdd\u5b58\u72b6\u6001\uff0c\u4e0d\u7528\u6bcf\u4e2arecursion call\u90fdpass\u4e00\u6b21\u3002 \u505a\u5b8c\u8fd9\u9898\u540e\uff0c\u91cd\u65b0\u505acourse&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u8fd9\u9898\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u62d3\u6251\u6392\u5e8ftopological sort\uff0c\u9700\u8981\u5224\u65ad\u6709\u6ca1\u6709\u73af\u6765\u5904\u7406\u65e0\u6548input\uff0c\u96be\u5ea6\u4e3b\u8981\u6765\u81ea\u8fd9\u90e8\u5206\u3002Topological sort\u53ef\u4ee5\u7528dfs\u89e3\u51b3\uff0c\u4f46\u662f\u6ca1\u7ec3\u4e60\u8fc7\u8fd8\u4e0d\u4e0d\u5bb9\u6613\u5199\u5bf9\u7684\u3002\u5148\u4e0a\u4e00\u4e2a\u6211\u6bd4\u8f83\u6ee1\u610f\u7684python\u89e3\u6cd5\u3002 \u603b\u7ed3\u4e00\u4e0b\u8981\u70b9\u30021. edges = {c: [] for word in words for c in word} \u8fd9\u79cd\u4e00\u884c\u4e24\u4e2a\u5faa\u73af\u5efamap\u7684\u65b9\u5f0f\u6211\u4e5f\u662f\u65b0\u5b66\u7684\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u4e2amap key\u662f\u6392\u524d\u9762\u7684\u5b57\u6bcd\uff0cvalue\u662f\u6392\u540e\u9762\u7684\u5b57\u6bcd\u3002\u800c\u4e14\u4e0d\u7ba1\u6709\u6ca1\u6709\u540e\u9762\u7684\uff0ckey\u90fd\u52a0\u4e0a\u4e86\uff0c\u8fd9\u6837\u7701\u4e86\u53e6\u4e00\u4e2a\u53d8\u91cf\u5b58\u5b57\u6bcd\u8868\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u6211\u4e4b\u524d\u4e00\u4e2a\u5199\u5bf9\u4e0d\u90a3\u4e48\u597d\u7684\u89e3\u6cd5\u30022. \u7528zip\u904d\u5386pair\u5f88\u597d\u7528\uff0c\u51cf\u5c11\u4e86\u51faindex bug\u7684\u53ef\u80fd3. len(word1) &gt; len(word2) and word1.startswith(word2)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684case\u3002\u6bd4\u5982abc\u6392\u5728ab\u524d\u9762\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u5b57\u6bcd\u8868\u987a\u5e8f\uff0c\u5c31\u662fc\u6392\u5728\u7a7a\u524d\u9762\uff0c\u90a3\u662f\u65e0\u6548\u7684\u30024. seen\u7684\u5229\u7528\uff0c\u8fd9\u662f\u6700\u5de7\u5999\u7684\u5730\u65b9\u3002\u5199\u4e86\u51e0\u79cd\u5c1d\u8bd5\uff0c\u53d1\u73b0\u73af\u4e0d\u592a\u5bb9\u6613\u3002\u5982\u679c\u53ea\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u5c31\u65e0\u6cd5\u533a\u5206\u662f\u4e00\u6761\u5df2\u7ecf\u8d70\u901a\u7684\u8def\u8fd8\u662f\u5f53\u524d\u8fd9\u6761\u8def\u53c8\u56de\u5230\u67d0\u4e2a\u4e4b\u524d\u7ecf\u8fc7\u7684\u4e00\u70b9\u3002\u89e3\u6cd5\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u53ea\u52a0\u4e0d\u51cf\uff0c\u53e6\u4e00\u4e2aset\u8bb0\u5f55\u5f53\u524d\u8fd9\u6761\u8def\u7684\u8def\u5f84\uff0c\u56de\u6eaf\u65f6\u8981\u66f4\u65b0\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd9\u79cd\u89e3\u6cd5\u5229\u7528\u4e86False\u548cTrue\u4e24\u79cdstates\uff0cFalse\u4ee3\u8868\u6b63\u5728\u8d70\u7684\u8def\uff0cTrue\u4ee3\u8868\u4ee5\u524d\u5df2\u7ecf\u8d70\u5230\u5e95\u80fd\u8d70\u901a\u7684\u8def\u3002\u975e\u5e38\u6df1\u523b\uff0c\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u7b97\u5927\u6982\u7406\u89e3\u30025. if not all(dfs(start) for start in edges)\uff0call\u662f\u4e2a\u6211\u65b0\u5b66\u7684function\uff0cinput\u662fcollection\u6216\u8005\u66f4\u51c6\u786e\u8bf4\u662f\u53ef\u4ee5iterable\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u8981\u6bcf\u4e2a\u5143\u7d20\u90fd\u662ftrue\u5c31\u662ftrue\u3002\u6362\u53e5\u8bdd\u8bf4\u5c31\u50cf\u5f00\u5173\u4e32\u8054\u3002\u672c\u6765\u6211\u5199\u5f97\u662fall([dfs(start) for start in edges])\uff0c\u540e\u6765\u53d1\u73b0\u4ee3\u8868list\u7684\u65b9\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\uff0c\u4f30\u8ba1\u662f\u4e9b\u8bed\u6cd5\u7cd6\u3002Python\u91cc\u9762\u786e\u5b9e\u6709\u5f88\u591a\u6211\u4e0d\u77e5\u9053\u7684\u8bed\u6cd5\u7cd6\u30026. dfs\u662f\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u4e86self\u548c\u7528\u5185\u51fd\u6570\u5916\u7684\u53d8\u91cf\u4fdd\u5b58\u72b6\u6001\uff0c\u4e0d\u7528\u6bcf\u4e2arecursion call\u90fdpass\u4e00\u6b21\u3002 \u505a\u5b8c\u8fd9\u9898\u540e\uff0c\u91cd\u65b0\u505acourse...Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-20T17:20:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-20T22:10:12+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5\",\"datePublished\":\"2020-06-20T17:20:18+00:00\",\"dateModified\":\"2020-06-20T22:10:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\"},\"wordCount\":85,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\",\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-06-20T17:20:18+00:00\",\"dateModified\":\"2020-06-20T22:10:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/","og_locale":"en_US","og_type":"article","og_title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning","og_description":"\u8fd9\u9898\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u62d3\u6251\u6392\u5e8ftopological sort\uff0c\u9700\u8981\u5224\u65ad\u6709\u6ca1\u6709\u73af\u6765\u5904\u7406\u65e0\u6548input\uff0c\u96be\u5ea6\u4e3b\u8981\u6765\u81ea\u8fd9\u90e8\u5206\u3002Topological sort\u53ef\u4ee5\u7528dfs\u89e3\u51b3\uff0c\u4f46\u662f\u6ca1\u7ec3\u4e60\u8fc7\u8fd8\u4e0d\u4e0d\u5bb9\u6613\u5199\u5bf9\u7684\u3002\u5148\u4e0a\u4e00\u4e2a\u6211\u6bd4\u8f83\u6ee1\u610f\u7684python\u89e3\u6cd5\u3002 \u603b\u7ed3\u4e00\u4e0b\u8981\u70b9\u30021. edges = {c: [] for word in words for c in word} \u8fd9\u79cd\u4e00\u884c\u4e24\u4e2a\u5faa\u73af\u5efamap\u7684\u65b9\u5f0f\u6211\u4e5f\u662f\u65b0\u5b66\u7684\uff0c\u5f88\u65b9\u4fbf\u3002\u8fd9\u4e2amap key\u662f\u6392\u524d\u9762\u7684\u5b57\u6bcd\uff0cvalue\u662f\u6392\u540e\u9762\u7684\u5b57\u6bcd\u3002\u800c\u4e14\u4e0d\u7ba1\u6709\u6ca1\u6709\u540e\u9762\u7684\uff0ckey\u90fd\u52a0\u4e0a\u4e86\uff0c\u8fd9\u6837\u7701\u4e86\u53e6\u4e00\u4e2a\u53d8\u91cf\u5b58\u5b57\u6bcd\u8868\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u6211\u4e4b\u524d\u4e00\u4e2a\u5199\u5bf9\u4e0d\u90a3\u4e48\u597d\u7684\u89e3\u6cd5\u30022. \u7528zip\u904d\u5386pair\u5f88\u597d\u7528\uff0c\u51cf\u5c11\u4e86\u51faindex bug\u7684\u53ef\u80fd3. len(word1) > len(word2) and word1.startswith(word2)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684case\u3002\u6bd4\u5982abc\u6392\u5728ab\u524d\u9762\uff0c\u4e0d\u7ba1\u4ec0\u4e48\u5b57\u6bcd\u8868\u987a\u5e8f\uff0c\u5c31\u662fc\u6392\u5728\u7a7a\u524d\u9762\uff0c\u90a3\u662f\u65e0\u6548\u7684\u30024. seen\u7684\u5229\u7528\uff0c\u8fd9\u662f\u6700\u5de7\u5999\u7684\u5730\u65b9\u3002\u5199\u4e86\u51e0\u79cd\u5c1d\u8bd5\uff0c\u53d1\u73b0\u73af\u4e0d\u592a\u5bb9\u6613\u3002\u5982\u679c\u53ea\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u5c31\u65e0\u6cd5\u533a\u5206\u662f\u4e00\u6761\u5df2\u7ecf\u8d70\u901a\u7684\u8def\u8fd8\u662f\u5f53\u524d\u8fd9\u6761\u8def\u53c8\u56de\u5230\u67d0\u4e2a\u4e4b\u524d\u7ecf\u8fc7\u7684\u4e00\u70b9\u3002\u89e3\u6cd5\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e00\u4e2aset\u8bb0\u5f55visited\uff0c\u53ea\u52a0\u4e0d\u51cf\uff0c\u53e6\u4e00\u4e2aset\u8bb0\u5f55\u5f53\u524d\u8fd9\u6761\u8def\u7684\u8def\u5f84\uff0c\u56de\u6eaf\u65f6\u8981\u66f4\u65b0\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002\u8fd9\u79cd\u89e3\u6cd5\u5229\u7528\u4e86False\u548cTrue\u4e24\u79cdstates\uff0cFalse\u4ee3\u8868\u6b63\u5728\u8d70\u7684\u8def\uff0cTrue\u4ee3\u8868\u4ee5\u524d\u5df2\u7ecf\u8d70\u5230\u5e95\u80fd\u8d70\u901a\u7684\u8def\u3002\u975e\u5e38\u6df1\u523b\uff0c\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u7b97\u5927\u6982\u7406\u89e3\u30025. if not all(dfs(start) for start in edges)\uff0call\u662f\u4e2a\u6211\u65b0\u5b66\u7684function\uff0cinput\u662fcollection\u6216\u8005\u66f4\u51c6\u786e\u8bf4\u662f\u53ef\u4ee5iterable\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u8981\u6bcf\u4e2a\u5143\u7d20\u90fd\u662ftrue\u5c31\u662ftrue\u3002\u6362\u53e5\u8bdd\u8bf4\u5c31\u50cf\u5f00\u5173\u4e32\u8054\u3002\u672c\u6765\u6211\u5199\u5f97\u662fall([dfs(start) for start in edges])\uff0c\u540e\u6765\u53d1\u73b0\u4ee3\u8868list\u7684\u65b9\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\uff0c\u4f30\u8ba1\u662f\u4e9b\u8bed\u6cd5\u7cd6\u3002Python\u91cc\u9762\u786e\u5b9e\u6709\u5f88\u591a\u6211\u4e0d\u77e5\u9053\u7684\u8bed\u6cd5\u7cd6\u30026. dfs\u662f\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u4e86self\u548c\u7528\u5185\u51fd\u6570\u5916\u7684\u53d8\u91cf\u4fdd\u5b58\u72b6\u6001\uff0c\u4e0d\u7528\u6bcf\u4e2arecursion call\u90fdpass\u4e00\u6b21\u3002 \u505a\u5b8c\u8fd9\u9898\u540e\uff0c\u91cd\u65b0\u505acourse...Read more","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/","og_site_name":"Feel Like Learning","article_published_time":"2020-06-20T17:20:18+00:00","article_modified_time":"2020-06-20T22:10:12+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5","datePublished":"2020-06-20T17:20:18+00:00","dateModified":"2020-06-20T22:10:12+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/"},"wordCount":85,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/","url":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/","name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5 | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-06-20T17:20:18+00:00","dateModified":"2020-06-20T22:10:12+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/06\/20\/leetcode-269-alien-dictionary-solution\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 269. Alien Dictionary \u89e3\u6cd5"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/323"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":6,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":338,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/323\/revisions\/338"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}