This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079_word_search.py
61 lines (53 loc) · 1.62 KB
/
0079_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
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
"""
neetcode - backtracking - 6
sol:
iterate through the the 2darr to find start point
call recursion at each potential start
recursion returns when ipointer reaches word len
if cur r, c is in bounds, and pointing to desired char
add to path
recurse with neighbors
remove from path
if any of the start points have a valid path, return True
else false
"""
class Solution:
def exist(self, board: list[list[str]], word: str) -> bool:
height = len(board)
width = len(board[0])
path = set()
def dfs(r: int, c: int, i: int) -> bool:
# if i reaches len(word), finished
if i == len(word):
return True
# ensure r, c in boundds and pointing to desired letter and not visited
if not (
0 <= r < height
and 0 <= c < width
and board[r][c] == word[i]
and (r, c) not in path
):
return False
path.add((r, c))
# if one of the child paths is successful, the whole thing is
res = (
dfs(r - 1, c, i + 1)
or dfs(r + 1, c, i + 1)
or dfs(r, c - 1, i + 1)
or dfs(r, c + 1, i + 1)
)
path.remove((r, c))
return res
# find start
for r in range(height):
for c in range(width):
# recurse
if dfs(r, c, 0):
return True
return False
s = Solution()
print(
s.exist(
[["A", "B", "C", "E"], ["S", "F", "E", "S"], ["A", "D", "E", "E"]], "ABCESEEEFS"
)
)