-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWord_Search.py
More file actions
56 lines (41 loc) · 1.52 KB
/
Word_Search.py
File metadata and controls
56 lines (41 loc) · 1.52 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
50
51
52
53
54
55
56
from typing import List
class Solution:
def dfs(self, i, j, board, index, words):
if i >= len(board) or j >= len(board[i]) or i < 0 or j < 0:
return False
if index >= len(words):
return False
if board[i][j] != words[index]:
return False
if index == len(words) - 1:
return True
board[i][j] = "#"
res = self.dfs(i - 1, j, board, index + 1, words) or \
self.dfs(i + 1, j, board, index + 1, words) or \
self.dfs(i, j + 1, board, index + 1, words) or \
self.dfs(i, j - 1, board, index + 1, words)
board[i][j] = words[index]
return res
def exist(self, board: List[List[str]], word: str) -> bool:
words = list(word)
for i in range(len(board)):
for j in range(len(board[i])):
if words[0] == board[i][j]:
result = self.dfs(i, j, board, 0, words)
if result:
return True
return False
if __name__ == '__main__':
board = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
]
sol = Solution()
print(sol.exist(board, "ABCCED"))
print(sol.exist(board, "SEE"))
print(sol.exist(board, "ABCB"))
board2 = [["C", "A", "A"], ["A", "A", "A"], ["B", "C", "D"]]
print(sol.exist(board2, "AAB"))
board3 = [["A", "B", "C", "E"], ["S", "F", "E", "S"], ["A", "D", "E", "E"]]
print(sol.exist(board3, "ABCESEEEFS"))