Skip to content

Commit fe10936

Browse files
refactor 212
1 parent a21985d commit fe10936

File tree

1 file changed

+50
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+50
-49
lines changed

src/main/java/com/fishercoder/solutions/_212.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,66 +30,67 @@
3030
If you would like to learn how to implement a basic trie, please work on this problem: Implement Trie (Prefix Tree) first.*/
3131
public class _212 {
3232

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+
}
3941
}
42+
return result;
4043
}
41-
return result;
42-
}
4344

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];
4647

47-
if (c == '#' || root.children[c - 'a'] == null) {
48-
return;
49-
}
48+
if (c == '#' || root.children[c - 'a'] == null) {
49+
return;
50+
}
5051

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+
}
6869

69-
board[i][j] = c;
70-
}
70+
board[i][j] = c;
71+
}
7172

72-
private TrieNode root;
73+
private TrieNode root;
7374

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+
}
7879

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'];
8790
}
88-
temp = temp.children[c - 'a'];
91+
temp.word = word;
8992
}
90-
temp.word = word;
93+
return root;
9194
}
92-
return root;
9395
}
94-
9596
}

0 commit comments

Comments
 (0)