Solution 1
Java
class Trie {
Map<Character, Trie> children;
boolean isWord;
public Trie() {
this.children = new HashMap<>();
this.isWord = false;
}
public void insert(String word) {
Trie current = this;
for (char c: word.toCharArray()) {
current.children.putIfAbsent(c, new Trie());
current = current.children.get(c);
}
current.isWord = true;
}
public boolean search(String word) {
Trie current = this;
for (char c: word.toCharArray()) {
if (!current.children.containsKey(c)) {
return false;
}
current = current.children.get(c);
}
return current.isWord;
}
public boolean startsWith(String prefix) {
Trie current = this;
for (char c: prefix.toCharArray()) {
if (!current.children.containsKey(c)) {
return false;
}
current = current.children.get(c);
}
return true;
}
}
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/