classTrieNode {// Initialize your data structure here.Character val;HashMap<Character,TrieNode> map;boolean isWord;publicTrieNode() { val =null; map =newHashMap<Character,TrieNode>(); isWord =false; }publicTrieNode(char chr){ val = chr; map =newHashMap<Character,TrieNode>(); isWord =false; }}publicclassTrie {privateTrieNode root;publicTrie() { root =newTrieNode(); }// Inserts a word into the trie.publicvoidinsert(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,newTrieNode(chr)); } cur =cur.map.get(chr);if(i ==word.length() -1) cur.isWord=true; } }// Returns if the word is in the trie.publicbooleansearch(String word) {TrieNode cur = root;for(int i =0; i <word.length(); i++){char chr =word.charAt(i);if(!cur.map.containsKey(chr)) returnfalse; cur =cur.map.get(chr); }returncur.isWord; }// Returns if there is any word in the trie// that starts with the given prefix.publicbooleanstartsWith(String prefix) {TrieNode cur = root;for(int i =0; i <prefix.length(); i++){char chr =prefix.charAt(i);if(!cur.map.containsKey(chr)) returnfalse; cur =cur.map.get(chr); }returntrue; }}
publicclassWordDictionary {privateclassTrieNode{Character chr;HashMap<Character,TrieNode> map;boolean isWord;publicTrieNode(){ map =newHashMap<Character,TrieNode>(); }publicTrieNode(char chr){this.chr= chr; map =newHashMap<Character,TrieNode>(); isWord =false; } }TrieNode root =newTrieNode();// Adds a word into the data structure.publicvoidaddWord(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,newTrieNode(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.publicbooleansearch(String word) {returnsearch(word,0, root); }publicbooleansearch(String word,int index,TrieNode node){if(index ==word.length()) returnnode.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))) returntrue; }returnfalse; } else {if(!node.map.containsKey(chr)) {returnfalse; } else {returnsearch(word, index +1,node.map.get(chr)); } } }}
publicclassWordDictionary {privateclassTrieNode{char chr;TrieNode[] map;boolean isWord;publicTrieNode(){ map =newTrieNode[26]; }publicTrieNode(char chr){this.chr= chr; map =newTrieNode[26]; } }TrieNode root =newTrieNode();// Adds a word into the data structure.publicvoidaddWord(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] =newTrieNode(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.publicbooleansearch(String word) {returnsearch(word,0, root); }publicbooleansearch(String word,int index,TrieNode node){if(index ==word.length()) returnnode.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])) returntrue; } }returnfalse; } else {if(node.map[chrIndex] ==null) {returnfalse; } else {returnsearch(word, index +1,node.map[chrIndex]); } } }}