forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0079_Word_Search.py
More file actions
35 lines (25 loc) · 1.01 KB
/
Copy path0079_Word_Search.py
File metadata and controls
35 lines (25 loc) · 1.01 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
def exist(self, grid: List[List[str]], word: str) -> bool:
seen = set()
# Boundary checking
def inBound(row, col, grid):
return 0 <= row < len(grid) and 0 <= col < len(grid[0])
def dfs(grid, word, curr_i, row, col):
if curr_i == len(word):
return True
if not inBound(row, col,grid) or grid[row][col] != word[curr_i]:
return False
seen.add((row, col))
# Explore possible paths
pos_paths = [(row, col + 1), (row, col - 1), (row + 1, col), (row - 1, col)]
for row_n, col_n in pos_paths:
if (row_n,col_n) not in seen:
if dfs(grid, word, curr_i + 1, row_n, col_n):
return True
seen.remove((row,col))
return False
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == word[0]:
if dfs(grid, word, 0, row, col):
return True
return False