class Trie {
private class Node {
boolean end;
Node[] child;
public Node() {
end = false;
child = new Node[26];
}
}
private Node root;
/** Initialize your data structure here. */
public Trie() {
root = new Node();
}
/** Inserts a word into the trie. */
public void insert(String word) {
Node curr = root;
for (char c : word.toCharArray()) {
if (curr.child[c - 'a'] == null) {
curr.child[c - 'a'] = new Node();
}
curr = curr.child[c - 'a'];
}
curr.end = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
Node curr = root;
for (char c : word.toCharArray()) {
if (curr.child[c - 'a'] == null) return false;
curr = curr.child[c - 'a'];
}
return curr.end;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
Node curr = root;
for (char c : prefix.toCharArray()) {
if (curr.child[c - 'a'] == null) return false;
curr = curr.child[c - 'a'];
}
return true;
}
}