-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexist.py
61 lines (45 loc) · 1.86 KB
/
exist.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def dfs(path:str, y:int, x:int):
# check for word
# mark visited
# go to valid neighbors
if len(path) == 0:
return True
if x < 0 or y < 0 or y == len(board) or x == len(board[y]) or board[y][x] != path[:1]:
return False
tmp = board[y][x]
board[y][x] = '#'
if dfs(path[1:], y-1, x) or \
dfs(path[1:], y+1, x) or \
dfs(path[1:], y, x+1) or \
dfs(path[1:], y, x-1):
return True
board[y][x] = tmp
# double loop to check all starting places
for y in range(0, len(board)):
for x in range(0, len(board[y])):
if dfs(word, y, x):
return True
return False;
class Solution:
def exist(self, board:List[List[str]], word:str) -> bool:
def dfs(path:str, y:int, x:int):
if len(path) == 0:
return True
if (y<0 or x<0 or y==len(board) or \
x==len(board[y]) or board[y][x] != path[0]):
return False
tmp = board[y][x]
board[y][x] = '#'
if dfs(path[1:], y-1, x) or \
dfs(path[1:], y+1, x) or \
dfs(path[1:], y, x-1) or \
dfs(path[1:], y, x+1):
return True
board[y][x] = tmp
for y in range(len(board)):
for x in range(len(board[y])):
if dfs(word, y, x):
return True
return False