Word Search - Problem
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code