-
Notifications
You must be signed in to change notification settings - Fork 0
/
79. Word Search.py
34 lines (31 loc) · 954 Bytes
/
79. 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
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
def dfs(i, j, d):
if i<0 or j<0 or i==h or j==w or board[i][j] != word[d]: return False
tmp = board[i][j]
if board[i][j] == word[d]: board[i][j] = '#' # avoid visting again
if d == len(word)-1: return True
res = dfs(i - 1, j, d + 1) \
or dfs(i + 1, j, d + 1) \
or dfs(i, j - 1, d + 1) \
or dfs(i, j + 1, d + 1)
board[i][j] = tmp
return res
if not board: return False
h = len(board)
w = len(board[0])
for i in xrange(h):
for j in xrange(w):
if dfs(i, j, 0):
return True
return False
board =[
['a','a']
]
S = Solution()
print S.exist(board, "aaa")