Problem statement:
Given an m x n
2-D grid of characters board
, return true
if the word exists in the grid, otherwise return false
.
The word can be formed from letters which are horizontally or vertically neighboring cells in the board. The same cell may not be used more than once.
Example1:
Input: board = [ ["A","B","C","D"], ["W","M","G","O"], ["B","D","C","N"] ], word = "DOG"
Output: true
Example2:
Input: board = [ ["A","B","C","D"], ["W","A","G","O"], ["B","T","C","N"] ], word = "CAT"
Output: false
Algorithmic Steps This problem is solved by Depth-First Search (DFS) using recursion, which allows us to explore all potential paths from a given cell to form the word. The algorithmic approach can be summarized as follows:
-
Create a function(
isWordExist
) by accepting them*n
matrix as input parameter. -
Calculate dimensions
rows
andcols
for traversing the square matrix. -
Create a DFS function by passing element co-ordinates and current index of word.
- Add a first base check, returning
true
if the length of word is equals to word's index. - Add a second base check to verify out of boundary conditions, letter's equality and element already visisted or not. If any of those conditions satisfy, return
false
indicating given word doesn't exist - Mark the currently visited cells with
*
and reset it at the end. - Perform dfs function in all four directions of current cell and return
true
if any of them forms a word.
- Add a first base check, returning
-
Iterate over each cell of the matrix and invoke recursive DFS function. The return value of dfs function determine whether given word exists or not.
-
After completing step4, return
false
if the given word doesn't found in the board.
Time and Space complexity:
This algorithm has a time complexity of O(m * n * 4^L)
, where ``mis the number of rows,
n` is the number of columns, and `L` is the length of the word. This is because each element needs to visted once and four neighbouring directions.
It requires O(L)
,where L
is the length of the word. constant space complexity irrespective of the matrix size. This is due to recursion stack used during Depth-First Search (DFS).