在一个二维矩阵中,每个元素都是一个字母,要判断目标字符串能否由该矩阵中的元素连接而成。所谓连接就是从矩阵中的某一个元素开始,向前后左右不断前进,但不允许再次经过走过的元素。
注意点:
例子:
输入:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED"
输出: True
采用深度优先遍历的方法,以每一个元素为起点进行查找。在此之前,可以做一个简单的前置判断,如果目标字符串中的某一个字母的数目比矩阵中所有该字母的数目还多,那么肯定是找不到目标字符串的。在进行深度遍历的时候,如果所有当前的遍历的位置越界或者与预期的值不等则返回,如果值相等,那么暂时把当前的值用特殊字符代替,防止继续遍历的时候又访问到访问过的点。
from collections import defaultdict
class Solution(object):
def exist(self, board, word):
""" :type board: List[List[str]] :type word: str :rtype: bool """
if self._hasEnoughCharacters(board, word):
m = len(board)
n = len(board[0])
for i in range(m):
for j in range(n):
if self._exist(board, i, j, m, n, word):
return True
return False
else:
return False
def _exist(self, board, i, j, m, n, word):
if len(word) == 0:
return True
if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[0]:
return False
temp = board[i][j]
board[i][j] = "."
next_target = word[1:]
next_result = self._exist(board, i - 1, j, m, n, next_target) \
or self._exist(board, i + 1, j, m, n, next_target) \
or self._exist(board, i, j - 1, m, n, next_target) \
or self._exist(board, i, j + 1, m, n, next_target)
board[i][j] = temp
return next_result
def _hasEnoughCharacters(self, board, word):
character_counts = defaultdict(int)
for ch in word:
character_counts[ch] += 1
return all(sum(map(lambda line: line.count(ch), board)) >= count for ch, count in character_counts.items())
if __name__ == "__main__":
assert Solution().exist([
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
], "ABCCED") == True
assert Solution().exist([
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
], "SEE") == True
assert Solution().exist([
['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']
], "ABCB") == False
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。