Practice: Word Ladder

Problem: https://leetcode.com/problems/word-ladder/

Recognition reminder: the words are nodes, and two words are adjacent when they differ by exactly one letter. The question asks for the shortest transformation sequence, which is the fewest edges between begin_word and end_word in an unweighted graph. “Shortest, unweighted” is the BFS cue: the first time BFS reaches end_word, it has reached it in the fewest steps. The edges are implicit, so generating neighbors efficiently is the second half of the problem.

Signature note

Write your solution as a function ladder_length(begin_word: str, end_word: str, word_list: list[str]) -> int. Return the number of words in the shortest transformation sequence from begin_word to end_word (counting both endpoints), or 0 if no such sequence exists. Every intermediate word, and end_word, must be in word_list; begin_word need not be. All words are the same length and lowercase.

Before you start (the five-beat rhythm)

  1. Your Pattern Card for this week is already written.
  2. Name the pattern aloud and write your approach as a plain-English comment before any code.
  3. Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
  4. If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
  5. Debrief in your commit message before moving to the next problem.

Your target

Fill these in yourself before you look at anyone else’s solution:

Target time complexity:  ____
Target space complexity: ____

Reason about it in terms of N, the number of words, and L, the length of each word. Hint to chew on: for each word you can try changing each of its L positions, and forming or matching a candidate string is itself O(L) work; the wildcard-pattern precompute is what keeps neighbor lookup from becoming “compare against all N words”. Work out where the N and both factors of L land.

Where your code goes

Write your solution in your own work repo (see getting-started.md), not in this folder. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.

Debrief (paste into your commit message)

1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?