MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / find

Method find

DFS/WordSearch.py:378–416  ·  view source on GitHub ↗
(board, x, y, word)

Source from the content-addressed store, hash-verified

376 :rtype: bool
377 """
378 def find(board, x, y, word):
379 if not word:
380 return True
381
382
383 if board[y][x] == word[0]:
384 raw = board[y][x]
385 board[y][x] = 0
386 if len(word) == 1:
387 board[y][x] = raw
388 return True
389 else:
390 return False
391
392 # up
393 if y-1 >= 0 and board[y-1][x] == word[1]:
394 if find(board, x, y-1, word[1:]):
395 board[y][x] = raw
396 return True
397 # down
398 if y+1 < len(board) and board[y+1][x] == word[1]:
399 if find(board, x, y+1, word[1:]):
400 board[y][x] = raw
401 return True
402
403 # left
404 if x-1 >= 0 and board[y][x-1] == word[1]:
405 if find(board, x-1, y, word[1:]):
406 board[y][x] = raw
407 return True
408
409 # right
410 if x+1 < len(board[0]) and board[y][x+1] == word[1]:
411 if find(board, x+1, y, word[1:]):
412 board[y][x] = raw
413 return True
414
415 board[y][x] = raw
416 return False
417
418 for i in range(len(board)):
419 for j in range(len(board[0])):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected