-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path79_word_search.py
More file actions
49 lines (36 loc) · 1.18 KB
/
Copy path79_word_search.py
File metadata and controls
49 lines (36 loc) · 1.18 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import List
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
row = len(board)
column = len(board[0])
visted = [[0 for _ in range(column)] for _ in range(row)]
offsets = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def dfs(i: int, j: int, level: int) -> bool:
if visted[i][j] or board[i][j] != word[level]:
return False
if level == len(word) - 1:
return True
visted[i][j] = 1
for (x_, y_) in offsets:
x, y = i + x_, j + y_
if 0 <= x < row and 0 <= y < column and dfs(x, y, level + 1):
return True
visted[i][j] = 0
return False
for r in range(row):
for c in range(column):
if dfs(r, c, 0):
return True
return False
def test():
board = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
]
s = Solution()
assert s.exist(board, "ABCCED")
assert s.exist(board, "SEE")
assert not s.exist(board, "ABCB")
if __name__ == "__main__":
test()