-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalienDictionary.py
47 lines (40 loc) · 1.48 KB
/
alienDictionary.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
35
36
37
38
39
40
41
42
43
44
45
46
47
from collections import deque, defaultdict
from typing import List
class Solution:
def foreignDictionary(self, words: List[str]) -> str:
# Initialize graph and in-degree
graph = defaultdict(set)
in_degree = defaultdict(int)
all_chars = set()
# Add all characters to the set
for word in words:
for char in word:
all_chars.add(char)
# Build the graph
for i in range(len(words) - 1):
w1, w2 = words[i], words[i + 1]
min_length = min(len(w1), len(w2))
found_diff = False
for j in range(min_length):
if w1[j] != w2[j]:
if w2[j] not in graph[w1[j]]:
graph[w1[j]].add(w2[j])
in_degree[w2[j]] += 1
found_diff = True
break
if not found_diff and len(w1) > len(w2):
return ""
# Topological sort using Kahn's Algorithm (BFS)
q = deque([c for c in all_chars if in_degree[c] == 0])
result = []
while q:
char = q.popleft()
result.append(char)
for neighbor in graph[char]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
q.append(neighbor)
# Check if the topological sort is valid
if len(result) != len(all_chars):
return ""
return "".join(result)