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.
classTrie{
Node root;/** Initialize your data structure here. */publicTrie(){
root =newNode();}/** Inserts a word into the trie. */publicvoidinsert(String word){if(word==null){return;}char[] chs = word.toCharArray();
Node cur = root;
cur.pass++;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(cur.nexts[index]==null){
cur.nexts[index]=newNode();}
cur = cur.nexts[index];
cur.pass++;}
cur.end++;}/** Returns if the word is in the trie. */publicbooleansearch(String word){if(word==null || word.equals("")){returnfalse;}char[] chs = word.toCharArray();
Node cur = root;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(cur.nexts[index]==null){returnfalse;}
cur = cur.nexts[index];}return cur.end>0;}/** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix){if(prefix==null || prefix.equals("")){returnfalse;}char[] chs = prefix.toCharArray();
Node cur = root;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(cur.nexts[index]==null){returnfalse;}
cur = cur.nexts[index];}return cur.pass>0;}privateclassNode{int pass;int end;
Node[] nexts;Node(){
nexts =newNode[26];}}}
/*
先定义前缀树节点
*/classTrieNode{int pass;int end;
TrieNode[] nexts;TrieNode(){
nexts =newTrieNode[26];}}/*
接着实现前缀树
*/classTrie{//一切要从根节点开始
TrieNode root;/** Initialize your data structure here. */publicTrie(){
root =newTrieNode();}/** Inserts a word into the trie. */publicvoidinsert(String word){//input check(也不用写, 因为题目保证了输入非空的string)if(word==null)return;char[] chs = word.toCharArray();
TrieNode curr = root;
curr.pass++;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(curr.nexts[index]==null)
curr.nexts[index]=newTrieNode();
curr = curr.nexts[index];
curr.pass++;}//路径最后一个节点的end要++
curr.end++;}/** Returns if the word is in the trie. */publicbooleansearch(String word){if(word==null)returnfalse;char[] chs = word.toCharArray();
TrieNode curr = root;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(curr.nexts[index]== null)returnfalse;
curr = curr.nexts[index];}return curr.end>0;}/** Returns if there is any word in the trie that starts with the given prefix. */publicbooleanstartsWith(String prefix){if(prefix==null)returnfalse;char[] chs = prefix.toCharArray();
TrieNode curr = root;for(int i=0; i<chs.length; i++){int index = chs[i]-'a';if(curr.nexts[index]==null)returnfalse;
curr = curr.nexts[index];}returntrue;}}/**
* 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);
*/
welcome to my blogLeetCode Top 100 Liked Questions 208. Implement Trie (Prefix Tree) (Java版; Medium)题目描述Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie...