class TrieNode {
// Initialize your data structure here.
Character val;
HashMap<Character, TrieNode> map;
boolean isWord;
public TrieNode() {
val = null;
map = new HashMap<Character, TrieNode>();
isWord = false;
}
public TrieNode(char chr){
val = chr;
map = new HashMap<Character, TrieNode>();
isWord = false;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char chr = word.charAt(i);
if(!cur.map.containsKey(chr)){
cur.map.put(chr, new TrieNode(chr));
}
cur = cur.map.get(chr);
if(i == word.length() - 1) cur.isWord = true;
}
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char chr = word.charAt(i);
if(!cur.map.containsKey(chr)) return false;
cur = cur.map.get(chr);
}
return cur.isWord;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode cur = root;
for(int i = 0; i < prefix.length(); i++){
char chr = prefix.charAt(i);
if(!cur.map.containsKey(chr)) return false;
cur = cur.map.get(chr);
}
return true;
}
}
public class WordDictionary {
private class TrieNode{
Character chr;
HashMap<Character, TrieNode> map;
boolean isWord;
public TrieNode(){
map = new HashMap<Character, TrieNode>();
}
public TrieNode(char chr){
this.chr = chr;
map = new HashMap<Character, TrieNode>();
isWord = false;
}
}
TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char chr = word.charAt(i);
if(!cur.map.containsKey(chr)){
cur.map.put(chr, new TrieNode(chr));
}
cur = cur.map.get(chr);
}
cur.isWord = true;
}
// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
return search(word, 0, root);
}
public boolean search(String word, int index, TrieNode node){
if(index == word.length()) return node.isWord;
char chr = word.charAt(index);
if(chr == '.'){
Set<Character> keySet = node.map.keySet();
for(Character next : keySet){
if(search(word, index + 1, node.map.get(next))) return true;
}
return false;
} else {
if(!node.map.containsKey(chr)) {
return false;
} else {
return search(word, index + 1, node.map.get(chr));
}
}
}
}
public class WordDictionary {
private class TrieNode{
char chr;
TrieNode[] map;
boolean isWord;
public TrieNode(){
map = new TrieNode[26];
}
public TrieNode(char chr){
this.chr = chr;
map = new TrieNode[26];
}
}
TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char chr = word.charAt(i);
int index = chr - 'a';
if(cur.map[index] == null){
cur.map[index] = new TrieNode(chr);
}
cur = cur.map[index];
}
cur.isWord = true;
}
// Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
return search(word, 0, root);
}
public boolean search(String word, int index, TrieNode node){
if(index == word.length()) return node.isWord;
char chr = word.charAt(index);
int chrIndex = chr - 'a';
if(chr == '.'){
for(int i = 0; i < 26; i++){
if(node.map[i] != null){
if(search(word, index + 1, node.map[i])) return true;
}
}
return false;
} else {
if(node.map[chrIndex] == null) {
return false;
} else {
return search(word, index + 1, node.map[chrIndex]);
}
}
}
}
这题难度是 Hard,一是因为要用到数据结构 trie,另一方面又融合了 Word Search I 里面在矩阵里面搜索的 dfs + backtracking;