Opening scenario

Number of Islands (LC 200): given an m × n grid of '1' (land) and '0' (water), count the islands. An island is land connected 4-directionally. Follow-up you should expect: Word Ladder (LC 127) — given beginWord, endWord, and a wordList, return the length of the shortest transformation sequence changing one letter at a time, where every intermediate word is in the list.

The grid is rectangular; only up/down/left/right land connections count, not diagonals. Word Ladder uses equal-length lowercase English words: every word after beginWord, including endWord, must occur in wordList; count both endpoint words, and return 0 if no transformation exists. Start by identifying what one reachable neighbor means in each problem. LC numbers identify LeetCode problems.

Question progression

Q1 — The grid isn't a graph object. Why is it still a graph problem?

Question: There are no nodes or edges given — just a 2D array of characters. What makes "count the islands" a graph traversal?

In the full tutorial

  • Canonical Template
  • Complexity
  • Pattern Recognition Signals
  • Practice Set
  • Pitfalls
  • Variations
  • Closing: When to Reach for This

Glossary history

Click dotted jargon to save explanations here.