forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word Search.py
51 lines (38 loc) · 1.29 KB
/
Word Search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import List
class Solution:
"""
Time: O(n*m*k)
Memory: O(k)
where k - word length
"""
VISITED = '#'
def exist(self, board: List[List[str]], word: str) -> bool:
n, m = len(board), len(board[0])
beginning = end = 0
for r in range(n):
for c in range(m):
if board[r][c] == word[0]:
beginning += 1
elif board[r][c] == word[-1]:
end += 1
if beginning > end:
word = word[::-1]
for i in range(n):
for j in range(m):
if self.dfs(board, i, j, 0, word):
return True
return False
@classmethod
def dfs(cls, board: List[List[str]], i: int, j: int, k: int, word: str) -> bool:
if k == len(word):
return True
n, m = len(board), len(board[0])
if (i < 0 or i >= n) or (j < 0 or j >= m) or word[k] != board[i][j]:
return False
board[i][j] = cls.VISITED
res = cls.dfs(board, i + 1, j, k + 1, word) or \
cls.dfs(board, i - 1, j, k + 1, word) or \
cls.dfs(board, i, j + 1, k + 1, word) or \
cls.dfs(board, i, j - 1, k + 1, word)
board[i][j] = word[k]
return res