外星人词典
链接到在线判断-> LINK
给定一个已排序的外语词典,它具有标准词典的 N 个单词和 k 个起始字母。找出外星语言中的字符顺序。注意:对于特定的测试用例,可能有很多订单,因此您可以返回任何有效订单,如果函数返回的字符串顺序正确,则输出将为 1,否则 0 表示返回的字符串不正确。
Example 1:
Input:
N = 5, K = 4
dict = {"baa","abcd","abca","cab","cad"}
Output:
1
Explanation:
Here order of characters is
'b', 'd', 'a', 'c' Note that words are sorted
and in the given language "baa" comes before
"abcd", therefore 'b' is before 'a' in output.
Similarly we can find other orders.
Run Code Online (Sandbox Code Playgroud)
我的工作代码:
from collections import defaultdict
class Solution:
def __init__(self):
self.vertList = defaultdict(list)
def addEdge(self,u,v):
self.vertList[u].append(v)
def topologicalSortDFS(self,givenV,visited,stack):
visited.add(givenV) …Run Code Online (Sandbox Code Playgroud)