export default function alienOrder(words) {
const graph = new Map();
const indegree = new Map();
for (const word of words) {
for (const ch of word) {
if (!graph.has(ch)) graph.set(ch, new Set());
if (!indegree.has(ch)) indegree.set(ch, 0);
}
}
for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i];
const w2 = words[i + 1];
if (w1.length > w2.length && w1.startsWith(w2)) {
return "";
}
const len = Math.min(w1.length, w2.length);
for (let j = 0; j < len; j++) {
const a = w1[j];
const b = w2[j];
if (a !== b) {
if (!graph.get(a).has(b)) {
graph.get(a).add(b);
indegree.set(b, indegree.get(b) + 1);
}
break;
}
}
}
const queue = [];
for (const [ch, degree] of indegree) {
if (degree === 0) queue.push(ch);
}
let result = "";
while (queue.length > 0) {
const ch = queue.shift();
result += ch;
for (const next of graph.get(ch)) {
indegree.set(next, indegree.get(next) - 1);
if (indegree.get(next) === 0) {
queue.push(next);
}
}
}
return result.length === indegree.size ? result : "";
}