-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien Dictionary
More file actions
35 lines (27 loc) · 923 Bytes
/
Alien Dictionary
File metadata and controls
35 lines (27 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#User function Template for python3
class Solution:
def findOrder(self,alien_dict, N, K):
adj = {chr(97+idx): set() for idx in range(K)}
for i in range(N - 1):
word_one, word_two = alien_dict[i], alien_dict[i + 1]
length = min(len(word_one), len(word_two))
for j in range(length):
if word_one[j] != word_two[j]:
adj[word_one[j]].add(word_two[j])
break
visited = {}
res = []
def dfs(char):
if char in visited:
return visited[char]
visited[char] = True
for neighChar in adj[char]:
if dfs(neighChar):
return True
visited[char] = False
res.append(char)
for char in adj:
if dfs(char):
return []
res.reverse()
return res