
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Word Exists in a Grid 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
- if degree is same as size of input_str , then
- 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
- if (find_grid(matrix, input_str, row, col, row_count, col_count, 0) is True), then
- if matrix[row, col] is same as input_str[0], then
- for col in range 0 to col_count, do
- return False
Example
Let us see the following implementation to get better understanding −
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"
Output
True
Advertisements