-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path079__Word Search.py
More file actions
33 lines (25 loc) · 855 Bytes
/
Copy path079__Word Search.py
File metadata and controls
33 lines (25 loc) · 855 Bytes
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
class Solution:
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
#This is called dfs.
def dfs(board,i,j,word):
if len(word)==0:
return True
if i<0 or i>=len(board) or j<0 or j>=len(board[0]) or board[i][j]!=word[0]:
return False
tmp=board[i][j]
board[i][j]="#"
res=dfs(board,i+1,j,word[1:])or dfs(board,i-1,j,word[1:]) or dfs(board,i,j-1,word[1:]) or dfs(board,i,j+1,word[1:])
board[i][j]=tmp
return res
if not board:
return False
for i in range(len(board)):
for j in range(len(board[0])):
if dfs(board,i,j,word):
return True
return False