|
| 1 | +# Word Search |
| 2 | +# https://leetcode.com/problems/word-search/ |
| 3 | +class Solution(object): |
| 4 | + |
| 5 | + def findIfAllLettersInBoard(self, board, word): |
| 6 | + letterHash = {} |
| 7 | + # Count all letters on the board and put it in a hash table. |
| 8 | + for row in range(len(board)): |
| 9 | + for col in range(len(board[0])): |
| 10 | + if not (board[row][col] in letterHash): |
| 11 | + letterHash[board[row][col]] = 0 |
| 12 | + letterHash[board[row][col]] = letterHash[board[row][col]] + 1 |
| 13 | + # see if the word has enough letters that are present on the board |
| 14 | + for letter in word: |
| 15 | + # all letters from the word are not present on the board |
| 16 | + if (letter not in letterHash or letterHash[letter] == 0): |
| 17 | + return False |
| 18 | + letterHash[letter] = letterHash[letter] - 1 |
| 19 | + return True |
| 20 | + |
| 21 | + def isValid(self, row, col, board, visited): |
| 22 | + if (row >= 0 and row < len(board)): |
| 23 | + if (col >= 0 and col < len(board[0])): |
| 24 | + if ([row, col] not in visited): |
| 25 | + return True |
| 26 | + |
| 27 | + def nextMoves(self, board, row, col, visited): |
| 28 | + move = [] |
| 29 | + # up |
| 30 | + if (self.isValid(row-1, col, board, visited)): |
| 31 | + move.append([row-1, col]) |
| 32 | + # down |
| 33 | + if (self.isValid(row+1, col, board, visited)): |
| 34 | + move.append([row+1, col]) |
| 35 | + # left |
| 36 | + if (self.isValid(row, col-1, board, visited)): |
| 37 | + move.append([row, col-1]) |
| 38 | + # right |
| 39 | + if (self.isValid(row, col+1, board, visited)): |
| 40 | + move.append([row, col+1]) |
| 41 | + # return moves |
| 42 | + return move |
| 43 | + |
| 44 | + def dfs(self, board, row, col, visited, word, index): |
| 45 | + if (index == len(word)): |
| 46 | + return True |
| 47 | + # get next moves |
| 48 | + moves = self.nextMoves(board, row, col, visited) |
| 49 | + # iterate through next moves |
| 50 | + for move in moves: |
| 51 | + # if move has not been visited |
| 52 | + if (move not in visited): |
| 53 | + # if the cell has same letter as letter at word index |
| 54 | + if (board[move[0]][move[1]] == word[index]): |
| 55 | + # Mark current row, col visited in the function call, this helps in backtracking. |
| 56 | + if (self.dfs(board, move[0], move[1], visited+[[row, col]], word, index+1)): |
| 57 | + return True |
| 58 | + |
| 59 | + def exist(self, board, word): |
| 60 | + # Edge case to see if all letters in word are present in board |
| 61 | + if not(self.findIfAllLettersInBoard(board, word)): |
| 62 | + return False |
| 63 | + # Search for word in board |
| 64 | + for row in range(len(board)): |
| 65 | + for col in range(len(board[0])): |
| 66 | + if (board[row][col] == word[0]): |
| 67 | + if (self.dfs(board, row, col, [], word, 1)): |
| 68 | + return True |
| 69 | + return False |
| 70 | + """ |
| 71 | + :type board: List[List[str]] |
| 72 | + :type word: str |
| 73 | + :rtype: bool |
| 74 | + """ |
| 75 | + |
0 commit comments