Implement Trie (Prefix Tree) LeetCode solution in Java
1class Trie {2private class Node {3char character;4Node[] sub;56Node(char c) {7this.character = c;8sub = new Node[26];9}10}1112Node root;13HashMap<String, Boolean> map;1415public Trie() {16root = new Node('\0');17map = new HashMap<>();18}1920public void insert(String word) {21Node temp = root;22for (char c : word.toCharArray()) {23int index = c - 'a';24if (temp.sub[index] == null) temp.sub[index] = new Node(c);2526temp = temp.sub[index];27}28map.put(word, true);29}3031public boolean search(String word) {32return map.containsKey(word);33}3435public boolean startsWith(String prefix) {36Node temp = root;37for (char c : prefix.toCharArray()) {38int index = c - 'a';39if (temp.sub[index] == null) return false;40temp = temp.sub[index];41}42return true;43}44}