-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord Search.py
65 lines (48 loc) · 2.24 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
55
56
57
58
59
60
61
62
63
64
65
# Link: https://leetcode.com/problems/word-search/submissions/
class Solution(object):
def traverseMatrix(self, matrix, xPos, yPos, word, wordPos):
# If wordPos equals length of word, then the word has been found inside the matrix
# NOTE: This has to be here before next if-condition is executed.
if wordPos == len(word):
return True
# Stop function from going out-of-bounds
if xPos < 0 or xPos >= len(matrix) or yPos < 0 or yPos >= len(matrix[0]):
return False
# Get cell
currCell = matrix[xPos][yPos]
# If cell's value doesn't match with current letter, then exit function
if currCell != word[wordPos]:
return False
# Update cell's value
matrix[xPos][yPos] = ""
# Check all directions
right = self.traverseMatrix(matrix, xPos, yPos + 1, word, wordPos + 1) # right
down = self.traverseMatrix(matrix, xPos + 1, yPos, word, wordPos + 1) # down
left = self.traverseMatrix(matrix, xPos, yPos - 1, word, wordPos + 1) # left
up = self.traverseMatrix(matrix, xPos - 1, yPos, word, wordPos + 1) # up
# Change cell's value to its original value
matrix[xPos][yPos] = currCell
# If either variables are true, return true
return right or down or left or up
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
# If board is a NoneType, exit function
if board is None:
return
# Get board's dimensions
m = len(board)
n = len(board[0])
# Iterate board
for xPos in range(m):
for yPos in range(n):
# If cell's value equals to first letter of the word, then start start search from here
if board[xPos][yPos] == word[0]:
# Traverse matrix from current point
if self.traverseMatrix(board, xPos, yPos, word, 0):
return True
# Return false by default
return False