forked from alqamahjsr/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path79_Word_Search.py
More file actions
44 lines (36 loc) · 1.63 KB
/
79_Word_Search.py
File metadata and controls
44 lines (36 loc) · 1.63 KB
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
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
if not board:
return False
for row in range(len(board)):
for col in range(len(board[0])):
result = self.searchWord(board, word, row, col)
if result:
return True
return False
# check whether can find word, start at (i,j) position
def searchWord(self, origialBoard, originalWord, row, col):
if len(originalWord) == 0: # all the characters are checked
return True
if row < 0 or row >= len(origialBoard) or col < 0 or col >= len(origialBoard[0]) or origialBoard[row][col] != originalWord[0]:
return False
choice = origialBoard[row][col]
print("choice: " + choice + ", row: " + str(row) + ", col: " + str(col))
origialBoard[row][col] = "#" # first character is found, check the remaining part. And this choice is to avoid to visit the same node agian
result = self.searchWord(origialBoard, originalWord[1:], row + 1, col) or self.searchWord(origialBoard, originalWord[1:], row - 1, col) or self.searchWord(origialBoard, originalWord[1:], row, col + 1)or self.searchWord(origialBoard, originalWord[1:], row, col - 1)
origialBoard[row][col] = choice # repairing the origialBoard if the previous recursion call stack returns false.
return result
sol = Solution()
board = [
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED"
output = sol.exist(board, word)
print('Result: ', output)