classTrie{privateclassTrieNode{TrieNode[] children;boolean isWord;TrieNode(){
children =newTrieNode[26];}}privateTrieNode root;/**
* Initialize your data structure here.
*/publicTrie(){
root =newTrieNode();}/**
* Inserts a word into the trie.
*/publicvoidinsert(String word){TrieNode cur = root;for(char ch : word.toCharArray()){if(null== cur.children[ch -'a'])
cur.children[ch -'a']=newTrieNode();
cur = cur.children[ch -'a'];}
cur.isWord =true;}/**
* Returns if the word is in the trie.
*/publicbooleansearch(String word){TrieNode cur = root;for(char ch : word.toCharArray()){if(null== cur.children[ch -'a'])returnfalse;
cur = cur.children[ch -'a'];}return cur.isWord;}/**
* Returns if there is any word in the trie that starts with the given prefix.
*/publicbooleanstartsWith(String prefix){TrieNode cur = root;for(char ch : prefix.toCharArray()){if(null== cur.children[ch -'a'])returnfalse;
cur = cur.children[ch -'a'];}returntrue;}}