Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8055

# https://leetcode.

com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False
for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True
return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()
def dfs(r, c, i):
if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False
path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False
# https://leetcode.com/problems/word-search/description/

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS, COLS = len(board), len(board[0])
n = len(word)
path = set()

def dfs(r, c, i):


if i==n:
return True
if (r<0 or c<0 or
r>=ROWS or c>=COLS or
word[i] != board[r][c] or
(r,c) in path):
return False

path.add((r,c))
if dfs(r+1, c, i+1): return True
if dfs(r, c-1, i+1): return True
if dfs(r, c+1, i+1): return True
if dfs(r-1, c, i+1): return True
# res = (dfs(r+1, c, i+1) or
# dfs(r, c-1, i+1) or
# dfs(r, c+1, i+1) or
# dfs(r-1, c, i+1) )
path.remove((r,c))
return False

for i in range(ROWS):
for j in range(COLS):
if dfs(i,j,0): return True

return False

# https://leetcode.com/problems/word-search/description/

class

You might also like