Word Search
Backtracking
Asked at 12 companies
8 recent
- Faire
- Uber
- Bloomberg
- Grammarly
- Amazon
- Microsoft
- Meta
- Karat
- Mastercard
+2 moreShow fewer
- PayPal
- Netflix
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
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
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.
Examples
Full statement on LeetCode ↗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)
- 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.
- (0, 0) is "A" — matches letter 1. Look at its neighbours for "B".
- (0, 1) is "B" — matches letter 2. Look at its neighbours for "C".
- (0, 2) is "C" — matches letter 3. Look at its neighbours for "C".
- (1, 2) is "C" — matches letter 4. Look at its neighbours for "E".
- (2, 2) is "E" — matches letter 5. Look at its neighbours for "D".
- Matched every letter: "ABCCED" — the word is on the board!
Example 2
1×1 gridSingle character- Input
- board = [["a"]]word = "a"
- Output
- true
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
Solution code
Optimal solutionfrom every cell that matches the first letter. Mark a cell as used while it's on the current path (and unmark when ) so no cell is reused.
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
rows, cols = len(board), len(board[0])
def dfs(r: int, c: int, i: int) -> bool:
if i == len(word):
return True
if not (0 <= r < rows and 0 <= c < cols) or board[r][c] != word[i]:
return False
board[r][c] = '#' # in use on this path
found = dfs(r + 1, c, i + 1) or dfs(r - 1, c, i + 1) or dfs(r, c + 1, i + 1) or dfs(r, c - 1, i + 1)
board[r][c] = word[i]
return found
return any(dfs(r, c, 0) for r in range(rows) for c in range(cols))Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.