Skip to content

Commit 19d1776

Browse files
Solve : Word Search
1 parent c0feba2 commit 19d1776

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

word-search/printjin-gmailcom.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
class Solution:
4+
def exist(self, board, word):
5+
m, n = len(board), len(board[0])
6+
def dfs(x, y, index):
7+
if index == len(word):
8+
return True
9+
if x < 0 or x >= m or y < 0 or y >= n or board[x][y] != word[index]:
10+
return False
11+
temp = board[x][y]
12+
board[x][y] = '#'
13+
found = (
14+
dfs(x + 1, y, index + 1) or dfs(x - 1, y, index + 1) or dfs(x, y + 1, index + 1) or dfs(x, y - 1, index + 1)
15+
)
16+
board[x][y] = temp
17+
return found
18+
19+
for i in range(m):
20+
for j in range(n):
21+
if dfs(i, j, 0):
22+
return True
23+
return False

0 commit comments

Comments
 (0)