208. Implement Trie
Problem:
6/5/2018 update:
现在倾向于用hash map来表示children.
Analysis:
Implement a trie with
insert
, search
, and startsWith
methods.6/5/2018 update:
现在倾向于用hash map来表示children.
class TrieNode { Map<Character, TrieNode> children; boolean isWord; public TrieNode() { children = new HashMap<>(); } } TrieNode root; /** Initialize your data structure here. */ public WordDictionary() { root = new TrieNode(); } /** Adds a word into the data structure. */ public void addWord(String word) { TrieNode cur = root; for (char c: word.toCharArray()) { if (!cur.children.containsKey(c)) { cur.children.put(c, new TrieNode()); } cur = cur.children.get(c); } cur.isWord = true; }--------------------------------------------------------------------------------------------------------------------------
Analysis:
第一次接触trie的时候,还消化了挺久。经过几个月的刷题,功力大增。第二次实现trie,秒过啊。而且还优化了实现方法。
Trie node is the key to trie tree.
Solution:
class Trie { class TrieNode { char val; TrieNode[] children; boolean isWord; public TrieNode() {} public TrieNode(char val) { this.val = val; children = new TrieNode[26]; } } TrieNode root; /** Initialize your data structure here. */ public Trie() { root = new TrieNode(' '); } /** Inserts a word into the trie. */ public void insert(String word) { TrieNode node = root; for (char c: word.toCharArray()) { if (node.children[c - 'a'] == null) { node.children[c - 'a'] = new TrieNode(c); } node = node.children[c - 'a']; } node.isWord = true; } /** Returns if the word is in the trie. */ public boolean search(String word) { TrieNode node = root; for (char c: word.toCharArray()) { if (node.children[c - 'a'] == null) { return false; } node = node.children[c - 'a']; } return node.isWord; } /** Returns if there is any word in the trie that starts with the given prefix. */ public boolean startsWith(String prefix) { TrieNode node = root; for (char c: prefix.toCharArray()) { if (node.children[c - 'a'] == null) { return false; } node = node.children[c - 'a']; } 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); */--------------------------------------------------------------------------------------------------------------------------
/**
* Your Trie object will be instantiated and called as such:
* Trie trie = new Trie();
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
class TrieNode {
// Initialize your data structure here.
private TrieNode[] links;
private final int R=26;
private boolean isEnd;
public TrieNode() {
links=new TrieNode[R];
}
public boolean containsKey(char c)
{
return links[c-'a']!=null;
}
public TrieNode get(char c)
{
return links[c-'a'];
}
public void put(char c,TrieNode t)
{
links[c-'a']=t;
}
public void setEnd()
{
isEnd=true;
}
public boolean isEnd()
{
return isEnd;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode node=root;
for(int i=0;i<word.length();i++)
{
char c=word.charAt(i);
if(!node.containsKey(c))
{
node.put(c,new TrieNode());
}
node=node.get(c);
}
node.setEnd();
}
private TrieNode searchPrefix(String word)
{
TrieNode node=root;
for(int i=0;i<word.length();i++)
{
char c=word.charAt(i);
if(!node.containsKey(c))
{
return null;
}
node=node.get(c);
}
return node;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode node = searchPrefix(word);
return node != null && node.isEnd();
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode node = searchPrefix(prefix);
return node != null;
}
}
评论
发表评论