Tutorialspoint
Problem
Solution
Submissions

Word Search in a 2D Board

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a C program to determine if a given word exists in a 2D board of characters. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same cell cannot be used more than once.

Example 1
  • Input:
    • board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]
    • word = "ABCCED"
  • Output: true
  • Explanation:
    • Step 1: Start at board[0][0] = 'A', which matches the first character of "ABCCED".
    • Step 2: Move right to board[0][1] = 'B', which matches the second character.
    • Step 3: Move right to board[0][2] = 'C', which matches the third character.
    • Step 4: Move down to board[1][2] = 'C', which matches the fourth character.
    • Step 5: Move down to board[2][2] = 'E', which matches the fifth character.
    • Step 6: Move left to board[2][1] = 'D', which matches the last character.
    • Step 7: The word "ABCCED" is found in the board.
Example 2
  • Input:
    • board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]
    • word = "SEE"
  • Output: true
  • Explanation:
    • Step 1: One possible path starts at board[1][0] = 'S'.
    • Step 2: Move right to board[1][3] = 'S' (after trying other paths that don't work).
    • Step 3: Move up to board[0][3] = 'E'.
    • Step 4: Move down to board[2][3] = 'E'.
    • Step 5: The word "SEE" is found in the board.
Constraints
  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board and word consist of only uppercase English letters
  • Time Complexity: O(m*n*4^L), where L is the length of the word
  • Space Complexity: O(m*n) for the visited array and recursion stack
ArraysAlgorithmsHCL TechnologiesArctwist
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a depth-first search (DFS) approach to explore all possible paths.
  • For each cell in the board, try to match the first character of the word.
  • If the first character matches, recursively search for the remaining characters.
  • Keep track of visited cells to avoid using the same cell multiple times.
  • Use backtracking: if a path doesn't lead to a solution, backtrack and try other paths.

Steps to solve by this approach:

 Step 1: Create a recursive DFS function that takes the board, word, current index in the word, current position (row, col), and a visited array.

 Step 2: For each cell in the board, try to match the first character of the word and start DFS from there.
 Step 3: In the DFS function, first check if we've matched all characters (base case for success).
 Step 4: If the current cell is out of bounds, already visited, or doesn't match the current character, return false.
 Step 5: Mark the current cell as visited to avoid using it again in the same path.
 Step 6: Recursively explore all four adjacent cells (up, down, left, right) for the next character.
 Step 7: If none of the paths lead to a solution, backtrack by marking the current cell as unvisited and return false.

Submitted Code :