|
30 | 30 | If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.*/
|
31 | 31 | public class _212 {
|
32 | 32 |
|
33 |
| - public List<String> findWords(char[][] board, String[] words) { |
34 |
| - TrieNode root = buildTrie(words); |
35 |
| - List<String> result = new ArrayList(); |
36 |
| - for (int i = 0; i < board.length; i++) { |
37 |
| - for (int j = 0; j < board[0].length; j++) { |
38 |
| - dfs(root, board, i, j, result); |
| 33 | + public static class Solution1 { |
| 34 | + public List<String> findWords(char[][] board, String[] words) { |
| 35 | + TrieNode root = buildTrie(words); |
| 36 | + List<String> result = new ArrayList(); |
| 37 | + for (int i = 0; i < board.length; i++) { |
| 38 | + for (int j = 0; j < board[0].length; j++) { |
| 39 | + dfs(root, board, i, j, result); |
| 40 | + } |
39 | 41 | }
|
| 42 | + return result; |
40 | 43 | }
|
41 |
| - return result; |
42 |
| - } |
43 | 44 |
|
44 |
| - private void dfs(TrieNode root, char[][] board, int i, int j, List<String> result) { |
45 |
| - char c = board[i][j]; |
| 45 | + private void dfs(TrieNode root, char[][] board, int i, int j, List<String> result) { |
| 46 | + char c = board[i][j]; |
46 | 47 |
|
47 |
| - if (c == '#' || root.children[c - 'a'] == null) { |
48 |
| - return; |
49 |
| - } |
| 48 | + if (c == '#' || root.children[c - 'a'] == null) { |
| 49 | + return; |
| 50 | + } |
50 | 51 |
|
51 |
| - if (root.children[c - 'a'].word != null) { |
52 |
| - result.add(root.children[c - 'a'].word); |
53 |
| - root.children[c - 'a'].word = null;//de-duplicate |
54 |
| - } |
55 |
| - board[i][j] = '#';//mark it as visited to avoid cycles |
56 |
| - if (i > 0) { |
57 |
| - dfs(root.children[c - 'a'], board, i - 1, j, result); |
58 |
| - } |
59 |
| - if (j > 0) { |
60 |
| - dfs(root.children[c - 'a'], board, i, j - 1, result); |
61 |
| - } |
62 |
| - if (i + 1 < board.length) { |
63 |
| - dfs(root.children[c - 'a'], board, i + 1, j, result); |
64 |
| - } |
65 |
| - if (j + 1 < board[0].length) { |
66 |
| - dfs(root.children[c - 'a'], board, i, j + 1, result); |
67 |
| - } |
| 52 | + if (root.children[c - 'a'].word != null) { |
| 53 | + result.add(root.children[c - 'a'].word); |
| 54 | + root.children[c - 'a'].word = null;//de-duplicate |
| 55 | + } |
| 56 | + board[i][j] = '#';//mark it as visited to avoid cycles |
| 57 | + if (i > 0) { |
| 58 | + dfs(root.children[c - 'a'], board, i - 1, j, result); |
| 59 | + } |
| 60 | + if (j > 0) { |
| 61 | + dfs(root.children[c - 'a'], board, i, j - 1, result); |
| 62 | + } |
| 63 | + if (i + 1 < board.length) { |
| 64 | + dfs(root.children[c - 'a'], board, i + 1, j, result); |
| 65 | + } |
| 66 | + if (j + 1 < board[0].length) { |
| 67 | + dfs(root.children[c - 'a'], board, i, j + 1, result); |
| 68 | + } |
68 | 69 |
|
69 |
| - board[i][j] = c; |
70 |
| - } |
| 70 | + board[i][j] = c; |
| 71 | + } |
71 | 72 |
|
72 |
| - private TrieNode root; |
| 73 | + private TrieNode root; |
73 | 74 |
|
74 |
| - class TrieNode { |
75 |
| - String word; |
76 |
| - TrieNode[] children = new TrieNode[26]; |
77 |
| - } |
| 75 | + class TrieNode { |
| 76 | + String word; |
| 77 | + TrieNode[] children = new TrieNode[26]; |
| 78 | + } |
78 | 79 |
|
79 |
| - private TrieNode buildTrie(String[] words) { |
80 |
| - TrieNode root = new TrieNode(); |
81 |
| - for (String word : words) { |
82 |
| - char[] chars = word.toCharArray(); |
83 |
| - TrieNode temp = root; |
84 |
| - for (char c : chars) { |
85 |
| - if (temp.children[c - 'a'] == null) { |
86 |
| - temp.children[c - 'a'] = new TrieNode(); |
| 80 | + private TrieNode buildTrie(String[] words) { |
| 81 | + TrieNode root = new TrieNode(); |
| 82 | + for (String word : words) { |
| 83 | + char[] chars = word.toCharArray(); |
| 84 | + TrieNode temp = root; |
| 85 | + for (char c : chars) { |
| 86 | + if (temp.children[c - 'a'] == null) { |
| 87 | + temp.children[c - 'a'] = new TrieNode(); |
| 88 | + } |
| 89 | + temp = temp.children[c - 'a']; |
87 | 90 | }
|
88 |
| - temp = temp.children[c - 'a']; |
| 91 | + temp.word = word; |
89 | 92 | }
|
90 |
| - temp.word = word; |
| 93 | + return root; |
91 | 94 | }
|
92 |
| - return root; |
93 | 95 | }
|
94 |
| - |
95 | 96 | }
|
0 commit comments