-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathalienOrder.py
34 lines (29 loc) · 969 Bytes
/
alienOrder.py
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
"""
Alien Dictionary
O(n) for letters in words
O(1) since constant letters (26)
"""
class Solution:
def alienOrder(self, words: List[str]) -> str:
nodes = set("".join(words))
adj = collections.defaultdict(list)
degree = collections.Counter(nodes)
for w1, w2 in zip(words, words[1:]):
for c1, c2 in zip(w1, w2):
if c1 != c2:
adj[c1].append(c2)
degree[c2] += 1
break
else:
if len(w1) > len(w2):
return ""
stk = list(filter(lambda x: degree[x]==1, degree.keys()))
ans = []
while stk:
node = stk.pop()
ans.append(node)
for nei in adj[node]:
degree[nei] -= 1
if degree[nei] == 1:
stk.append(nei)
return "".join(ans) * (set(ans) == nodes)