|
1 | 1 | package com.diguage.algo.leetcode;
|
2 | 2 |
|
3 |
| -import java.util.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
4 | 7 |
|
5 | 8 | public class _0212_WordSearchIi {
|
6 | 9 | // tag::answer[]
|
| 10 | + |
7 | 11 | /**
|
8 | 12 | * 通过 43 / 65 个测试用例。超时!
|
9 | 13 | *
|
10 | 14 | * @author D瓜哥 · https://www.diguage.com
|
11 | 15 | * @since 2025-06-12 22:53:03
|
12 | 16 | */
|
| 17 | + int[][] options = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; |
| 18 | + |
13 | 19 | public List<String> findWords(char[][] board, String[] words) {
|
14 |
| - Arrays.sort(words, String::compareTo); |
15 |
| - Map<Character, List<String>> charToWordMap = new HashMap<>(); |
16 |
| - for (int i = 0; i < words.length; i++) { |
17 |
| - String word = words[i]; |
18 |
| - char c = word.charAt(0); |
19 |
| - charToWordMap.computeIfAbsent(c, k -> new ArrayList<>()).add(word); |
| 20 | + Trie trie = new Trie(); |
| 21 | + for (String word : words) { |
| 22 | + trie.insert(word); |
20 | 23 | }
|
21 |
| - Map<Character, List<int[]>> charToIndexMap = new HashMap<>(); |
| 24 | + Set<String> result = new HashSet<>(); |
22 | 25 | for (int r = 0; r < board.length; r++) {
|
23 | 26 | for (int c = 0; c < board[r].length; c++) {
|
24 |
| - char ac = board[r][c]; |
25 |
| - if (charToWordMap.containsKey(ac)) { |
26 |
| - charToIndexMap.computeIfAbsent(ac, k -> new ArrayList<>()) |
27 |
| - .add(new int[]{r, c}); |
28 |
| - } |
| 27 | + dfs(board, r, c, trie, result); |
29 | 28 | }
|
30 | 29 | }
|
31 |
| - Set<String> result = new HashSet<>(); |
32 |
| - charToIndexMap.forEach((k, v) -> { |
33 |
| - for (int[] idx : v) { |
34 |
| - List<String> aWords = charToWordMap.get(k); |
35 |
| - for (String aw : aWords) { |
36 |
| - dfs(board, idx[0], idx[1], aw, 0, new HashSet<>(), result); |
37 |
| - } |
38 |
| - } |
39 |
| - }); |
40 | 30 | return new ArrayList<>(result);
|
41 | 31 | }
|
42 | 32 |
|
43 | 33 | private void dfs(char[][] board, int row, int col,
|
44 |
| - String word, int idx, Set<List<Integer>> path, |
45 |
| - Set<String> result) { |
46 |
| - List<Integer> index = Arrays.asList(row, col); |
47 |
| - if (path.contains(index)) { |
48 |
| - return; |
49 |
| - } |
50 |
| - if (result.contains(word)) { |
51 |
| - return; |
52 |
| - } |
53 |
| - if (idx == word.length()) { |
54 |
| - result.add(word); |
55 |
| - return; |
56 |
| - } |
| 34 | + Trie trie, Set<String> result) { |
57 | 35 | if (row < 0 || row >= board.length
|
58 | 36 | || col < 0 || col >= board[0].length) {
|
59 | 37 | return;
|
60 | 38 | }
|
61 |
| - if (board[row][col] != word.charAt(idx)) { |
| 39 | + char letter = board[row][col]; |
| 40 | + if (!trie.containsLetter(letter)) { |
62 | 41 | return;
|
63 | 42 | }
|
64 |
| - path.add(index); |
65 |
| - int[][] options = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; |
| 43 | + trie = trie.search(letter); |
| 44 | + if (trie.word != null) { |
| 45 | + result.add(trie.word); |
| 46 | + } |
| 47 | + board[row][col] = '#'; |
66 | 48 | for (int[] option : options) {
|
67 | 49 | int r = row + option[0];
|
68 | 50 | int c = col + option[1];
|
69 |
| - dfs(board, r, c, word, idx + 1, path, result); |
| 51 | + dfs(board, r, c, trie, result); |
| 52 | + } |
| 53 | + board[row][col] = letter; |
| 54 | + } |
| 55 | + |
| 56 | + public static class Trie { |
| 57 | + Trie[] children = new Trie[26]; |
| 58 | + String word; |
| 59 | + |
| 60 | + public void insert(String word) { |
| 61 | + Trie curr = this; |
| 62 | + for (int i = 0; i < word.length(); i++) { |
| 63 | + char c = word.charAt(i); |
| 64 | + if (curr.children[c - 'a'] == null) { |
| 65 | + curr.children[c - 'a'] = new Trie(); |
| 66 | + } |
| 67 | + curr = curr.children[c - 'a']; |
| 68 | + } |
| 69 | + curr.word = word; |
| 70 | + } |
| 71 | + |
| 72 | + public boolean containsLetter(char letter) { |
| 73 | + int idx = letter - 'a'; |
| 74 | + if (idx < 0 || idx >= children.length) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + return children[idx] != null; |
| 78 | + } |
| 79 | + |
| 80 | + public Trie search(char letter) { |
| 81 | + return children[letter - 'a']; |
70 | 82 | }
|
71 |
| - path.remove(index); |
72 | 83 | }
|
73 | 84 | // end::answer[]
|
| 85 | + |
| 86 | + public static void main(String[] args) { |
| 87 | + new _0212_WordSearchIi().findWords(new char[][]{ |
| 88 | + {'o', 'a', 'b', 'n'}, |
| 89 | + {'o', 't', 'a', 'e'}, |
| 90 | + {'a', 'h', 'k', 'r'}, |
| 91 | + {'a', 'f', 'l', 'v'}}, |
| 92 | + new String[]{"oa", "oaa"}); |
| 93 | + } |
74 | 94 | }
|
0 commit comments