-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_Search.py
54 lines (47 loc) · 1.91 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
52
53
54
# https://leetcode.com/problems/word-search/submissions/
class Solution:
def exist(self, board: 'List[List[str]]', word: 'str') -> 'bool':
num_of_row = len(board) - 1
if num_of_row >= 0:
num_of_col = len(board[0]) - 1
N = len(word)
def get_neigbours(row_pos, col_pos):
neighbours = []
if col_pos < num_of_col:
neighbours.append((row_pos, col_pos+1))
if col_pos > 0:
neighbours.append((row_pos, col_pos-1))
if row_pos < num_of_row:
neighbours.append((row_pos+1, col_pos))
if row_pos > 0:
neighbours.append((row_pos-1, col_pos))
return neighbours
def exist(word, curr_row, curr_col, wrd_start, N, path):
if wrd_start == N:
return True
if board[curr_row][curr_col] != word[wrd_start]:
return False
neighbours = get_neigbours(curr_row, curr_col)
for neigbour in neighbours:
if neigbour in path:
continue
path.append(neigbour)
is_exist = exist(
word, neigbour[0], neigbour[1], wrd_start+1, N, path)
if is_exist:
return True
path.pop()
return N-1 == wrd_start
for i in range(num_of_row + 1):
for j in range(num_of_col + 1):
path = [(i, j)]
is_exist = exist(word, i, j, 0, N, path)
if is_exist:
return True
return False
assert(Solution().exist([["A", "B", "C", "E"], [
"S", "F", "C", "S"], ["A", "D", "E", "E"]], "ABCCED"))
assert(Solution().exist([["a", "a"]], "aaa") == False)
assert(Solution().exist([["A", "B", "C", "E"],
["S", "F", "E", "S"],
["A", "D", "E", "E"]], "ABCESEEEFS"))