📖 Scenario: You are building a simple word search puzzle solver. The puzzle is a grid of letters, and you want to find if a given word exists in the grid by moving horizontally or vertically to adjacent letters.This is like searching for a hidden word in a crossword puzzle.
🎯 Goal: Build a program that checks if a given word can be found in the letter grid by moving up, down, left, or right, without revisiting the same cell twice.
📋 What You'll Learn
Create a 2D array called board with exact letters
Create a string variable called word with the exact word to search
Write a function exist that returns true if the word is found in the board, else false
Use backtracking to explore possible paths in the grid
Print the result of exist(board, word)
💡 Why This Matters
🌍 Real World
Word search puzzles and games often require searching for words hidden in grids of letters. This technique helps solve or generate such puzzles.
💼 Career
Backtracking and grid traversal are common in technical interviews and real-world problems like pathfinding, puzzle solving, and game development.
Progress0 / 4 steps
1
Create the letter grid
Create a 2D array called board with these exact rows: ['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']
DSA Typescript
Hint
Use a constant variable board and assign a 2D array with the exact letters as rows.
2
Set the word to search
Create a string variable called word and set it to the exact value 'ABCCED'
DSA Typescript
Hint
Use const word: string = 'ABCCED'; to set the word.
3
Write the backtracking function to find the word
Write a function called exist that takes board: string[][] and word: string and returns true if the word exists in the board by moving horizontally or vertically. Use backtracking with a helper function dfs inside exist. Use variables rows, cols, and a 2D boolean array visited to track visited cells. Use for loops and recursion to explore neighbors.
DSA Typescript
Hint
Use a helper function dfs inside exist to explore neighbors recursively. Mark cells visited before recursion and unmark after.
4
Print the result of the word search
Write a console.log statement to print the result of calling exist(board, word)
DSA Typescript
Hint
Use console.log(exist(board, word)); to print the result.
Practice
(1/5)
1. What is the main purpose of backtracking in the Word Search problem on a grid?
easy
A. To sort the grid alphabetically
B. To explore all possible paths to find the word
C. To count the number of words in the grid
D. To replace characters in the grid
Solution
Step 1: Understand backtracking role
Backtracking tries all possible paths from each cell to find the target word.
Step 2: Eliminate other options
Sorting, counting, or replacing characters are unrelated to backtracking in word search.
Final Answer:
To explore all possible paths to find the word -> Option B
Quick Check:
Backtracking = Explore paths [OK]
Hint: Backtracking means trying all paths step-by-step [OK]
Common Mistakes:
Thinking backtracking sorts or counts
Confusing backtracking with simple search
Ignoring path exploration in grid
2. Which of the following TypeScript function signatures correctly represents a backtracking helper for word search in a grid?
easy
A. function backtrack(grid: string[][], word: string, row: number, col: number, index: number): boolean
B. function backtrack(grid: number[], word: string): void
C. function backtrack(word: string, grid: string[][]): number
D. function backtrack(grid: string[][], word: string): string
Solution
Step 1: Identify correct parameters
The helper needs the grid, the word, current position (row, col), and index in word to track progress.
Step 2: Check return type
It returns boolean to indicate if word is found along the path.
Final Answer:
function backtrack(grid: string[][], word: string, row: number, col: number, index: number): boolean -> Option A
Quick Check:
Correct params and boolean return = function backtrack(grid: string[][], word: string, row: number, col: number, index: number): boolean [OK]