-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_212.py
66 lines (54 loc) · 1.74 KB
/
leetcode_212.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def main(board: list[list[str]], words:list[str]):
found = []
rows = len(board)
cols = len(board[0])
i = 0
j = 0
while i < rows:
j = 0
while j < cols:
for word in words.copy():
result = findWord(board, word, i, j, rows, cols, [])
if result:
words.remove(word)
found.append(word)
j += 1
i += 1
return found
def findWord(board: list[list[str]], word: str, k: int, l: int, rows: int, cols: int, usedCells: list[tuple[int, int]]):
# this is a recuresive function
wordLen = len(word)
if wordLen == 0:
return True
if board[k][l] == word[0]:
copy = usedCells.copy()
copy.append((k, l))
adjacent = getAdjacent(k, l, rows, cols, copy)
if not adjacent and wordLen == 1:
return True
for k, l in adjacent:
result = findWord(board, word[1:], k, l, rows, cols, copy)
if result:
return True
else:
return False
def getAdjacent(i: int, j: int, rows: int, cols: int, usedCells: list[tuple[int, int]]):
adjacent = []
if 0 <= i+1 < rows and (i + 1, j) not in usedCells:
adjacent.append((i + 1, j))
if 0 <= i-1 < rows and (i - 1, j) not in usedCells:
adjacent.append((i - 1, j))
if 0 <= j+1 < cols and (i, j + 1) not in usedCells:
adjacent.append((i, j+1))
if 0 <= j-1 < cols and (i, j - 1) not in usedCells:
adjacent.append((i, j - 1))
return adjacent
print(main([["o","a","b","n"],["o","t","a","e"],["a","h","k","r"],["a","f","l","v"]], ["oa","oaa"]))
"""
[
["o","a","b","n"],
["o","t","a","e"],
["a","h","k","r"],
["a","f","l","v"]
]
"""