元宝 LeetCode 212. Word Search II Java Implement

发布时间:2026/10/10 18:22:57
元宝    LeetCode 212. Word Search II Java Implement Approach: Trie (Prefix Tree) Backtracking DFSBuild a Trie from“words”, then DFS from every cell on the board. Three pruning tricks make it fast enough:Bail out when the prefix dies — if“children[ch]” is null, stop immediatelyNull out the word once found — set“node.word null” so the same word isn’t collected twiceDetach exhausted subtrees — once every word under a node has been found, cut the parent’s pointer to itclass Solution {static class TrieNode {TrieNode[] children new TrieNode[26];String word; // non-null: a complete word ends here}private final TrieNode root new TrieNode(); private final ListString res new ArrayList(); private static final int[][] DIRS {{1,0},{-1,0},{0,1},{0,-1}}; private char[][] board; private int m, n; public ListString findWords(char[][] board, String[] words) { this.board board; this.m board.length; this.n board[0].length; for (String w : words) insert(w); for (int i 0; i m; i) for (int j 0; j n; j) dfs(i, j, root, new boolean[m][n]); return res; } private void insert(String word) { TrieNode cur root; for (char c : word.toCharArray()) { int k c - a; if (cur.children[k] null) cur.children[k] new TrieNode(); cur cur.children[k]; } cur.word word; } private void dfs(int r, int c, TrieNode parent, boolean[][] visited) { if (r 0 || r m || c 0 || c n || visited[r][c]) return; char ch board[r][c]; TrieNode node parent.children[ch - a]; if (node null) return; // prune 1: dead prefix if (node.word ! null) { res.add(node.word); node.word null; // prune 2: dedupe } if (isEmpty(node)) { // prune 3: subtree drained parent.children[ch - a] null; return; } visited[r][c] true; for (int[] d : DIRS) dfs(r d[0], c d[1], node, visited); visited[r][c] false; // backtrack } private boolean isEmpty(TrieNode node) { for (TrieNode child : node.children) if (child ! null) return false; return node.word null; }}ComplexityTime:“O(Σ|word|)” to build the Trie; search is“O(m·n·4·3^(L-1))” worst case where“L” is the longest word length, but pruning keeps real runs far below thatSpace:“O(Σ|word|)” for the Trie, plus“O(m·n)” for the recursion stack and visited arrayTwo optional micro-optimizationsDrop the visited array: temporarily flip a visited cell to“‘#’” and restore it on backtrack, saving an extra arrayAvoid the“O(26)” scan in“isEmpty”: give“TrieNode” an“int count” of remaining words in its subtree, decrement on collection, and detach the pointer when it hits 0One pitfall worth noting: don’t collect into a“Set” and skip prune 2 — on heavily overlapping prefixes like““aaa”” that gets noticeably slower.

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询