Public class Num208 implements the Trie prefix tree {class Trie {private class TrieNode {// Whether there is a word ending with this character Boolean isEnd; // TrieNode[] next; publicTrieNode() {
                isEnd = false; next = new TrieNode[26]; }} private TrieNode root; publicTrie() {
            root = new TrieNode();
        }

        public void insert(String word) {
            TrieNode node = root;
            for (char c : word.toCharArray()) {
                if(node.next[c-'a']==null){
                    node.next[c - 'a'] = new TrieNode();
                }
                node = node.next[c - 'a'];
            }
            node.isEnd = true;
        }


        public boolean search(String word) {
            TrieNode node = root;
            for (char c : word.toCharArray()) {
                node = node.next[c - 'a'];
                if(node==null) return false;
            }
            return node.isEnd;
        }


        public boolean startsWith(String prefix) {
            TrieNode node = root;
            for (char c : prefix.toCharArray()) {
                node = node.next[c - 'a'];
                if(node==null) return false;
            }
            return true; }}}Copy the code