-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0079_Word_Search.py
More file actions
25 lines (23 loc) · 911 Bytes
/
Copy path0079_Word_Search.py
File metadata and controls
25 lines (23 loc) · 911 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
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
# time: O(R×C×4^L)
# space: O(4L)
def dfs(i, j, word_i, visited):
if board[i][j] != word[word_i]:
return False
if word_i == len(word) - 1:
return True
visited.add((i, j))
for d_i, d_j in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
i2, j2 = i + d_i, j + d_j
if i2 < 0 or i2 >= len(board) or j2 < 0 or j2 >= len(board[0]) or (i2, j2) in visited:
continue
if dfs(i + d_i, j + d_j, word_i + 1, visited):
return True
visited.remove((i, j))
return False
for i in range(len(board)):
for j in range(len(board[0])):
if dfs(i, j, 0, set()):
return True
return False