|
1 | 1 | class Solution { |
2 | | - class TrieNode { |
3 | | - private TrieNode[] children; |
4 | | - private String word; |
5 | | - |
6 | | - public TrieNode() { |
7 | | - children = new TrieNode[26]; |
8 | | - word = null; |
9 | | - } |
10 | | - } |
11 | | - |
12 | | - private TrieNode buildTrie(String[] words) { |
13 | | - TrieNode root = new TrieNode(); |
14 | | - |
15 | | - for (String word : words) { |
16 | | - TrieNode runner = root; |
17 | | - |
18 | | - for (char c : word.toCharArray()) { |
19 | | - if (runner.children[c - 'a'] == null) { |
20 | | - runner.children[c - 'a'] = new TrieNode(); |
21 | | - } |
22 | | - |
23 | | - runner = runner.children[c - 'a']; |
24 | | - } |
25 | | - |
26 | | - runner.word = word; |
27 | | - } |
28 | | - |
29 | | - return root; |
30 | | - } |
31 | | - |
32 | 2 | public List<String> findWords(char[][] board, String[] words) { |
33 | | - TrieNode root = buildTrie(words); |
34 | 3 | List<String> result = new ArrayList<>(); |
| 4 | + TrieNode root = buildTrie(words); |
35 | 5 |
|
36 | | - for (int i = 0; i < board.length; i++) { |
37 | | - for (int j = 0; j < board[i].length; j++) { |
38 | | - helper(board, i, j, root, result); |
| 6 | + for (int row = 0; row < board.length; row++) { |
| 7 | + for (int col = 0; col < board[row].length; col++) { |
| 8 | + dfs(board, row, col, root, result); |
39 | 9 | } |
40 | 10 | } |
41 | 11 |
|
42 | 12 | return result; |
43 | 13 | } |
44 | 14 |
|
45 | | - private void helper(char[][] board, int row, int col, TrieNode root, List<String> result) { |
| 15 | + private void dfs(char[][] board, int row, int col, TrieNode root, List<String> result) { |
46 | 16 | if (root.word != null) { |
47 | 17 | result.add(root.word); |
48 | 18 | root.word = null; |
49 | | - return; |
50 | 19 | } |
51 | 20 |
|
52 | 21 | if (row < 0 || col < 0 || row >= board.length || col >= board[row].length || board[row][col] == '.' |
53 | | - || root.children[board[row][col] - 'a'] == null) { |
| 22 | + || root.letters[board[row][col] - 'a'] == null) { |
54 | 23 | return; |
55 | 24 | } |
56 | 25 |
|
57 | 26 | char c = board[row][col]; |
58 | 27 | board[row][col] = '.'; |
59 | 28 |
|
60 | | - helper(board, row + 1, col, root.children[c - 'a'], result); |
61 | | - helper(board, row - 1, col, root.children[c - 'a'], result); |
62 | | - helper(board, row, col + 1, root.children[c - 'a'], result); |
63 | | - helper(board, row, col - 1, root.children[c - 'a'], result); |
| 29 | + dfs(board, row + 1, col, root.letters[c - 'a'], result); |
| 30 | + dfs(board, row - 1, col, root.letters[c - 'a'], result); |
| 31 | + dfs(board, row, col + 1, root.letters[c - 'a'], result); |
| 32 | + dfs(board, row, col - 1, root.letters[c - 'a'], result); |
64 | 33 |
|
65 | 34 | board[row][col] = c; |
66 | 35 | } |
| 36 | + |
| 37 | + private TrieNode buildTrie(String[] words) { |
| 38 | + TrieNode root = new TrieNode(); |
| 39 | + |
| 40 | + for (String w : words) { |
| 41 | + TrieNode runner = root; |
| 42 | + |
| 43 | + for (char c : w.toCharArray()) { |
| 44 | + if (runner.letters[c - 'a'] == null) { |
| 45 | + runner.letters[c - 'a'] = new TrieNode(); |
| 46 | + } |
| 47 | + |
| 48 | + runner = runner.letters[c - 'a']; |
| 49 | + } |
| 50 | + |
| 51 | + runner.word = w; |
| 52 | + } |
| 53 | + |
| 54 | + return root; |
| 55 | + } |
| 56 | + |
| 57 | + private class TrieNode { |
| 58 | + private TrieNode[] letters; |
| 59 | + private String word; |
| 60 | + |
| 61 | + public TrieNode() { |
| 62 | + letters = new TrieNode[26]; |
| 63 | + word = null; |
| 64 | + } |
| 65 | + } |
67 | 66 | } |
0 commit comments