Problem 131 of 198MediumLeetCode

Word Search

Backtracking

Asked at 12 companies

8 recent

  • Faire
  • Uber
  • Bloomberg
  • Grammarly
  • Amazon
  • Microsoft
  • Meta
  • Google
  • Karat
  • Mastercard
+2 more
  • PayPal
  • Netflix

Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.

  1. 1Basics
  2. 2Paper
  3. 3Code it
  4. 4Watch
  5. 5Visualize
  6. 6Accepted
  7. 7Review

Go step by step: try it yourself before the video and the visual. Progress saves on this device.

0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15

Step 1

Know these before you start

Practise each one here, or tick what you already know

Concept video
Learn ›Practise ✎Learn ›Practise ✎Learn ›Practise ✎

Builds on: Building a new string or array as the answer · Replacing recursion with your own stack · Recursion and the call stack · The base case: where the calls stop · Depth-first traversal: preorder, inorder, postorder · Binary numbers and AND, OR, XOR

Step 2

Try it on paper

5–10 minutes, no code yet

In the wild · Sudoku solvers and regex: Try a choice, go deeper, and undo it when it fails. Solvers fill puzzles this way, and many regex engines match patterns the same way.

About this problem

Try a choice, recurse, undo it — subsets, permutations, and search.

Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.

  1. Example 1

    Input
    board = [["A", "B", "C", "E"],["S", "F", "C", "S"],["A", "D", "E", "E"]]word = "ABCCED"
    Output
    true
    Walk through it step by step(7 steps · shows the approach, so try first)
    1. Start from every cell that matches "A". From there, step to a neighbour (up/down/left/right) that matches the next letter, never reusing a cell. If a path dead-ends, back up and try another direction.
    2. (0, 0) is "A" — matches letter 1. Look at its neighbours for "B".
    3. (0, 1) is "B" — matches letter 2. Look at its neighbours for "C".
    4. (0, 2) is "C" — matches letter 3. Look at its neighbours for "C".
    5. (1, 2) is "C" — matches letter 4. Look at its neighbours for "E".
    6. (2, 2) is "E" — matches letter 5. Look at its neighbours for "D".
    7. Matched every letter: "ABCCED" — the word is on the board!

    ▶ Watch these steps animate

  2. Example 2

    1×1 gridSingle character
    Input
    board = [["a"]]word = "a"
    Output
    true
  3. Example 3

    Input
    board = [["A", "B", "C", "E"],["S", "F", "C", "S"],["A", "D", "E", "E"]]word = "SEE"
    Output
    true

Code it here

Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor

Step 6

Get it accepted

All test cases pass

Step 7

Review it so it sticks

Re-solve from memory after

The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.

Your notes

Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.

Solve it yourself before you look

Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.

Hidden: optimal solution

Stuck? One hint at a time.

    Srujan's notes aren't here yet. When he solves this one in his journal, his own writeup and solutions show up on this page too.