Skip to main content

Word Ladder

Shortest transformation from beginWord to endWord using wordList Change one letter at a time; each intermediate word must be in wordList Return length of the shortest transformation, or 0 if impossible

Transform one word into another, changing one letter at a time, using only valid dictionary words. It's a shortest-path problem hiding behind a word puzzle—BFS finds the minimum steps. The insight is modeling words as graph nodes where edges connect words that differ by exactly one letter.

Examples

You write

ladder_length(begin_word: str, end_word: str, word_list: List[str])

  1. Example 1

    Input
    beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
    returns
    5
  2. Example 2

    Input
    beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
    returns
    0

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Shortest Transformationhard

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.