-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079_Word_Search.py
59 lines (43 loc) · 1.67 KB
/
0079_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
from typing import List
from unittest import TestCase, main
def exist(board: List[List[str]], word: str) -> bool:
m, n = len(board), len(board[0])
if not m or not n or not word:
return False
def search(row: int, col: int, index: int) -> bool:
if index >= len(word):
return True
if row < 0 or row >= m or col < 0 or col >= n:
return False
if board[row][col] != word[index]:
return False
index += 1
temp, board[row][col] = board[row][col], None
found_next_letter = search(row - 1, col, index) or \
search(row + 1, col, index) or \
search(row, col - 1, index) or \
search(row, col + 1, index)
board[row][col] = temp
return found_next_letter
for row in range(0, m):
for col in range(0, n):
if board[row][col] == word[0]:
if search(row, col, 0):
return True
return False
class Test(TestCase):
board = [
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
def test_given_case_1(self):
self.assertTrue(exist(self.board, "ABCCED"))
def test_given_case_2(self):
self.assertTrue(exist(self.board, "SEE"))
def test_given_case_3(self):
self.assertFalse(exist(self.board, "ABCB"))
def test_empty_word(self):
self.assertFalse(exist(self.board, ""))
if __name__ == "__main__":
main()