Skip to content
Problem 212 · LeetCode 212(opens in a new tab)

Word Search II

HardBacktracking

You get a grid of lower-case letters, board, and a list of different words. Find every word in the list that can be spelled on the board.

A word is spelled by a path of cells, where each next cell touches the previous one on a side: up, down, left or right. One path may not visit the same cell twice. Return the words that can be spelled, in any order.

Example 1

Input:  board = [["c","a","t"],
                 ["o","r","e"],
                 ["d","o","g"]]
        words = ["cat", "core", "dog", "rat", "toe"]
Output: ["cat", "core", "dog", "rat"]
Why:    no o touches the t, so "toe" cannot be spelled
2 more examples and the constraints

Example 2

Input:  board = [["x","y"],["z","w"]], words = ["xyx"]
Output: []
Why:    the only x would have to be used twice

Example 3

Input:  board = [["a","t"],["o","e"]],
        words = ["ate", "at", "toe"]
Output: ["at", "ate"]
Why:    a → t → e spells both; t never touches an o

Constraints

  • 1 ≤ rows, columns ≤ 12
  • 1 ≤ number of words ≤ 30,000
  • 1 ≤ length of each word ≤ 10
  • Board and words use only a to z; the words are all different

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Word Search once per wordO(W·m·n·3^L)
  2. Trie + one backtracking passO(m·n·3^L)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 30,000 words on a 12 × 12 boardVerdict
Word Search once per wordO(W·m·n·3^L)
Too slow on the big tests
Trie + one backtracking passO(m·n·3^L)
Best; shares work across words

W = number of words, m × n = board size, L = longest word.

Edge cases to test

  • Same word on two paths. On [["a","a"]], "a" can start at either cell but must be returned only once.
  • A cell needed twice. "xyx" on a board with one x is not spellable.
  • One word is a prefix of another. "at" and "ate" share a path; finding "at" must not stop the search for "ate".
  • Nothing matches. Return an empty list.

Common mistakes

  • Stopping the DFS when a word is found, so longer words on the same path are missed.
  • Forgetting to restore the cell after backtracking, so later paths see a "#" there.
  • Adding a word every time it is reached instead of clearing it, which returns duplicates.

How to talk through it

  1. 1Clarify. Can one cell be used twice in a word? Are diagonal moves allowed? Does the output order matter?
  2. 2Give the brute force. Run Word Search for every word. Say why it repeats work: words share prefixes, but each search starts from scratch.
  3. 3Share the prefixes. A trie stores each shared prefix once, and tells the DFS when no word can continue.
  4. 4Code the DFS. Walk the board and the trie together, mark cells in use, record and clear words, prune empty branches.
  5. 5Test out loud. Run a word found twice, a prefix pair like "at" and "ate", and a cell needed twice.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?