-
Notifications
You must be signed in to change notification settings - Fork 0
/
findWords.java
109 lines (86 loc) · 2.66 KB
/
findWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Leetcode 212: Word Search II
* https://leetcode.com/problems/word-search-ii/
* Martin.Xia
*/
public class Solution {
Set<String> result = new HashSet<String>();
public List<String> findWords(char[][] board, String[] words) {
//HashSet<String> result = new HashSet<String>();
Trie trie = new Trie();
for(String word: words){
trie.insert(word);
}
int m=board.length;
int n=board[0].length;
boolean[][] recordVisited = new boolean[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
wordSearch(board, recordVisited, "", i, j, trie);
}
}
return new ArrayList<String>(result);
}
public boolean wordSearch(char[][] board, boolean[][] recordVisited, String str, int i, int j, Trie trie){
int m=board.length;
int n=board[0].length;
if(i<0 || j<0||i>=m||j>=n){
return false;
}
if(recordVisited[i][j])
return false;
str = str + board[i][j];
if(!trie.startsWith(str))
return false;
if(trie.search(str)){
result.add(str);
}
recordVisited[i][j]=true;
boolean res = wordSearch(board, recordVisited, str, i-1, j, trie)
|| wordSearch(board, recordVisited, str, i+1, j, trie)
|| wordSearch(board, recordVisited, str, i, j+1, trie)
|| wordSearch(board, recordVisited, str, i, j-1, trie);
recordVisited[i][j] = false;
return res;
}
}
class TrieNode{
public TrieNode[] children = new TrieNode[26];
public String item = "";
}
//Trie
class Trie{
public TrieNode root = new TrieNode();
public void insert(String word){
TrieNode node = root;
for(char c: word.toCharArray()){
if(node.children[c-'a']==null){
node.children[c-'a']= new TrieNode();
}
node = node.children[c-'a'];
}
node.item = word;
}
public boolean search(String word){
TrieNode node = root;
for(char c: word.toCharArray()){
if(node.children[c-'a']==null)
return false;
node = node.children[c-'a'];
}
if(node.item.equals(word)){
return true;
}else{
return false;
}
}
public boolean startsWith(String prefix){
TrieNode node = root;
for(char c: prefix.toCharArray()){
if(node.children[c-'a']==null)
return false;
node = node.children[c-'a'];
}
return true;
}
}