Data Structures & Algorithms Backtracking
💡
Exercise 146

Word Search 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given a 2D board and a word, find if the word exists in the grid. Letters must be adjacent (horizontal/vertical) and each cell used at most once.

Backtracking on a grid: try each cell as starting point. DFS matching one character at a time. Mark cells as visited and unmark when backtracking.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Check if 'ABCCED' exists in the given board.
For each cell matching first char, DFS. Mark visited by temporarily changing cell. Restore on backtrack.
🧪 Test Cases
Input
exist(board, 'ABCCED')
Expected
True
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.