212 Word Search II

Given an m x n board of characters and a list of strings words, return all words on the board.

Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Example 1:

Input: board = [
    ["o","a","a","n"],
    ["e","t","a","e"],
    ["i","h","k","r"],
    ["i","f","l","v"]
], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]

Example 2:

Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []

Constraints:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 12
  • board[i][j] is a lowercase English letter.
  • 1 <= words.length <= 3 * 104
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters.
  • All the strings of words are unique.
 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
class Solution:
    def findWords(
        self,
        board: List[List[str]],
        words: List[str]
    ) -> List[str]:
        def prune_shitty_trie(root, word):
            prune_candidate = (root, word[0])
            current = root

            i = 0
            while i < len(word):
                char = word[i]
                if current[char]["is_word"] or len(current[char]) > 2:
                    # can't prune anything above and including current[char]
                    if i + 1 >= len(word):
                        return
                    prune_candidate = (current[char], word[i + 1])

                current = current[char]
                i += 1

            if len(current) == 1:
                del prune_candidate[0][prune_candidate[1]]

        def dfs(r, c, node, accumulated):
            if r < 0 or r >= m or c < 0 or c >= n:
                return

            if board[r][c] not in node:
                return

            char = board[r][c]
            board[r][c] = "#"
            accumulated += char
            node = node[char]

            if node["is_word"]:
                res.append(accumulated)
                node["is_word"] = False
                prune_shitty_trie(root, accumulated)

            # 4 directions
            for (dr, dc) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                dfs(r + dr, c + dc, node, accumulated)

            board[r][c] = char

        m, n = len(board), len(board[0])
        
        # Build trie
        root = {"is_word": False}
        for word in words:
            current = root
            for char in word:
                current = current.setdefault(char, {"is_word": False})
            current["is_word"] = True

        res = []
        for r in range(m):
            for c in range(n):
                dfs(r, c, root, '')

        return res