-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
28 lines (21 loc) · 747 Bytes
/
Copy pathsolution.py
File metadata and controls
28 lines (21 loc) · 747 Bytes
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
class Solution:
def isWordExist(self, mat, word):
n, m = len(mat), len(mat[0])
directions = [(1,0), (-1,0), (0,1), (0,-1)]
def dfs(i, j, idx):
if idx == len(word):
return True
if i < 0 or j < 0 or i >= n or j >= m or mat[i][j] != word[idx]:
return False
temp = mat[i][j]
mat[i][j] = '#'
for dx, dy in directions:
if dfs(i + dx, j + dy, idx + 1):
return True
mat[i][j] = temp
return False
for i in range(n):
for j in range(m):
if dfs(i, j, 0):
return True
return False