classSolution:defexist(self,board: List[List[str]],word:str) ->bool: m =len(board) n =len(board[0])for i inrange(m):for j inrange(n):if self.dfs(i, j, 0, board, word):returnTruereturnFalsedefdfs(self,x,y,k,board,word):if k ==len(word):returnTrueif x <0or x >=len(board)or y <0or y >=len(board[0])or board[x][y] != word[k]:returnFalse mem = board[x][y] board[x][y] ='*' res = self.dfs(x+1, y, k+1, board, word)or self.dfs(x, y+1, k+1, board, word)or self.dfs(x-1, y, k+1, board, word)or self.dfs(x, y-1, k+1, board, word) board[x][y] = memreturn res