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
+ class Solution :
41
+ def exist (self , board : List [List [str ]], word : str ) -> bool :
42
+ if not board or not word :
43
+ return False
44
+
45
+ m , n = len (board ), len (board [0 ])
46
+
47
+ # Step 1: Check if all characters in the word exist in the board
48
+ char_count = {}
49
+ for row in board :
50
+ for char in row :
51
+ if char in char_count :
52
+ char_count [char ] += 1
53
+ else :
54
+ char_count [char ] = 1
55
+
56
+ for char in word :
57
+ if char not in char_count or char_count [char ] == 0 :
58
+ return False
59
+ char_count [char ] -= 1
60
+
61
+ # Helper function to check if the word can be found starting from (i, j)
62
+ def dfs (i , j , word_index ):
63
+ if word_index == len (word ):
64
+ return True
65
+
66
+ if i < 0 or i >= m or j < 0 or j >= n or board [i ][j ] != word [word_index ]:
67
+ return False
68
+
69
+ temp = board [i ][j ]
70
+ board [i ][j ] = "#" # mark as visited
71
+
72
+ # Explore all possible directions
73
+ found = (dfs (i + 1 , j , word_index + 1 ) or
74
+ dfs (i - 1 , j , word_index + 1 ) or
75
+ dfs (i , j + 1 , word_index + 1 ) or
76
+ dfs (i , j - 1 , word_index + 1 ))
77
+
78
+ board [i ][j ] = temp # unmark
79
+ return found
80
+
81
+ # Step 2: Find all indices of the first character in the word
82
+ for i in range (m ):
83
+ for j in range (n ):
84
+ if board [i ][j ] == word [0 ] and dfs (i , j , 0 ):
85
+ return True
86
+
87
+ return False
0 commit comments