[NeetCode 150] Search for Word II

Given a 2-D grid of characters board and a list of strings words, return all words that are present in the grid.

For a word to be present it must be possible to form the word with a path in the board with horizontally or vertically neighboring cells. The same cell may not be used more than once in a word.

Example 1:

Input:
board = [
  ["a","b","c","d"],
  ["s","a","a","t"],
  ["a","c","k","e"],
  ["a","c","d","n"]
],
words = ["bat","cat","back","backend","stack"]

Output: ["cat","back","backend"]

Example 2:

Input:
board = [
  ["x","o"],
  ["x","o"]
],
words = ["xoxo"]

Output: []

Constraints:

1 <= board.length, board[i].length <= 10
board[i] consists only of lowercase English letter.
1 <= words.length <= 100
1 <= words[i].length <= 10

words[i] consists only of lowercase English letters.

All strings within words are distinct.

Solution

Compared with basic Search for Word, this problem involves multiple queries. For one query, we have to go through the whole matrix and apply DFS, which consume O ( n × m × Len ( w o r d ) ) O(n\times m\times \text{Len}(word)) O(n×m×Len(word)) time complexity. If we simply repeat this process on every query, the time complexity is O ( n × m × ∑ w ∈ w o r d s Len ( w ) ) O(n\times m\times \sum_{w\in words}\text{Len}(w)) O(n×m×∑w∈wordsLen(w)), which is too time-consuming.

In fact, we can go through the whole matrix and apply DFS at each position only once, because in just one going through, we can meet all possible words that can be produced by the matrix. There is no need to repeat it.

One possible solution is to record all strings while we go through the matrix, using a set or hash algorithm, but the number of all possible strings might be large and we only need few among them. The overall time and space complexity will be O ( n 2 m 2 ) O(n^2m^2) O(n2m2).

Actually, the DFS can be much more efficient if we search according to the given word list. To achieve this, we can build a Trie tree on all the words we want to find in the matrix. Then, we simultaneously move on both the matrix and Trie tree. Under the guidance of Trie tree, we only search the position that can compose a prefix of target words. Although in the worst case, the time complexity will still be O ( n 2 m 2 ) O(n^2m^2) O(n2m2), but in general cases, the time complexity will be about O ( ∑ w ∈ w o r d s Len ( w ) ) O( \sum_{w\in words}\text{Len}(w)) O(∑w∈wordsLen(w)) and the space complexity will be constantly O ( n × m + ∑ w ∈ w o r d s Len ( w ) ) O(n\times m+ \sum_{w\in words}\text{Len}(w)) O(n×m+∑w∈wordsLen(w)).

Code

py 复制代码
class Trie:
    def __init__(self):
        self.is_word = [False]
        self.tree = [{}]
        self.id_cnt = 0
        self.word_cnt = [0]
    
    def insert(self, word):
        node = 0
        self.word_cnt[0] += 1
        for c in word:
            if c not in self.tree[node]:
                self.id_cnt += 1
                self.tree[node][c] = self.id_cnt
                self.is_word.append(False)
                self.tree.append({})
                self.word_cnt.append(0)
            node = self.tree[node][c]
            self.word_cnt[node] += 1
        self.is_word[node] = True

    def remove(self, word):
        node = 0
        self.word_cnt[0] -= 1
        for c in word:
            if c not in self.tree[node]:
                return
            node = self.tree[node][c]
            if self.word_cnt[node] <= 0:
                return
            self.word_cnt[node] -= 1
        self.is_word[node] = False



class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        trie = Trie()
        for word in words:
            trie.insert(word)
        ans = []
        vis = set()
        X = [ 1,-1, 0, 0]
        Y = [ 0, 0, 1,-1]
        def dfs(x, y, node, string):
            print(x, y, node, string)
            if trie.word_cnt[node] <= 0:
                return
            if trie.is_word[node]:
                ans.append(string)
                trie.remove(string)
            vis.add((x, y))
            for i in range(4):
                nxt_x = x + X[i]
                nxt_y = y + Y[i]
                if 0 <= nxt_x < len(board) and 0 <= nxt_y < len(board[0]) and (nxt_x, nxt_y) not in vis:
                    nxt_char = board[nxt_x][nxt_y]
                    if nxt_char in trie.tree[node]:
                        dfs(nxt_x, nxt_y, trie.tree[node][nxt_char], string+nxt_char)
            vis.remove((x,y))
            return False

        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] in trie.tree[0]:
                    dfs(i, j, trie.tree[0][board[i][j]], board[i][j])
        return ans
        
相关推荐
带多刺的玫瑰1 小时前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
爱敲代码的憨仔1 小时前
《线性代数的本质》
线性代数·算法·决策树
yigan_Eins1 小时前
【数论】莫比乌斯函数及其反演
c++·经验分享·算法
阿史大杯茶1 小时前
AtCoder Beginner Contest 381(ABCDEF 题)视频讲解
数据结构·c++·算法
დ旧言~2 小时前
【高阶数据结构】图论
算法·深度优先·广度优先·宽度优先·推荐算法
张彦峰ZYF2 小时前
投资策略规划最优决策分析
分布式·算法·金融
The_Ticker2 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
爪哇学长3 小时前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
Dola_Pan3 小时前
C语言:数组转换指针的时机
c语言·开发语言·算法
繁依Fanyi3 小时前
简易安卓句分器实现
java·服务器·开发语言·算法·eclipse