generated from foambubble/foam-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsolution2.py
More file actions
29 lines (23 loc) · 1.02 KB
/
solution2.py
File metadata and controls
29 lines (23 loc) · 1.02 KB
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
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
n = len(word)
N = len(board)
M = len(board[0])
def dfs(x,y,i):
if i == n: return True
if not (0<=x<N and 0<=y<M): return False
if board[x][y]!=word[i]: return False
board[x][y] = ' '
path = [(-1,0),(1,0),(0,-1),(0,1)]
result = any(dfs(x+r,y+m,i+1) for r,m in path)
board[x][y] = word[i]
return result
C,starts = Counter(word),[]
for x in range(N):
for y in range(M):
C[board[x][y]] -= 1
if board[x][y] == word[0]: starts.append((x,y))
if max(C.values())>0: return False
for r,c in starts:
if dfs(r,c,0): return True
return False