主题:力按钮(LeetCode ) ) ) )。
链接: https://leet code-cn.com/problems/word-search-ii
给出m x n二维字符网格board和单词(字符串)列表words,找出二维网格和词典中同时出现的所有单词。
单词必须按字母顺序由相邻kddpj内的字符组成。 其中,“相邻”kddpj是水平相邻或垂直相邻的kddpj。 同一kddpj中的字符不允许在一个单词中重复使用。
示例 1:
输入: board=[[“o、[a]、[a]、[n]、[e]、[t]、[e]、[I]、[h]、[k]、[r]]
输出: " eat "、" oath " "
示例 2:
输入: board=[[“a”、“b”]、[“c”、“d”]、words=[“abcb”]
输出: []
提示:
m==board.length
n==board[i].length
1=m,n=12
board[i][j]是小写字母
1=words.length=3 * 104
1=words[i].length=10
words[i]由小写英文字母构成
words中的所有字符串都互不相同
解法树回溯算法dfs
pythonclasssolution 3360定义窗口(self, board: List[List[str]],words 3360 list [ str ]-list [ str ] : ifboardisnoneorwordsisnoneorlen [ board ]1andlen trie turn forwordinwords 3360 t=triesforwinword : ifwnotint : t [ w ]={ } t=t [ w ] t [ ' end ' ]=1打印(三层) roww s 65: c=board [ I ] [ j ] ifcnotintries 3360 return trie=tries [ c ] if ' end ' intrieandtrie [ ' end ' ]=13360 RES . -1] ] : tmp _ x=Xi tmp_y=yj iii=' # ' 3360 DFS (tmp _ x,tmp _ y,trie, s c ) board[I][j]=cforIinrange ) :forjinrange ) cols ) 3360空间复杂性: o(1) https://leet code-cn.com/problems/worand