You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution:
defexist(self, board: List[List[str]], word: str) ->bool:
height=len(board)
width=len(board[0])
used=set()
defbacktrack(h, w, c):
# Base caseifc==len(word):
returnTrueif (h>=heightorh<0orw>=widthorw<0# Tile is out of boundsorboard[h][w] !=word[c] # Tile does not contain the target letteror (h, w) inused): # Character has already been usedreturnFalse# Actions - check all adjacent tilesused.add((h, w))
answer= (backtrack(h, w-1, c+1) orbacktrack(h, w+1, c+1)
orbacktrack(h-1, w, c+1) orbacktrack(h+1, w, c+1))
# Undo the above actionused.remove((h, w))
returnanswer# Iterate over each tile on the boardforhinrange(height):
forwinrange(width):
ifbacktrack(h, w, 0):
returnTruereturnFalse
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: