forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_098.py
52 lines (42 loc) · 1.57 KB
/
problem_098.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
def check_new_coordinate(word, row, col, used_coordinates):
expected_char = word[0]
copy_coordinates = used_coordinates.copy()
char = board[row][col]
result = False
if expected_char == char and "{}-{}".format(row, col) not in copy_coordinates:
copy_coordinates.add("{}-{}".format(row, col))
result = existence_helper(
word[1:], board, row, col, copy_coordinates)
return result
def existence_helper(word, board, crow, ccol, used_coordinates):
if not word:
return True
top, bottom, left, right = (False, False, False, False)
if crow > 0:
top = check_new_coordinate(word, crow - 1, ccol, used_coordinates)
if crow < len(board) - 1:
bottom = check_new_coordinate(word, crow + 1, ccol, used_coordinates)
if ccol > 0:
left = check_new_coordinate(word, crow, ccol - 1, used_coordinates)
if ccol < len(board[0]) - 1:
right = check_new_coordinate(word, crow, ccol + 1, used_coordinates)
return top or bottom or left or right
def exists(board, word):
if not word:
return False
first_char = word[0]
result = False
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] == first_char:
result = result or existence_helper(
word[1:], board, row, col, set(["{}-{}".format(row, col)]))
return result
board = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
]
assert exists(board, "ABCCED")
assert exists(board, "SEE")
assert not exists(board, "ABCB")