
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
Word Search in Python
In Python word search refers to determining if a given word exists in the grid, this can be done using various approaches like DFS method and backtracking algorithm, etc, and the given word can be formed by sequentially connecting adjacent cells both horizontally or vertically.
Step Involved
The steps involved in performing word search in Python are as follows.
-
Consider the input board(2D grid)
-
Define the class and Implementation of exist() method, which iterates over each cell
-
Implementing of find method(), to recursively check the possible directions.
-
Execution
Input board
Let us consider a 2D board and a word, we have to find if it exists in the grid or not. For example, if the input word is "SEE", it will return true.
A | B | C | E |
S | F | C | S |
A | D | E | F |
Class definition and Exist Method
The Class Word_Search encapsulate the logic for the word search problem and,the exist() method checks if the word exists in the board.
Defining class
class Word_Search(object):
The exist() Method
In the below function 'n' and 'm' represent the number of rows and columns in the 2D grid. This method iterates over each cell in the board using two nested loops.
def exist(self, board, word): n = len(board) m = len(board[0]) for i in range(n): for j in range(m): if word[0] == board[i][j]: if self.find(board, word, i, j): return True return False
Initialization of Find Method
The find() method, recursively matches the characters of the word starting from a given cell (row, col).
find() method
In the below function, the i refers to (the index of the current character in the word) equals the length of the word. and returns True, if the entire word is successfully matched. if the condition fails, the function returns False.
def find(self, board, word, row, col, i=0): if i == len(word): return True if row >= len(board) or row < 0 or col >= len(board[0]) or col < 0 or word[i] != board[row][col]: return False board[row][col] = '*' res = self.find(board, word, row + 1, col, i + 1) or self.find(board, word, row - 1, col, i + 1) or self.find(board, word, row, col + 1, i + 1) or self.find(board, word, row, col - 1, i + 1) board[row][col] = word[i] return res
Execution
In the final step, an instance of the Word_Search class is created. The exist is called with a 2D board and the word "SEE" as a result based on the condition (if the given word exists or not) it prints 'True' or 'False'.
ob1 = Solution() print(ob1.exist([["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "SEE"))
Example
class Solution(object): def exist(self, board, word): n =len(board) m = len(board[0]) for i in range(n): for j in range(m): if word[0] == board[i][j]: if self.find(board,word,i,j): return True return False def find(self, board,word,row,col,i=0): if i== len(word): return True if row>= len(board) or row <0 or col >=len(board[0]) or col<0 or word[i]!=board[row][col]: return False board[row][col] = '*' res = self.find(board,word,row+1,col,i+1) or self.find(board,word,row-1,col,i+1) or self.find(board,word,row,col+1,i+1) or self.find(board,word,row,col-1,i+1) board[row][col] = word[i] return res ob1 = Solution() print(ob1.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"SEE"))
Input
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]] "SEE"
Output
True