0

Why is True not being returned to the "exist" main func when the first if statement in dfs is being hit? I'm sure I'm just missing something simple.

Leetcode question: https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/797/

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        
        def dfs(x=0, y=0, found=0, seen = set()):
            if found == len(word):
                return True

            elif x < len(board) and y < len(board[0]) and board[x][y] == word[found]:
                if x > 0 and (x,y) not in seen:
                    seen.add((x,y))
                    dfs(x-1,y,found+1, seen.copy())
                    seen.remove((x,y))
                if x < len(board) and (x,y) not in seen:
                    seen.add((x,y))
                    dfs(x+1,y,found+1, seen.copy())
                    seen.remove((x,y))
                if y > 0 and (x,y) not in seen:
                    seen.add((x,y))
                    dfs(x,y-1,found+1, seen.copy())
                    seen.remove((x,y))
                if y < len(board[0]) and (x,y) not in seen:
                    seen.add((x,y))
                    dfs(x,y+1,found+1, seen.copy())
                    seen.remove((x,y))


        return dfs()
Countour-Integral
  • 1,138
  • 2
  • 7
  • 21
SGuyette
  • 11
  • 2

0 Answers0