class Solution:
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
for y in range(len(board)):
for x in range(len(board[0])):
if self.dfs(board,word,x,y,0):
return True
return False
def dfs(self,board,word,x,y,i):
if i == len(word):
return True
if x < 0 or x >= len(board[0]) or y < 0 or y >= len(board):
return False
if board[y][x] != word[i]:
return False
board[y][x] = board[y][x].swapcase()
isdfs = self.dfs(board,word,x+1,y,i+1) or self.dfs(board,word,x,y+1,i+1) or self.dfs(board,word,x-1,y,i+1) or self.dfs(board,word,x,y-1,i+1)
board[y][x] = board[y][x].swapcase()
return isdfs
09-13
10-31
10-31
10-31
10-31