class Trie { /** Initialize your data structure here. */ private Trie[] dict; private boolean isEnd; public Trie() { dict = new Trie[26]; isEnd = false; } /** Inserts a word into the trie. */ public void insert(String word) { Trie node = this; for(int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if(node.dict[index] == null) { node.dict[index] = new Trie(); } node = node.dict[index]; } node.isEnd = true; } /** Returns if the word is in the trie. */ public boolean search(String word) { Trie node = searchPrefix(word); // if(node != null && node.isEnd) { // return true; // } // return false; return node != null && node.isEnd; } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { return searchPrefix(prefix) != null; } public Trie searchPrefix(String word) { Trie node = this; for(int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if(node.dict[index] == null) { return null; } node = node.dict[index]; } return node; }}/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */