442. Implement Trie (Prefix Tree)
class TrieNode {
public boolean isWord;
public TrieNode[] children; public TrieNode() {
isWord = false;
children = new TrieNode[26];
} } public class Trie {
private TrieNode root; public Trie() {
// do intialization if necessary
root = new TrieNode();
} /*
* @param word: a word
* @return: nothing
*/
public void insert(String word) {
// write your code here
if (word == null || word.length() == 0) {
return;
}
TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (p.children[index] == null) {
p.children[index] = new TrieNode();
}
p = p.children[index];
}
p.isWord = true;
} public TrieNode find(String prefix) {
if (prefix == null || prefix.length() == 0) {
return null;
}
TrieNode p = root;
for (int i = 0; i < prefix.length(); i++) {
int index = prefix.charAt(i) - 'a';
if (p.children[index] == null) {
return null;
}
p = p.children[index];
}
return p;
} /*
* @param word: A string
* @return: if the word is in the trie.
*/
public boolean search(String word) {
// write your code here
TrieNode p = find(word);
return p != null && p.isWord;
} /*
* @param prefix: A string
* @return: if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
// write your code here
return find(prefix) != null;
}
}
473. Add and Search Word - Data structure design
class TrieNode {
public boolean isWord;
public char c;
public Map<Character, TrieNode> children; public TrieNode() {
children = new HashMap<>();
} public TrieNode(char c) {
this.c = c;
children = new HashMap<>();
}
} public class WordDictionary {
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
TrieNode root = new TrieNode(); public void addWord(String word) {
// write your code here
if (word == null || word.length() == 0) {
return;
} TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (p.children.get(c) == null) {
TrieNode child = new TrieNode();
p.children.put(c, child);
} p = p.children.get(c);
}
p.isWord = true;
} /*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
public boolean search(String word) {
// write your code here
if (word == null || word.length() == 0) {
return false;
}
TrieNode p = root; for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c != '.') {
if (p.children.get(c) == null) {
return false;
}
p = p.children.get(c);
} else {
for (Map.Entry<Character, TrieNode> entry : p.children.entrySet()) {
if (search(word.substring(0, i) + entry.getKey() + word.substring(i + 1, word.length()))) {
return true;
}
}
return false;
} } return p.isWord;
}
}
132. Word Search II
class TrieNode {
String word;
Map<Character, TrieNode> children; public TrieNode() {
children = new HashMap<>();
}
} class TrieTree {
TrieNode root; public TrieTree(TrieNode node) {
this.root = node;
} public void insert(String word) {
if (word == null || word.length() == 0) {
return;
}
TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!p.children.containsKey(ch)) {
p.children.put(ch, new TrieNode());
}
p = p.children.get(ch);
}
p.word = word;
}
} public class Solution {
private int[] dx = {0, 1, 0, -1};
private int[] dy = {1, 0, -1, 0}; /**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
public List<String> wordSearchII(char[][] board, List<String> words) {
// write your code here
if (words == null || words.size() == 0) {
return new ArrayList<>();
}
Set<String> res = new HashSet<>();
TrieTree tree = new TrieTree(new TrieNode());
for (String word : words) {
tree.insert(word);
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
dfs(board, i, j, res, tree.root);
}
}
return new ArrayList<>(res);
} public void dfs(char[][] board, int x, int y, Set<String> res, TrieNode node) {
TrieNode child = node.children.get(board[x][y]);
if (child == null) {
return;
}
if (child.word != null) {
if (!res.contains(child.word)) {
res.add(child.word);
}
} char tmp = board[x][y];
board[x][y] = 0;
for (int i = 0; i < dx.length; i++) {
int nxtDx = x + dx[i];
int nxtDy = y + dy[i];
if (!isValid(board, nxtDx, nxtDy)) {
continue;
}
dfs(board, nxtDx, nxtDy, res, child);
}
board[x][y] = tmp;
} public boolean isValid(char[][] board, int x, int y) {
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) {
return false;
}
return board[x][y] != 0;
}
}