# Check if a word exists in a grid or not in Python

Suppose, we have a grid or a matrix of words. We have to check whether a given word is present in the grid or not. The word can be found in four ways, horizontally left and right and vertically up and down. If we can find the word we return True, otherwise False.

So, if the input is like

 p g h s f y k d g h t k g h i h n s j s o j f g h n r t y u

input_str = "python", then the output will be True.

To solve this, we will follow these steps −

• Define a function find_grid() . This will take matrix, input_str, row_pos, col_pos, row_count, col_count, degree
• if degree is same as size of input_str , then
• return True
• if row_pos < 0 or col_pos < 0 or row_pos >= row_count or col_pos >= col_count, then
• return False
• if matrix[row_pos, col_pos] is same as input_str[degree], then
• temp := matrix[row_pos, col_pos]
• replace elements of matrix[row_pos, col_pos] with '#'
• result := find_grid(matrix, input_str, row_pos - 1, col_pos,
• row_count, col_count, degree + 1) bitwise or
• find_grid(matrix, input_str, row_pos + 1, col_pos, row_count,
• col_count, degree + 1) bitwise or
• find_grid(matrix, input_str, row_pos, col_pos - 1, row_count,
• col_count, degree + 1) bitwise or
• find_grid(matrix, input_str, row_pos, col_pos + 1, row_count, col_count, degree + 1))
• replace elements of matrix[row_pos, col_pos] with temp
• return result
• otherwise,
• return False
• From the main function/method, do the following −
• if length of input_str > row_count * col_count, then
• return False
• for row in range 0 to row_count, do
• for col in range 0 to col_count, do
• if matrix[row, col] is same as input_str[0], then
• if (find_grid(matrix, input_str, row, col, row_count, col_count, 0) is True), then
• return True
• return False

## Example

Let us see the following implementation to get better understanding −

Live Demo

def find_grid(matrix, input_str, row_pos, col_pos, row_count, col_count, degree) :
if (degree == len(input_str)) :
return True
if (row_pos < 0 or col_pos < 0 or row_pos >= row_count or col_pos >= col_count) :
return Fals
if (matrix[row_pos][col_pos] == input_str[degree]) :
temp = matrix[row_pos][col_pos]
matrix[row_pos].replace(matrix[row_pos][col_pos], "#")
result = (find_grid(matrix, input_str, row_pos - 1, col_pos, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos + 1, col_pos, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos, col_pos - 1, row_count, col_count, degree + 1) |find_grid(matrix, input_str, row_pos, col_pos + 1, row_count, col_count, degree + 1))
matrix[row_pos].replace(matrix[row_pos][col_pos], temp)
return result
else :
return False
def solve(matrix, input_str, row_count, col_count) :
if (len(input_str) > row_count * col_count) :
return False
for row in range(row_count) :
for col in range(col_count) :
if (matrix[row][col] == input_str[0]) :
if (find_grid(matrix, input_str, row, col, row_count, col_count, 0)) :
return True
return False
word_grid = ["pghsf", "ykdgh", "tkghi", "hnsjs", "ojfgh", "nrtyu"]
print(solve(word_grid, "python", 6, 5))

## Input

["pghsf", "ykdgh", "tkghi", "hnsjs", "ojfgh", "nrtyu"],"python"

True