forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
25 lines (24 loc) · 800 Bytes
/
Solution.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
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def dfs(i, j, cur):
if cur == len(word):
return True
if (
i < 0
or i >= m
or j < 0
or j >= n
or board[i][j] == '0'
or word[cur] != board[i][j]
):
return False
t = board[i][j]
board[i][j] = '0'
for a, b in [[0, 1], [0, -1], [-1, 0], [1, 0]]:
x, y = i + a, j + b
if dfs(x, y, cur + 1):
return True
board[i][j] = t
return False
m, n = len(board), len(board[0])
return any(dfs(i, j, 0) for i in range(m) for j in range(n))