-
Notifications
You must be signed in to change notification settings - Fork 2
/
212.word-search-ii.java
119 lines (98 loc) · 3.37 KB
/
212.word-search-ii.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* @lc app=leetcode id=212 lang=java
*
* [212] Word Search II
*/
// @lc code=start
class Solution {
class TrieNode {
public boolean isEndOfWord;
public TrieNode[] children;
public char val;
public TrieNode() {
isEndOfWord = false;
children = new TrieNode[26];
}
}
class Trie {
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode wordStart = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (wordStart.children[c - 'a'] == null) {
wordStart.children[c - 'a'] = new TrieNode();
wordStart.children[c - 'a'].val = c;
}
wordStart = wordStart.children[c - 'a'];
}
wordStart.isEndOfWord = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode wordStart = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (wordStart.children[c - 'a'] == null) {
return false;
}
wordStart = wordStart.children[c - 'a'];
}
return wordStart.isEndOfWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode wordStart = root;
for (int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
if (wordStart.children[c - 'a'] == null) {
return false;
}
wordStart = wordStart.children[c - 'a'];
}
return true;
}
}
public List<String> findWords(char[][] board, String[] words) {
Trie trie = new Trie();
for (String word : words) {
trie.insert(word);
}
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
Set<String> result = new HashSet<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dfsHelper(board, visited, "", i, j, trie, result);
}
}
return new ArrayList<>(result);
}
public void dfsHelper(char[][] board, boolean[][] visited, String word, int i, int j, Trie trie, Set<String> result) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || visited[i][j]) {
return;
}
word += board[i][j];
if (!trie.startsWith(word)) {
return;
}
if (trie.search(word)) {
result.add(word);
}
//dfs
visited[i][j] = true;
dfsHelper(board, visited, word, i + 1, j, trie, result);
dfsHelper(board, visited, word, i - 1, j, trie, result);
dfsHelper(board, visited, word, i, j + 1, trie, result);
dfsHelper(board, visited, word, i, j - 1, trie, result);
visited[i][j] = false;
}
}
//Time Complexity:O(m*n*4^l)
// @lc code=end