|
| 1 | +# Word Search II (Hard) |
| 2 | +# Link - https://leetcode.com/problems/word-search-ii/ |
| 3 | + |
| 4 | +# Given an m x n board of characters and a list of strings words, return all words on the board. |
| 5 | + |
| 6 | +# 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. |
| 7 | + |
| 8 | +# Example 1: |
| 9 | +# Input: board = [["o", "a", "a", "n"], ["e", "t", "a", "e"], ["i", "h", "k", "r"], ["i", "f", "l", "v"]], words = ["oath", "pea", "eat", "rain"] |
| 10 | +# Output: ["eat", "oath"] |
| 11 | + |
| 12 | + |
| 13 | +class TrieNode: |
| 14 | + def __init__(self): |
| 15 | + self.children = {} |
| 16 | + self.is_word = False |
| 17 | + |
| 18 | + def add_word(self, word): |
| 19 | + curr = self |
| 20 | + for char in word: |
| 21 | + if char not in curr.children: |
| 22 | + curr.children[char] = TrieNode() |
| 23 | + curr = curr.children[char] |
| 24 | + curr.is_word = True |
| 25 | + |
| 26 | + def remove_word(self, word): |
| 27 | + curr = self |
| 28 | + for char in word: |
| 29 | + if char in curr.children: |
| 30 | + curr = curr.children[char] |
| 31 | + |
| 32 | + |
| 33 | +class Solution: |
| 34 | + def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: |
| 35 | + trie = TrieNode() |
| 36 | + for word in words: |
| 37 | + trie.add_word(word) |
| 38 | + |
| 39 | + ROWS, COLS = len(board), len(board[0]) |
| 40 | + res, visited = set(), set() |
| 41 | + |
| 42 | + def dfs(r, c, node, word): |
| 43 | + if ( |
| 44 | + r < 0 or c < 0 |
| 45 | + or r >= ROWS or c >= COLS |
| 46 | + or board[r][c] not in node.children |
| 47 | + or (r, c) in visited |
| 48 | + ): |
| 49 | + return |
| 50 | + |
| 51 | + visited.add((r, c)) |
| 52 | + node = node.children[board[r][c]] |
| 53 | + word += board[r][c] |
| 54 | + |
| 55 | + if node.is_word: |
| 56 | + trie.is_word = False |
| 57 | + res.add(word) |
| 58 | + |
| 59 | + if not node.children: |
| 60 | + del node |
| 61 | + else: |
| 62 | + dfs(r + 1, c, node, word) |
| 63 | + dfs(r - 1, c, node, word) |
| 64 | + dfs(r, c + 1, node, word) |
| 65 | + dfs(r, c - 1, node, word) |
| 66 | + |
| 67 | + visited.remove((r, c)) |
| 68 | + |
| 69 | + for r in range(ROWS): |
| 70 | + for c in range(COLS): |
| 71 | + dfs(r, c, trie, "") |
| 72 | + |
| 73 | + return list(res) |
| 74 | + |
| 75 | + |
| 76 | +# Solution 2 |
| 77 | +# class TrieNode: |
| 78 | +# def __init__(self): |
| 79 | +# self.children = collections.defaultdict(TrieNode) |
| 80 | +# self.is_word = False |
| 81 | + |
| 82 | +# class Trie: |
| 83 | +# def __init__(self): |
| 84 | +# self.root = TrieNode() |
| 85 | + |
| 86 | +# def insert(self, word): |
| 87 | +# node = self.root |
| 88 | +# for c in word: |
| 89 | +# node = node.children[c] |
| 90 | +# node.is_word = True |
| 91 | + |
| 92 | +# class Solution: |
| 93 | +# def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: |
| 94 | +# trie = Trie() |
| 95 | +# for w in words: |
| 96 | +# trie.insert(w) |
| 97 | + |
| 98 | +# m, n = len(board), len(board[0]) |
| 99 | +# res = [] |
| 100 | +# for i in range(m): |
| 101 | +# for j in range(n): |
| 102 | +# paths = [] |
| 103 | +# path = "" |
| 104 | +# self.dfs(board, i, j, trie.root, path, paths) |
| 105 | +# res += paths |
| 106 | +# return res |
| 107 | + |
| 108 | +# def dfs(self, board, row, col, node, path, paths): |
| 109 | +# # match words starting from node of the trie, board starting from (row, col) |
| 110 | +# if node.is_word: |
| 111 | +# paths.append(path) |
| 112 | +# # set to False so not to repeat the same word |
| 113 | +# node.is_word = False |
| 114 | + |
| 115 | +# m, n = len(board), len(board[0]) |
| 116 | +# # similar to two pointers: here is to check if pointer for board reaches its end, or not a match |
| 117 | +# if row < 0 or row >=m or col < 0 or col >= n or board[row][col] not in node.children: |
| 118 | +# return |
| 119 | + |
| 120 | +# # This is similar to 2 pointers: now a match is found, move the pointer for trie and pointer for board |
| 121 | +# tmp = board[row][col] |
| 122 | +# board[row][col] = '#' |
| 123 | +# dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] |
| 124 | +# for d in dirs: |
| 125 | +# r, c = row + d[0], col + d[1] |
| 126 | +# self.dfs(board, r, c, node.children[tmp], path + tmp, paths) |
| 127 | +# board[row][col] = tmp |
| 128 | + |
| 129 | +# # pruning: if it is a leaf and a matched word is found for it already, pop it to decrease trie size |
| 130 | +# if len(node.children[tmp].children) == 0: |
| 131 | +# del node.children[tmp] |
0 commit comments