| 31 | } |
| 32 | |
| 33 | public boolean search(String word) { |
| 34 | TrieNode node = this.root; |
| 35 | for (char c : word.toCharArray()) { |
| 36 | // For each character in the word, if it's not a child of |
| 37 | // the current node, the word doesn't exist in the Trie. |
| 38 | if (!node.children.containsKey(c)) { |
| 39 | return false; |
| 40 | } |
| 41 | node = node.children.get(c); |
| 42 | } |
| 43 | // Return whether the current node is marked as the end of the |
| 44 | // word. |
| 45 | return node.isWord; |
| 46 | } |
| 47 | |
| 48 | public boolean hasPrefix(String prefix) { |
| 49 | TrieNode node = this.root; |