-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_Search.py
33 lines (27 loc) · 1.04 KB
/
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
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
'''
["C","A","A"],
["A","A","A"],
["B","C","D"]
Word = 'AAB'
'''
ROWS, COLS = len(board), len(board[0])
visited, DIRECTIONS = set(), [[0,1],[1,0],[0,-1],[-1,0]]
def in_bounds(r, c):
return 0 <= r < ROWS and 0 <= c < COLS
def bt(r, c, wIdx= 0, s=''):
if not in_bounds(r, c) or (r, c) in visited or wIdx >= len(word) or board[r][c] != word[wIdx]: return
if wIdx == len(word) - 1:
return True
visited.add((r, c))
for dx, dy in DIRECTIONS:
nx, ny = r + dx, c + dy
if in_bounds(nx, ny) and (nx, ny) not in visited:
if bt(nx, ny, wIdx + 1, s + board[r][c]): return True
visited.remove((r, c))
return False
for r in range(ROWS):
for c in range(COLS):
if bt(r, c): return True
return False