TLE->8000ms->3500ms | Finally Python with NO "TLE"!! | Pro optimization | Hushh
Optimization number 1\n Using if -elif instead of for loop helped me resolve TLE:\nAs if will return as soon as it gets TRUE, for will keep checking next condit
**Optimization number 1**\n* Using if -elif instead of for loop helped me resolve TLE:\nAs if will return as soon as it gets TRUE, for will keep checking next conditions.\n(Even multiple "or" did\'nt work for me)\n\n**"PLEASE UPVOTE FOR MY AN HOUR SPENT"**\n\n```\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -> bool:         \n\t\t# as start is not restricted to 0,0 we can start from anywhere, will loop\n        for i in range(len(board)):\n            for j in range(len(board[0])):\n                if self.dfs(i,j,word, board, 0):\n                    return True\n        return False\n    \n    def dfs(self, x, y, word, board, c):\n\t\t# return True if can make word starting from x, y\n        if len(word)==c:\n            return True    \n\t\t# Exit if out of board or char is not required\n        if x<0 or y<0 or x>=len(board) or y>=len(board[0]) or board[x][y]!=word[c]:\n            return False      \n\t\t# move ahead by marking board in path just not to visit again\n        board[x][y] = "#"\n        if self.dfs(x+1, y, word, board, c+1):\n            return True\n        elif self.dfs(x, y+1, word, board, c+1):\n            return True\n        elif self.dfs(x-1, y, word, board, c+1):\n            return True\n        elif self.dfs(x, y-1, word, board, c+1):\n            return True \n\t\t# if from x,y no one making True, we need to restore x, y as it is not in path\n\t\t# so it is open to use by other paths\n        board[x][y] = word[c]\n        return False\n```\n\n**Optimization Pro Max number 2**\n* Check char in starting only\n```\nclass Solution:\n    def exist(self, board: List[List[str]], word: str) -> bool:       \n        # Count number of letters in board and store it in a dictionary\n        boardDic = collections.defaultdict(int)\n        for i in range(len(board)):\n            for j in range(len(board[0])):\n                boardDic[board[i][j]] += 1\n\n        # Count number of letters in word\n        # Check if board has all the letters in the word and they are atleast same count from word\n        wordDic = collections.Counter(word)\n        for c in wordDic:\n            if c not in boardDic or boardDic[c] < wordDic[c]:\n                return False\n            \n        for i in range(len(board)):\n            for j in range(len(board[0])):\n                if self.dfs(i,j,word, board, 0):\n                    return True\n        return False\n    \n    def dfs(self, x, y, word, board, c):\n        if len(word)==c:\n            return True    \n        if x<0 or y<0 or x>=len(board) or y>=len(board[0]) or board[x][y]!=word[c]:\n            return False      \n        board[x][y] = "#"\n        if self.dfs(x+1, y, word, board, c+1):\n            return True\n        elif self.dfs(x, y+1, word, board, c+1):\n            return True\n        elif self.dfs(x-1, y, word, board, c+1):\n            return True\n        elif self.dfs(x, y-1, word, board, c+1):\n            return True \n        board[x][y] = word[c]\n        return False\n```\n![image](https://assets.leetcode.com/users/images/f789f496-2899-40a4-8966-55302cc3843f_1667244477.9827275.jpeg)\n
['Depth-First Search', 'Python']