Thinking process
class TrieNode {
public TrieNode[] children;
public boolean isWord;
public TrieNode(){
children = new TrieNode[26];
for (int i = 0; i < 26; i++) {
children[i] = null;
}
isWord = false;
}
}
class Trie {
public TrieNode root;
public Trie(){
root = new TrieNode();
}
public void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
if (node.children[c - 'a'] == null) {
TrieNode child = new TrieNode();
node.children[c - 'a'] = child;
}
node = node.children[c - 'a'];
}
node.isWord = true;
}
}
public class Solution {
/**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
public List<String> findWords(char[][] board, String[] words) {
//init
List<String> res = new ArrayList<>();
Trie trie = new Trie();
//convert words to trie
for (String word : words) {
trie.insert(word);
}
StringBuilder sb = new StringBuilder();
//scan board
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
boolean[][] visited = new boolean[board.length][board[0].length];
search(board, trie.root, visited, res, sb, i, j);
}
}
return res;
}
private void search(char[][] board, TrieNode node, boolean[][] visited, List<String> res, StringBuilder sb, int i, int j) {
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || node == null || visited[i][j]) return;
visited[i][j] = true;
sb.append(board[i][j]);
TrieNode next = node.children[board[i][j] - 'a'];
if (next != null && next.isWord) {
if (!res.contains(sb.toString())) {
res.add(sb.toString());
}
}
search(board, next, visited, res, sb, i - 1, j);//up
search(board, next, visited, res, sb, i + 1, j);//down
search(board, next, visited, res, sb, i, j - 1);//left
search(board, next, visited, res, sb, i, j + 1);//right
visited[i][j] = false;
sb.deleteCharAt(sb.length() - 1);
}
}