Leetcode79. Python implementation of word search

Time:2021-8-21
  • Title Requirements:

Leetcode79. Python implementation of word search

  • Idea:

    • First traverse the array to find the possible starting point
    • Define a helper function to traverse the upper, lower, left and right elements of the current starting point to see if there is one that matches the next bit of a given word. If you call the helper function recursively again
    • Returns false when the upper, lower, left and right subscripts are out of bounds or the current array element is different from the letter of the current word
  • Core code:
#Traversing a two-dimensional array
for i in range(len(board)):
            for j in range(len(board[0])):
                   #Call helper function
                if self.helper(board,i,j,word,0):
                    return True
        return False
#Define helper functions:
def helper(self,board,i,j,word,index):
        #If the current subscript is equal to the length of the word, it indicates that all the preceding letters have found a match, and returns true
        if index == len(word):
            return True
        #Returns false if the current subscript is out of bounds, or if the current element letter is different from the letter in the word
        if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or board[i][j] != word[index]:
            return False
        #To mark the currently passed array elements
        board[i][j] = "*"
        #Recursively call the top, bottom, left and right of the current element to see if it is the same as the next letter of the word
        found = self.helper(board,i+1,j,word,index+1)\
                or self.helper(board,i,j+1,word,index+1)\
                or self.helper(board,i-1,j,word,index+1)\
                or self.helper(board,i,j-1,word,index+1)
        #Marked values to be restored   
        board[i][j] = word[index]
        return found
  • Full code:
class Solution(object):
    def exist(self, board, word):
        """
        :type board: List[List[str]]
        :type word: str
        :rtype: bool
        """
        for i in range(len(board)):
            for j in range(len(board[0])):
                if self.helper(board,i,j,word,0):
                    return True
        return False

    
    def helper(self,board,i,j,word,index):

        if index == len(word):
            return True

        if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or board[i][j] != word[index]:
            return False
        
        board[i][j] = "*"
        found = self.helper(board,i+1,j,word,index+1)\
                or self.helper(board,i,j+1,word,index+1)\
                or self.helper(board,i-1,j,word,index+1)\
                or self.helper(board,i,j-1,word,index+1)
        board[i][j] = word[index]
        return found

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]