-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path269_alien_dictionary.py
More file actions
67 lines (55 loc) · 1.69 KB
/
269_alien_dictionary.py
File metadata and controls
67 lines (55 loc) · 1.69 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution:
def alienOrder(self, words):
"""
:type words: list[str]
:rtype: str
"""
if not words:
return ''
ans = []
gotcha = set()
max_size = max(len(word) for word in words)
for j in range(max_size):
for word in words:
if j >= len(word):
continue
if word[j] in gotcha:
continue
ans.append(word[j])
gotcha.add(word[j])
return ''.join(ans)
class Solution:
"""
REF: https://discuss.leetcode.com/topic/28308/java-ac-solution-using-bfs/2
"""
def alienOrder(self, words):
if not words:
return ''
ans = []
edges = {}
indeg = {}
for w in words:
for c in w:
indeg[c] = 0
for i in range(len(words) - 1):
cur = words[i]
nxt = words[i + 1]
for j in range(min(len(cur), len(nxt))):
if cur[j] == nxt[j]:
continue
if cur[j] not in edges:
edges[cur[j]] = set()
if nxt[j] not in edges[cur[j]]:
edges[cur[j]].add(nxt[j])
indeg[nxt[j]] = indeg.get(nxt[j], 0) + 1
break
queue = [c for c, deg in indeg.items() if deg == 0]
for c in queue:
ans.append(c)
if c not in edges:
continue
for _c in edges[c]:
indeg[_c] -= 1
if indeg[_c] == 0:
queue.append(_c)
return ''.join(ans) if len(ans) == len(indeg) else ''