Implement a trie with insert, search, and startsWith methods.
Example:
Trie trie = new Trie();
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.
- Method:
- 使用内部类TrieNode.
class Trie {
TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode temp = root;
for(int i = 0; i < word.length(); i++){
int c = word.charAt(i) - 'a';
if(temp.childs[c] == null)
temp.childs[c] = new TrieNode();
temp = temp.childs[c];
}
temp.isLeaf = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode temp = root;
for(int i = 0; i < word.length(); i++){
int c = word.charAt(i) - 'a';
if(temp.childs[c] != null) temp = temp.childs[c];
else return false;
}
return temp.isLeaf;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode temp = root;
for(int i = 0; i < prefix.length(); i++){
int c = prefix.charAt(i) - 'a';
if(temp.childs[c] == null) return false;
else temp = temp.childs[c];
}
return true;
}
private static class TrieNode{
boolean isLeaf;
TrieNode[] childs;
TrieNode(){
childs = new TrieNode[26];
}
}
}
/**
* 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);
*/
- 最典型的字典树,将每个字符作为一个结点,我们通过遍历的方式,将一个字符串的所有字符添加进入当前的树中。
- 如果已经到了最后一个字符,我们将当前节点的isLeaf设置成true。
class Trie {
private static class TrieNode{
boolean isLeaf;
TrieNode[] childs;
public TrieNode(){
childs = new TrieNode[26];
}
}
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
this.root = new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
int len = word.length();
TrieNode temp = root;
for(int i = 0; i < len; i++){
int c = word.charAt(i) - 'a';
if(temp.childs[c] == null){
temp.childs[c] = new TrieNode();
}
temp = temp.childs[c];
}
temp.isLeaf = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
if(word == null || word.length() == 0) return true;
int len = word.length();
TrieNode temp = this.root;
for(int i = 0; i < len; i++){
int c = word.charAt(i) - 'a';
if(temp.childs[c] == null) return false;
else temp = temp.childs[c];
}
return temp.isLeaf;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
if(prefix == null || prefix.length() == 0) return false;
int len = prefix.length();
TrieNode temp = root;
for(int i = 0; i < len; i++){
int c = prefix.charAt(i) - 'a';
if(temp.childs[c] == null) return false;
temp = temp.childs[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);
*/
class Trie {
private static class Node{
boolean isLeaf;
Node[] childs;
public Node(){
this.childs = new Node[26];
}
}
private Node root;
/** Initialize your data structure here. */
public Trie() {
this.root = new Node();
}
/** Inserts a word into the trie. */
public void insert(String word) {
char[] arr = word.toCharArray();
Node node = root;
for(int i = 0; i < arr.length; i++){
int c = arr[i] - 'a';
if(node.childs[c] == null){
node.childs[c] = new Node();
}
node = node.childs[c];
}
node.isLeaf = true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
char[] arr = word.toCharArray();
Node node = root;
for(int i = 0; i < arr.length; i++){
int c = arr[i] - 'a';
if(node.childs[c] == null){
return false;
}
node = node.childs[c];
}
return node.isLeaf;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
char[] arr = prefix.toCharArray();
Node node = root;
for(int i = 0; i < arr.length; i++){
int c = arr[i] - 'a';
if(node.childs[c] == null){
return false;
}
node = node.childs[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);
*/
- Method 1: Tire Tree
class Trie { private static class Node{ boolean isLeaf; Node[] childs; public Node(){ childs = new Node[26]; } } private Node root; /** Initialize your data structure here. */ public Trie() { this.root = new Node(); } /** Inserts a word into the trie. */ public void insert(String word) { char[] arr = word.toCharArray(); Node temp = root; for(int i = 0; i < arr.length; i++){ if(temp.childs[arr[i] - 'a'] == null){ temp.childs[arr[i] - 'a'] = new Node(); } temp = temp.childs[arr[i] - 'a']; } temp.isLeaf = true; } /** Returns if the word is in the trie. */ public boolean search(String word) { char[] arr = word.toCharArray(); Node temp = root; for(int i = 0; i < arr.length; i++){ temp = temp.childs[arr[i] - 'a']; if(temp == null) return false; } return temp.isLeaf; } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { char[] arr = prefix.toCharArray(); Node temp = root; for(int i = 0; i < arr.length; i++){ temp = temp.childs[arr[i] - 'a']; if(temp == null) return false; } 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); */
class Trie {
public:
/** Initialize your data structure here. */
Trie(): root_(new Node()){}
~Trie(){
delete root_;
}
/** Inserts a word into the trie. */
void insert(string word) {
Node* temp = root_;
for(const char c: word){
if(temp->childs[c - 'a'] == nullptr){
temp->childs[c - 'a'] = new Node();
}
temp = temp->childs[c - 'a'];
}
temp->isLeaf = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Node* temp = find(std::move(word));
return temp == nullptr ? false: temp->isLeaf;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
return find(std::move(prefix)) != nullptr;
}
private:
struct Node{
bool isLeaf;
vector<Node*> childs;
Node(): isLeaf(false), childs(26, nullptr){};
~Node(){
for(auto nodePtr: childs){
delete nodePtr;
}
}
};
Node* find(const string& word){
Node* temp = root_;
for(const char c: word){
if(temp->childs[c - 'a'] == nullptr) return nullptr;
temp = temp->childs[c - 'a'];
}
return temp;
}
Node* root_;
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/