-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_word_search.py
106 lines (76 loc) · 2.18 KB
/
leetcode_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
# https://leetcode.com/problems/word-search/
#
# Given a matrix with letter and word
# Determine whether we can assemble a word moving only from
# aligned cells in matrix
#
def dfs(board, y, x, word, H, W):
if len(word) == 0:
return True
if y >= H or y < 0 or x >= W or x < 0:
return False
if word[0] != board[y][x]:
return False
prev = board[y][x]
board[y][x] = '*'
res = dfs(board, y + 1, x, word[1:], H, W) or \
dfs(board, y, x + 1, word[1:], H, W) or \
dfs(board, y, x - 1, word[1:], H, W) or \
dfs(board, y - 1, x, word[1:], H, W)
board[y][x] = prev
return res
def leetcode_word_search(board, word):
H = len(board)
if H < 1:
return False
W = len(board[0])
if H * W < len(word):
return False
d = {}
for l in word:
d[l] = d.get(l, 0) + 1
for y in xrange(H):
for x in xrange(W):
if board[y][x] in d:
d[board[y][x]] -= 1
for l in d:
if d[l] > 0:
return False
for y in xrange(H):
for x in xrange(W):
if board[y][x] == word[0]:
if dfs(board, y, x, word, H, W):
return True
return False
def tests():
board = [["a"]]
word = "a"
assert leetcode_word_search(board, word) == True
board = [["a","a"]]
word = "aa"
assert leetcode_word_search(board, word) == True
board = [["a","a"]]
word = "aaa"
assert leetcode_word_search(board, word) == False
board = [["a","b"],["c","d"]]
word = "abdc"
assert leetcode_word_search(board, word) == True
board = [["a","b"],["c","d"]]
word = "abcd"
assert leetcode_word_search(board, word) == False
board = [
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
]
assert leetcode_word_search(board, "ABCCED") == True
assert leetcode_word_search(board, "SEE") == True
assert leetcode_word_search(board, "ABCB") == False
board = [
["A","B","C","E"],
["S","F","E","S"],
["A","D","E","E"]
]
assert leetcode_word_search(board, "ABCESEEEFS") == True
tests()