forked from franklingu/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
56 lines (48 loc) · 1.8 KB
/
Solution.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
"""
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
"""
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def find_neighbors(position, board, target):
deltas = [(1, 0), (0, 1), (-1, 0), (0, -1)]
for y_d, x_d in deltas:
y, x = position[0] + y_d, position[1] + x_d
if not (0 <= y < len(board)):
continue
if not (0 <= x < len(board[0])):
continue
if board[y][x] == target:
yield (y, x)
def begin_search(position, word, board):
stack = [(position, set(), 1)]
while stack:
position, prevs, index = stack.pop()
if index >= len(word):
return True
target = word[index]
for ne in find_neighbors(position, board, target):
if ne in prevs:
continue
stack.append((ne, prevs.union(set([position])), index + 1))
return False
if not word:
return True
for i, row in enumerate(board):
for j, ch in enumerate(row):
if ch != word[0]:
continue
found = begin_search((i, j), word, board)
if found:
return True
return False