-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-search.py
32 lines (20 loc) · 1012 Bytes
/
word-search.py
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
# https://leetcode.com/problems/word-search/
class Solution:
def DFS(self,row, col, curr, board, word):
if 0 <= row < len(board) and 0 <= col < len(board[0]) and board[row][col] == word[curr]:
temp = board[row][col]
board[row][col] = '#'
if curr == len(word)-1:
return True
result = self.DFS(row-1, col, curr+1, board, word) or self.DFS(row+1, col, curr+1, board, word) or self.DFS(row, col-1, curr+1, board, word) or self.DFS(row, col+1, curr+1, board, word)
board[row][col] = temp
return result
else:
return False
def exist(self, board: List[List[str]], word: str) -> bool:
for row in range(len(board)):
for col in range(len(board[0])):
check = self.DFS(row, col, 0, board, word)
if check:
return True
return False