Word Search
Problem Statement
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.
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
Input Format:
- An m x n grid of characters 'board'
- A string 'word'
Output Format:
- Return true if word exists in the grid, otherwise return false
Examples:
Example 1:
Input:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output:
true
Explanation:
The word 'ABCCED' can be constructed by going from A → B → C → C → E → D.
Example 2:
Input:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output:
true
Explanation:
The word 'SEE' can be constructed by going from S → E → E.
Example 3:
Input:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output:
false
Explanation:
The word 'ABCB' cannot be constructed because the letter 'B' would be used twice.
Solutions
Backtracking DFS
Use depth-first search from each cell in the grid as a starting point. Explore in all four directions recursively while ensuring each cell is used only once.
Optimized Backtracking
Enhance the backtracking algorithm with additional optimizations such as checking character frequency and pruning paths early.
Algorithm Walkthrough
Example input: nums = []
Step-by-step execution:
- Start at cell (0,0) containing 'A'
- Mark (0,0) as visited and move right to (0,1) containing 'B'
- Mark (0,1) as visited and move right to (0,2) containing 'C'
- Mark (0,2) as visited and move down to (1,2) containing 'C'
- Mark (1,2) as visited and move right to (0,3) containing 'E'
- Mark (0,3) as visited and move down to (1,3) containing 'D'
- All characters in 'ABCCED' have been found, return true
Hints
Hint 1
Hint 2
Hint 3
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize the backtracking process on the grid as it searches for the word.
Key visualization elements:
- current cell
- visited cells
- path
Similar Questions
Number of Islands
MediumGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Word Search II
HardGiven an m x n board of characters and a list of strings words, return all words on the board. Each word must 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 in a word.
Implementation Notes
This problem tests understanding of backtracking and depth-first search in a 2D grid. The key insight is to explore all possible paths while ensuring each cell is used only once.