# LeetCode 79. Word Search

## Description

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

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example:

```board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
```

Constraints:

• `board` and `word` consists only of lowercase and uppercase English letters.
• `1 <= board.length <= 200`
• `1 <= board[i].length <= 200`
• `1 <= word.length <= 10^3`

## Explanation

The solution would be backtracking, which is a methodology where we mark the current path of exploration, if the path does not lead to a solution, we then revert the change (i.e. backtracking) and try another path.

As the general idea for the solution, we would walk around the 2D board, at each step we compare the board character with the first remaining character of the word. If matched, we continued on the path. If not, we give up the path. And at each step, we would mark or revert our visit so that we won’t have duplicated visits. Finally, if no more character left for the word, we find our solution.

## Python Solution

``````class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:

for i in range(0, len(board)):
for j in range(0, len(board)):
if self.helper(board, i, j, word):
return True

return False

def helper(self, board, i, j, word):
if len(word) == 0:
return True

if i < 0 or i > len(board) - 1 or j > len(board) - 1 or j < 0:
return False

if board[i][j] != word:
return False

word = word[1:]
char = board[i][j]
board[i][j] = '#'

result = self.helper(board, i + 1, j, word) or self.helper(board, i - 1, j, word) or self.helper(board, i, j + 1, word) or self.helper(board, i, j - 1, word)

board[i][j] = char

return result
``````
• Time complexity: O(N * 4^L). where N is the number of cells in the board and L is the length of the word to be matched. For the backtracking function, its execution trace would be visualized as a 4-ary tree, each of the branches represent a potential exploration in the corresponding direction. Therefore, in the worst case, the total number of invocation would be the number of nodes in a full 4-nary tree, which is about 4^L. We iterate through the board for backtracking, i.e. there could be N times invocation for the backtracking function in the worst case. As a result, overall the time complexity of the algorithm would be O(N * 4^L).
• Space complexity: O(L). where L is the length of the word to be matched.