trie.insert(“apple”);
trie.search(“apple”); // returns true
trie.search(“app”); // returns false
trie.startsWith(“app”); // returns true
trie.insert(“app”);
trie.search(“app”); // returns true
Note:
• You may assume that all inputs are consist of lowercase letters a-z.
• All inputs are guaranteed to be non-empty strings.
TC –
SC –
Pseudo code:
class Trie {
/** Initialize your data structure here. */
Trie[] children;
boolean isEndOfWord;
public Trie() {
children = new Trie[26];
isEndOfWord = false;
} /** Inserts a word into the trie. */
public void insert(String word) {
Trie curr = this;
for(char c : word.toCharArray()) {
if(curr.children[c - 'a']==null) {
curr.children[c - 'a'] = new Trie();
}
curr = curr.children[c - 'a'];
}
curr.isEndOfWord = true;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
Trie curr = this;
for(char c : word.toCharArray()) {
curr = curr.children[c - 'a'];
if(curr==null) {
return false;
}
}
if(curr.isEndOfWord) {
return true;
}
return false;
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
Trie curr = this;
for(char c : prefix.toCharArray()) {
curr = curr.children[c - 'a'];
if(curr==null) {
return false;
}
}
return true;
}
}