You are given a grid of letters, board, and a string, word. Decide whether you can spell word by walking through the grid one cell at a time.
Each step moves to a cell directly above, below, left or right of the current one. Diagonal moves are not allowed, and no cell may be used twice in the same word. Return true if such a walk exists, and false otherwise.
Example 1
Input: board = [["M","A","T"],
["A","S","E"],
["P","E","N"]], word = "MATE"
Output: true
Why: M, A, T along the top row, then down to E2 more examples and the constraints
Example 2
Input: same board, word = "PEN" Output: true Why: P, E, N run along the bottom row
Example 3
Input: same board, word = "TET" Output: false Why: the only T would have to be used twice
Constraints
- 1 ≤ rows, columns ≤ 6
- 1 ≤ length of word ≤ 15
- board and word hold only English letters, upper or lower case, and case matters
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Backtracking with a used gridO(m·n·3^L)
- Backtracking in place, with pruningO(m·n·3^L)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work on a 6 × 6 board of A's, word AAAAAAAAAAAAAAB | Verdict |
|---|---|---|---|
| Backtracking with a used grid | O(m·n·3^L) | Correct; explores every path of A's | |
| Backtracking in place, with pruning | O(m·n·3^L) | Best; the letter count says no at once |
m × n = board size, L = length of the word.
Edge cases to test
- A cell would be needed twice. TET on Example 1's board has only one T, so it is false even though T and E are neighbours.
- Word longer than the board. A 2 × 2 board can never spell a five-letter word; the letter count catches it.
- One-cell board. [["a"]] with word a is true; with word b it is false.
- Case matters. a and A are different letters, so [["a","B"]] cannot spell ab.
- Many matching starts. A board of all A's and a word ending in B: every path fails only at the end, unless you count letters first.
Common mistakes
- Forgetting to unmark a cell on the way back, so a later path that needs it is wrongly blocked.
- Marking cells visited for the whole search, like a flood fill, instead of only along the current path.
- Checking diagonal neighbours, or stopping after the first starting cell that fails.
How to talk through it
- 1Clarify. Are only up, down, left and right moves allowed? Can a cell be reused? Does case matter?
- 2Describe the search. Start from every cell, extend the path one matching letter at a time, and undo the last step when it leads nowhere.
- 3State the cost. Up to m·n starts, then at most 3 directions per letter: O(m·n·3^L). Say it is exponential in L, which is fine for L ≤ 15.
- 4Add pruning. Count letters first and search from the rarer end. Mark cells in place with # to save the extra grid.
- 5Test out loud. Run the reused-cell case and a one-cell board before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?