|
| 1 | +""" |
| 2 | +79. Word Search |
| 3 | +https://leetcode.com/problems/word-search/ |
| 4 | +
|
| 5 | +Solution: |
| 6 | + To solve this problem, we think of the board as a graph. |
| 7 | + We can use a depth-first search (DFS) to explore all possible paths starting from each cell. |
| 8 | + We can create a helper function that takes the current cell and the index of the current character in the word. |
| 9 | +
|
| 10 | + - We can count the frequency of each character in the board. |
| 11 | + - We can check if all characters in the word exist in the board. |
| 12 | + - If we cannot find all characters in the board, we return False. |
| 13 | +
|
| 14 | + - We can create a helper function that takes the current coordinates and the index in the word. |
| 15 | + - If the index is equal to the length of the word, |
| 16 | + it means we have found all characters in the word, so we return True. |
| 17 | + - If the coordinates are out of bounds or the current character does not match, |
| 18 | + we return False. |
| 19 | + - We mark the current cell as visited and explore all possible directions. |
| 20 | + - If any direction returns True, we return True. |
| 21 | + - We unmark the current cell and return False. |
| 22 | +
|
| 23 | + - We can find all indices of the first character in the word. |
| 24 | + - We can start the DFS from each index and return True if any DFS returns True. |
| 25 | + - If no DFS returns True, we return False. |
| 26 | +
|
| 27 | +Time complexity: O(m*n*4^l) |
| 28 | + - m and n are the dimensions of the board. |
| 29 | + - l is the length of the word. |
| 30 | + - We explore 4 directions at each cell, and the maximum depth is the length of the word. |
| 31 | +
|
| 32 | +Space complexity: O(l) |
| 33 | + - The recursive call stack has a maximum depth of the length of the word. |
| 34 | + |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +from typing import List |
| 39 | + |
| 40 | + |
| 41 | +class Solution: |
| 42 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 43 | + if not board or not word: |
| 44 | + return False |
| 45 | + |
| 46 | + m, n = len(board), len(board[0]) |
| 47 | + |
| 48 | + # Step 1: Check if all characters in the word exist in the board |
| 49 | + char_count = {} |
| 50 | + for row in board: |
| 51 | + for char in row: |
| 52 | + if char in char_count: |
| 53 | + char_count[char] += 1 |
| 54 | + else: |
| 55 | + char_count[char] = 1 |
| 56 | + |
| 57 | + for char in word: |
| 58 | + if char not in char_count or char_count[char] == 0: |
| 59 | + return False |
| 60 | + char_count[char] -= 1 |
| 61 | + |
| 62 | + # Helper function to check if the word can be found starting from (i, j) |
| 63 | + def dfs(i, j, word_index): |
| 64 | + if word_index == len(word): |
| 65 | + return True |
| 66 | + |
| 67 | + if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[word_index]: |
| 68 | + return False |
| 69 | + |
| 70 | + temp = board[i][j] |
| 71 | + board[i][j] = "#" # mark as visited |
| 72 | + |
| 73 | + # Explore all possible directions |
| 74 | + found = ( |
| 75 | + dfs(i + 1, j, word_index + 1) |
| 76 | + or dfs(i - 1, j, word_index + 1) |
| 77 | + or dfs(i, j + 1, word_index + 1) |
| 78 | + or dfs(i, j - 1, word_index + 1) |
| 79 | + ) |
| 80 | + |
| 81 | + board[i][j] = temp # unmark |
| 82 | + return found |
| 83 | + |
| 84 | + # Step 2: Find all indices of the first character in the word |
| 85 | + for i in range(m): |
| 86 | + for j in range(n): |
| 87 | + if board[i][j] == word[0] and dfs(i, j, 0): |
| 88 | + return True |
| 89 | + |
| 90 | + return False |
0 commit comments