[リートコード] 0212. ワードサーチ II

Python、C++、JavaScript、SQL、TypeScript の多様な LeetCode ソリューションを探索してください。面接の準備、学習、複数のプログラミング言語でのコードの練習に最適です。 Github リポジトリ リンク

与えられた xn ボード 文字と文字列のリスト 言葉、 戻る ボード上のすべての単語.

各単語は連続して隣接するセルの文字から構成され、 隣接セル 水平または垂直に隣接しています。同じ文字セルを単語内で複数回使用することはできません。

 

例 1:

入力: ボード = [["o","a","a","n"],["e","t","a","e"],["i","h","k ","r"],["i","f","l","v"]], 単語 = ["誓い","エンドウ豆","食べる","雨"]
出力: ["食べる"、"誓い"]

例 2:

入力: ボード = [["a","b"],["c","d"]]、単語 = ["abcb"]
出力: []

 

制約:

  • m == ボードの長さ
  • n == ボード[i].長さ
  • 1 <= m、n <= 12
  • ボード[i][j] は英語の小文字です。
  • 1 <= 単語数.長さ <= 3 * 10 4
  • 1 <= 単語[i].長さ <= 10
  • 言葉[i] 小文字の英語の文字で構成されています。
  • のすべての文字列 言葉 ユニークです。

パイソン

				
					# 時間計算量: O(M(4*3^(L-1))) # 空間計算量: O(n) from entering import List class ソリューション: def findWords(self, board: List[List[str]], words : List[str]) -> List[str]: WORDKEY = "$" trie = {} for word in words: node = trie for letter in word: node = node.setdefault(letter, {}) node[WORDKEY] = word rowNum = len(board) colNum = len(board[0]) matchedWords = [] def backtracking(row, col, parent): letter = board[row][col] currNode = parent[letter] wordMatch = currNode. wordMatchの場合、pop(WORDKEY, False)、matchedWords.append(wordMatch)、board[row][col] = "#"、rowOffset、colOffsetが[(-1, 0)、(0, 1)、(1, 0)の場合, (0, -1)]: newRow、newCol = row + rowOffset、col + colOffset if ( newRow < 0 または newRow >= rowNum または newCol < 0 または newCol >= colNum ): そうでない場合は続行 board[newRow][newCol ] in currNode: バックトラックを続行(newRow, newCol, currNode) board[row][col] = letter if not currNode: parent.pop(letter) for row in range(rowNum): for col in range(colNum): if board [row][col] in trie: backtracking(row, col, trie) return matchedWords # 時間計算量: O(n*3^l) # 空間計算量: O(m) クラス TrieNode: def __init__(self, char=" "): self.char = char self.children = {} self.isEnd = False クラス Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for c単語: c が node.children にない場合: node.children[c] = TrieNode() node = node.children.get(c) node.isEnd = True def startsWith(self, prefix): node = self.root for c がプレフィックス内にある場合: c が node.children 内にない場合: False を返す node = node.children[c] True を返す def removeChars(self, word): node = self.root childList = [] c が word 内にある場合: childList.append( [node, c]) node = node.children[c] for parent, childChar in reversed(childList): target = parent.children[childChar] if target.children: return del parent.children[childChar] class 解決策: def findWords (self, board: List[List[str]], words: List[str]) -> List[str]: def dfs(trieForWords: Trie, node: TrieNode, grid: List[List[str]], row: int、col: int、result: List[str]、word=""): if node.isEnd: result.append(word) node.isEnd = False trieForWords.removeChars(word) if 0 <= row < ROW かつ 0 <= col < COL: char = grid[row][col] child = node.children.get(char) 子が None でない場合: word += char grid[row][col] = None dR、dC の場合 [ (0, 1), (1, 0), (-1, 0), (0, -1)]: dfs(trieForWords, 子, グリッド, 行 + dR, 列 + dC, 結果, 単語) グリッド[行][col] = char ROW = len(board) COL = len(board[0]) trieForWords = Trie() result = [] for word in words: trieForWords.insert(word) for r in range(ROW): for範囲(COL)内のc: dfs(trieForWords, trieForWords.root, board, r, c, result) 結果を返す board = [["o", "a", "a", "n"], ["e" 、["t"、"a"、"e"]、["i"、"h"、"k"、"r"]、["i"、"f"、"l"、"v"]] words = ["oath", "pea", "eat", "rain"] print(Solution().findWords(board, words))
				
			
ja日本語