-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path212_Word_Search_II.py
108 lines (94 loc) · 3.12 KB
/
212_Word_Search_II.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
107
108
"""
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example:
Input:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Output: ["eat","oath"]
Note:
You may assume that all inputs are consist of lowercase letters a-z.
"""
class Solution:
def findWords(self, board, words):
"""
:type board: List[List[str]]
:type words: List[str]
:rtype: List[str]
"""
"""
第一代的变体,遍历每个word,把符合要求的word存下即可
to be continue:
still TLE, pls ref this https://leetcode.com/problems/word-search-ii/discuss/59864/Python-code-use-trie-and-dfs-380ms
"""
rs = []
words = list(set(words))
for word in words:
if self.wordSearch(board, word):
rs.append(word)
return rs
"""
查找board中是否存在word ,也就是lc79的源代码
"""
def wordSearch(self, board, word):
if len(word) == 0: return True
if len(board) == 0 or len(board[0]) == 0: return False
if not self._hasEnoughChar(board, word): return False
for i in range(len(board)):
for j in range(len(board[0])):
if self.dfs(i,j, board, word):
return True
"""
dfs搜索
"""
def dfs(self, i, j, board, word):
if len(word) == 0: return True
if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or board[i][j] != word[0]:
return False
visited = board[i][j]
board[i][j] = ' '
# rs =(self.dfs(i-1,j,board,word[1:]) or \
# self.dfs(i+1,j,board,word[1:]) or \
# self.dfs(i,j-1,board,word[1:]) or\
# self.dfs(i,j+1,board,word[1:]))
# board[i][j] = visited
# return rs
if self.dfs(i-1,j,board,word[1:]):
board[i][j] = visited
return True
if self.dfs(i+1,j,board,word[1:]):
board[i][j] = visited
return True
if self.dfs(i,j-1,board,word[1:]):
board[i][j] = visited
return True
if self.dfs(i,j+1,board,word[1:]):
board[i][j] = visited
return True
return False
"""
判断word里的元素的个数是否少于board里面的对应元素的个数,对提升速度有很大的影响
"""
def _hasEnoughChar(self, board, word):
from collections import Counter
c_w = Counter(word)
c_board = Counter([c for row in board for c in row])
for k,v in c_w.items():
if c_board[k] < v:
return False
return True
so = Solution()
board =\
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
words = ["oath","pea","eat","rain"]
print(so.findWords(board,words))