import java.util.HashMap;/**
* @ClassName DictTree
* @Description TODO
* @Author chendeyu
* @Date 2021/1/8 14:38
*/publicclassDictTree{classTrieNode{boolean flag;
HashMap<Character, TrieNode> next =newHashMap<>();publicTrieNode(){
flag =false;}}
TrieNode root;DictTree(){this.root =newTrieNode();}/**
* Inserts a word into the trie.
*/publicvoidinsert(String word){
TrieNode node = root;for(Character c : word.toCharArray()){if(!node.next.containsKey(c)){
TrieNode newNode =newTrieNode();
node.next.put(c, newNode);
node = newNode;}else{
node = node.next.get(c);}}
node.flag =true;}/**
* Returns if the word is in the trie.
*/publicbooleansearch(String word){if(word.length()==0)returntrue;
TrieNode node = root;for(Character c : word.toCharArray()){if(!node.next.containsKey(c))returnfalse;
node = node.next.get(c);}return node.flag;}/**
* Returns if there is any word in the trie that starts with the given prefix.
*/publicbooleanstartsWith(String prefix){if(prefix.length()==0)returntrue;
TrieNode node = root;for(Character c : prefix.toCharArray()){if(!node.next.containsKey(c))returnfalse;
node = node.next.get(c);}returntrue;}publicstaticvoidmain(String[] args){final DictTree dictTree =newDictTree();
dictTree.insert("大江大河");
dictTree.insert("大江南北");
System.out.println(dictTree.startsWith("大"));}}