[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
        
相关推荐
边疆.22 分钟前
C++类和对象 (中)
c语言·开发语言·c++·算法
binqian35 分钟前
【K8S】kubernetes-dashboard.yaml
算法·贪心算法
Wils0nEdwards1 小时前
Leetcode 合并 K 个升序链表
算法·leetcode·链表
Tisfy1 小时前
LeetCode 3211.生成不含相邻零的二进制字符串:二进制枚举+位运算优化
算法·leetcode·二进制·题解·枚举·位运算
好看资源平台1 小时前
深入理解所有权与借用——借用与生命周期管理
开发语言·算法·rust
qiaoxinyu19892 小时前
线性代数(1)——线性方程组的几何意义
线性代数·算法·机器学习
爱吃饭团的饭桶2 小时前
【附源码】Python :哈密顿回路
开发语言·python·算法
TNTLWT2 小时前
算法:查找
算法
feilieren2 小时前
leetcode - 684. 冗余连接
java·开发语言·算法
余~185381628002 小时前
矩阵系统源码搭建,OEM贴牌技术
网络·人工智能·线性代数·算法·矩阵