Given an m x n grid of characters board and a string word, return true if word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

Input & Output

Example 1 — Found Word
$ Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
💡 Note: Path exists: A(0,0) → B(0,1) → C(0,2) → C(1,2) → E(2,2) → D(2,1). Each step moves to adjacent cell without reusing.
Example 2 — Word Not Found
$ Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
💡 Note: Cannot form ABCB because after A(0,0) → B(0,1) → C(0,2), we need another B but the original B(0,1) cannot be reused.
Example 3 — Single Character
$ Input: board = [["A"]], word = "A"
Output: true
💡 Note: Single cell matches single character word perfectly.

Constraints

  • m == board.length
  • n = board[i].length
  • 1 ≤ m, n ≤ 6
  • 1 ≤ word.length ≤ 15
  • board and word consists of only lowercase and uppercase English letters.

Visualization

Tap to expand
Word Search Problem: Find "ABCCED" in GridInput GridABCESFCSADEETarget: ABCCEDABCCEDValid Path FoundABCESFCSADEEAlgorithm: Try each cell as start → DFS in 4 directions → Backtrack if dead endOutput: true
Understanding the Visualization
1
Input
Grid of characters and target word ABCCED
2
Search
Try each cell as start, explore 4 directions with backtracking
3
Result
Return true if valid path found, false otherwise
Key Takeaway
🎯 Key Insight: Use DFS with backtracking to explore all possible paths while preventing cell reuse through temporary marking
Asked in
Microsoft 45 Amazon 38 Facebook 32 Google 28
89.2K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen