Overview
You are given a list of words sorted in lexicographic order according to an alien language's alphabet. Derive the order of characters in this language.
Given the sorted list, determine the ordering of characters. If the order is invalid (contains a cycle), return an empty string. If multiple valid orderings exist, return any one of them.
Constraints
1 <= words.length <= 1001 <= words[i].length <= 100- Words contain only lowercase English letters.
- The sorted order is derived from the alien alphabet.
- Return
""if the ordering is invalid.
Examples
alienOrder(["wrt", "wrf", "er", "ett", "rftt"]); // => "wertf" // From comparisons: w < e, e < r, t < f, r < t
alienOrder(["z", "x"]); // => "zx" alienOrder(["z", "x", "z"]); // => "" (invalid — z < x and z > x)
Solution
Reveal solution
function alienOrder(words) {
const adj = new Map();
const inDegree = new Map();
// Initialize all chars
for (const word of words) {
for (const ch of word) {
if (!adj.has(ch)) adj.set(ch, new Set());
if (!inDegree.has(ch)) inDegree.set(ch, 0);
}
}
// Build edges from adjacent word comparisons
for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i], w2 = words[i + 1];
// Check invalid: w1 is longer and is a prefix of w2
if (w1.length > w2.length && w1.startsWith(w2)) return "";
for (let j = 0; j < Math.min(w1.length, w2.length); j++) {
if (w1[j] !== w2[j]) {
if (!adj.get(w1[j]).has(w2[j])) {
adj.get(w1[j]).add(w2[j]);
inDegree.set(w2[j], inDegree.get(w2[j]) + 1);
}
break;
}
}
}
// Topological sort (Kahn's)
const queue = [];
for (const [ch, deg] of inDegree) {
if (deg === 0) queue.push(ch);
}
const result = [];
while (queue.length > 0) {
const ch = queue.shift();
result.push(ch);
for (const next of adj.get(ch)) {
inDegree.set(next, inDegree.get(next) - 1);
if (inDegree.get(next) === 0) queue.push(next);
}
}
return result.length === inDegree.size ? result.join("") : "";
}Resources
extraterrestrial-language.js
Extraterrestrial Language
hardcodingAlgorithmsGraphs
Overview
You are given a list of words sorted in lexicographic order according to an alien language's alphabet. Derive the order of characters in this language.
Given the sorted list, determine the ordering of characters. If the order is invalid (contains a cycle), return an empty string. If multiple valid orderings exist, return any one of them.
Constraints
1 <= words.length <= 1001 <= words[i].length <= 100- Words contain only lowercase English letters.
- The sorted order is derived from the alien alphabet.
- Return
""if the ordering is invalid.
Examples
alienOrder(["wrt", "wrf", "er", "ett", "rftt"]); // => "wertf" // From comparisons: w < e, e < r, t < f, r < t
alienOrder(["z", "x"]); // => "zx" alienOrder(["z", "x", "z"]); // => "" (invalid — z < x and z > x)
Solution
Reveal solution
function alienOrder(words) {
const adj = new Map();
const inDegree = new Map();
// Initialize all chars
for (const word of words) {
for (const ch of word) {
if (!adj.has(ch)) adj.set(ch, new Set());
if (!inDegree.has(ch)) inDegree.set(ch, 0);
}
}
// Build edges from adjacent word comparisons
for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i], w2 = words[i + 1];
// Check invalid: w1 is longer and is a prefix of w2
if (w1.length > w2.length && w1.startsWith(w2)) return "";
for (let j = 0; j < Math.min(w1.length, w2.length); j++) {
if (w1[j] !== w2[j]) {
if (!adj.get(w1[j]).has(w2[j])) {
adj.get(w1[j]).add(w2[j]);
inDegree.set(w2[j], inDegree.get(w2[j]) + 1);
}
break;
}
}
}
// Topological sort (Kahn's)
const queue = [];
for (const [ch, deg] of inDegree) {
if (deg === 0) queue.push(ch);
}
const result = [];
while (queue.length > 0) {
const ch = queue.shift();
result.push(ch);
for (const next of adj.get(ch)) {
inDegree.set(next, inDegree.get(next) - 1);
if (inDegree.get(next) === 0) queue.push(next);
}
}
return result.length === inDegree.size ? result.join("") : "";
}Resources
NameTopicDifficulty
103 of 103 problems