212. Word Search II
2017-03-06 11:58
369 查看
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example,
Given words =
Return
Note:
You may assume that all inputs are consist of lowercase letters
Solution:
Using Dfs can generate all the possible words that the board could have, but it takes to much time and has unnecessary computation. for example if we have search "oaa" in the board above, we could stop search since no word in the words have prefix "oaa".
How do we decide if there is no word in the words array that have the prefix "xxxx" ? We build a trie.
Code:
public class Solution {
int[] dirX = {1,-1,0,0};
int[] dirY = {0,0,1,-1};
public List<String> findWords(char[][] board, String[] words) {
if(board.length == 0 || board[0].length == 0) return new ArrayList<String>();
TrieNode root = buildTrie(words);
HashSet<String> ret = new HashSet<>();
boolean[][] visited = new boolean[board.length][board[0].length];
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
dfs(board,visited, ret, i, j, root);
}
}
return new ArrayList<String>(ret);
}
public void dfs(char[][] board, boolean[][] visited, HashSet<String> ret, int i, int j, TrieNode t){
if(i < 0 || j < 0 || i >= board.length || j >= board[0].length
|| visited[i][j] || t.subNodes[board[i][j] - 'a'] == null){
return;
}
t = t.subNodes[board[i][j] - 'a'];
if(t.isEnd){
ret.add(t.s);
}
visited[i][j] = true;
for(int k = 0; k < 4; k++){
int x = i + dirX[k];
int y = j + dirY[k];
dfs(board,visited,ret,x,y,t);
}
visited[i][j] = false;
}
public TrieNode buildTrie(String[] words){
TrieNode root = new TrieNode();
for(String s : words){
TrieNode curt = root;
for(char c : s.toCharArray()){
int index = c - 'a';
if(curt.subNodes[index] == null){
curt.subNodes[index] = new TrieNode();
}
curt = curt.subNodes[index];
}
curt.isEnd = true;
curt.s = s;
}
return root;
}
class TrieNode{
TrieNode[] subNodes = new TrieNode[26];
String s;
boolean isEnd = false;
public TrieNode(){}
}
}
Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example,
Given words =
["oath","pea","eat","rain"]and board =
[ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ]
Return
["eat","oath"].
Note:
You may assume that all inputs are consist of lowercase letters
a-z.
Solution:
Using Dfs can generate all the possible words that the board could have, but it takes to much time and has unnecessary computation. for example if we have search "oaa" in the board above, we could stop search since no word in the words have prefix "oaa".
How do we decide if there is no word in the words array that have the prefix "xxxx" ? We build a trie.
Code:
public class Solution {
int[] dirX = {1,-1,0,0};
int[] dirY = {0,0,1,-1};
public List<String> findWords(char[][] board, String[] words) {
if(board.length == 0 || board[0].length == 0) return new ArrayList<String>();
TrieNode root = buildTrie(words);
HashSet<String> ret = new HashSet<>();
boolean[][] visited = new boolean[board.length][board[0].length];
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
dfs(board,visited, ret, i, j, root);
}
}
return new ArrayList<String>(ret);
}
public void dfs(char[][] board, boolean[][] visited, HashSet<String> ret, int i, int j, TrieNode t){
if(i < 0 || j < 0 || i >= board.length || j >= board[0].length
|| visited[i][j] || t.subNodes[board[i][j] - 'a'] == null){
return;
}
t = t.subNodes[board[i][j] - 'a'];
if(t.isEnd){
ret.add(t.s);
}
visited[i][j] = true;
for(int k = 0; k < 4; k++){
int x = i + dirX[k];
int y = j + dirY[k];
dfs(board,visited,ret,x,y,t);
}
visited[i][j] = false;
}
public TrieNode buildTrie(String[] words){
TrieNode root = new TrieNode();
for(String s : words){
TrieNode curt = root;
for(char c : s.toCharArray()){
int index = c - 'a';
if(curt.subNodes[index] == null){
curt.subNodes[index] = new TrieNode();
}
curt = curt.subNodes[index];
}
curt.isEnd = true;
curt.s = s;
}
return root;
}
class TrieNode{
TrieNode[] subNodes = new TrieNode[26];
String s;
boolean isEnd = false;
public TrieNode(){}
}
}
相关文章推荐
- 212. Word Search II[hard]
- Leetcode 212. Word Search II
- Hard-题目48:212. Word Search II
- Leetcode 212. Word Search II
- LeetCode 212. Word Search II(单词搜索)
- 212. Word Search II
- [LeetCode] 212. Word Search II 词语搜索 II
- leetcode 212. Word Search II
- 79. Word Search && 212. Word Search II
- 212. Word Search II
- LeetCode 212. Word Search II
- 212. Word Search II
- 212. Word Search II (DFS,trie)
- 212. Word Search II
- 212. Word Search II
- Leetcode 212. Word Search II
- 212. Word Search II
- 212. Word Search II
- 212. Word Search II(Trie)
- Leetcode 79. Word Search & 212. Word Search II