forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079.py
30 lines (23 loc) · 943 Bytes
/
0079.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
if not board:
return False
for i in range(len(board)):
for j in range(len(board[0])):
if self.dfs(board, word, i, j, 0):
return True
return False
def dfs(self, board: List[List[str]], word: str, i: int, j: int, pos: int) -> bool:
if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]) or board[i][j] != word[pos] or board[i][j] == '*':
return False
if pos == len(word) - 1:
return True
c = board[i][j]
board[i][j] = '*'
flag = \
self.dfs(board, word, i + 1, j, pos + 1) or \
self.dfs(board, word, i - 1, j, pos + 1) or \
self.dfs(board, word, i, j + 1, pos + 1) or \
self.dfs(board, word, i, j - 1, pos + 1)
board[i][j] = c
return flag