1. Word set
A Set gives O(1) membership and visited-removal.
const words = new Set(wordList)Typorithm · hard
Step 1 of 8 · intro card
Graphs
BFS over an implicit graph where words connect if they differ by one letter; the first time endWord appears gives the shortest ladder.
Words are nodes; an edge joins two words that differ by exactly one letter.
BFS from beginWord level by level finds the shortest transformation sequence.
Neighbors are generated by swapping each position through a-z and checking the word set, deleting visited words.
Implicit graph: one-letter-apart edges.
BFS gives the shortest ladder.
Generate neighbors via a-z swaps.
function ladderLength(beginWord, endWord, wordList) {
const words = new Set(wordList);
if (!words.has(endWord)) {
return 0;
}
let queue = [beginWord];
let steps = 1;
while (queue.length > 0) {
const next = [];
for (const word of queue) {
if (word === endWord) {
return steps;
}
for (let i = 0; i < word.length; i += 1) {
for (let c = 97; c <= 122; c += 1) {
const candidate = word.slice(0, i) + String.fromCharCode(c) + word.slice(i + 1);
if (words.has(candidate)) {
words.delete(candidate);
next.push(candidate);
}
}
}
}
queue = next;
steps += 1;
}
return 0;
}Connect each part of the code to the concept before you type.
A Set gives O(1) membership and visited-removal.
const words = new Set(wordList)Process one transformation level per step.
const next = []Change each position to every letter a-z.
const candidate = word.slice(0, i) + String.fromCharCode(c) + word.slice(i + 1)Removing from the set prevents revisiting a word.
words.delete(candidate)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.