Problem Statement:

You are given a 2D board(N rows and M columns) of characters and a string “word”. Your task is to check if the given word exists in the grid or not. 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.

Example 1:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “ABCCED” Output: true

Example 2:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “SEE” Output: true

Example 3:

Input: board = [[“A”,”B”,”C”,”E”],[“S”,”F”,”C”,”S”],[“A”,”D”,”E”,”E”]], word = “ABCB” Output: false

 

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.

 

Solution : Using DFS

  • Our goal is to find if the given word exists in the matrix or not. We only have to look at the adjacent cells (ignore the diagonal ones).
  • One quick look at the problem can suggest that we can solve this by Backtracking. Match character-by-character, go ahead and check if the adjacent cells match the next character, and go back if it does not match.
  • The question is How should we traverse the matrix efficiently??
  • We need to think of a traversal approach. BFS? DFS? Both can work. But DFS will be better as it immediately checks the next node of the graph and returns if it is not needed after marking it as visited.

Steps:

  1. Traverse the entire matrix, For each cell (i,j)
  2. Check if matrix[i][j] == word[index]. (we are using index to keep track of the characters that we have already checked in the word during backtracking.)
  3. If step 2 is true, then check to repeat the same process for the adjacent cells and for the next index in the word.
  4. If step 2 is false, then return to the previous state where we received a true value and then further check any remaining and unvisited cells.

 

Note: We mark each cell as unvisited after we check all the adjacent possibilities because we might visit that cell again when we start traversing the matrix from a different source cell.

 

 

Time Complexity: O(M*N*4^L)

L- Length of given word.

For a given start point (i,j), it is impossible for you to search beyond the length of L and for each grid, we have four directions in general. Thus time complexity is O(MN4^L)

Space Complexity: O(L)

  • Recursion call stack

Similar Problems:

https://yellowcoding.com/tag/matrix/

https://yellowcoding.com/tag/dfs/

Additionals:

https://en.wikipedia.org/wiki/Word_search

https://www.codingninjas.com/codestudio/problem-details/word-search_892986

https://leetcode.com/problems/word-search/

https://rosettacode.org/wiki/Word_search