publicclassWordDictionary {TrieNode root;publicWordDictionary() { root =newTrieNode(); }// Adds a word into the data structure.publicvoidaddWord(String word) {root.addWord(word); }// Returns if the word is in the data structure. A word could// contain the dot character '.' to represent any one letter.publicbooleansearch(String word) {returnsearchR(word, root,0); }privatebooleansearchR(String word,TrieNode node,int index) {if (word.length() == index) {returnnode.isEnd; }Character cur =word.charAt(index);if (cur =='.') {for (Map.Entry<Character,TrieNode> entry :node.children.entrySet()) {if (searchR(word,entry.getValue() ,index +1)) {returntrue; } }returnfalse; } else {TrieNode nextNode =node.children.get(cur);if (nextNode ==null) {returnfalse; } else {returnsearchR(word, nextNode, index +1); } } }}// Your WordDictionary object will be instantiated and called as such:// WordDictionary wordDictionary = new WordDictionary();// wordDictionary.addWord("word");// wordDictionary.search("pattern");classTrieNode {publicHashMap<Character,TrieNode> children;publicboolean isEnd;publicTrieNode() { children =newHashMap<>(); isEnd =false; }publicvoidaddWord(String word) {if (word ==null||word.isEmpty()) {return; }Character first =word.charAt(0);TrieNode child =children.get(first);if (child ==null) { child =newTrieNode();children.put(first, child); }if (word.length() >1) {child.addWord(word.substring(1)); } else {child.isEnd=true; } } }