208. Implement Trie (Prefix Tree) && 211. Add and Search Word - Data structure design

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

 

Subscribe to see which companies asked this question

Hide Tags
 Design Trie

 

class TrieNode {
    
    public TrieNode[] children = new TrieNode[26];
    public boolean exist = false;
    
    public TrieNode() {
        
    }
}

public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    public void insert(String word) {
        TrieNode r = root;
        for(int i = 0; i<word.length(); ++i)
        {
            Character c = word.charAt(i);
            TrieNode child = r.children[c-'a'];
            if(child == null)
            {
                child = new TrieNode();
                r.children[c-'a'] = child;
            }
            r = child;
        }
        r.exist = true;
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode r = root;
        for(int i = 0; i<word.length(); ++i)
        {
            Character c = word.charAt(i);
            TrieNode child = r.children[c-'a'];
            if(child == null)
                return false;
            r = child;
        }
        return r.exist;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        TrieNode r = root;
        for(int i = 0; i<prefix.length(); ++i)
        {
            Character c = prefix.charAt(i);
            TrieNode child = r.children[c-'a'];
            if(child == null)
                return false;
            r = child;
        }
        return true;
    }
}

// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

 

 

211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

 
Hide Tags
 Backtracking Trie Design
Hide Similar Problems
 (M) Implement Trie (Prefix Tree)
 
 
public class WordDictionary {

    class TrieNode {
        public TrieNode[] children = new TrieNode[26];
        public boolean exist = false;
        
        public TrieNode() {
            
        }
    }
    
    private TrieNode root = new TrieNode();
    
    
    // Adds a word into the data structure.
    public void addWord(String word) {
        TrieNode r = root;
        for(int i = 0; i<word.length(); ++i)
        {
            Character c = word.charAt(i);
            TrieNode child = r.children[c-'a'];
            if(child == null)
            {
                child = new TrieNode();
                r.children[c-'a'] = child;
            }
            r = child;
        }
        r.exist = true;
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    public boolean search(String word) {
        return search(root, word, 0);
    }
    
    // check currentNode(cn)'s children from currentIndex to the end
    private boolean search(TrieNode cn, String word, int currentIndex) {
        if(cn == null )
            return false;
        
        
        Character c = word.charAt(currentIndex);
        if(c.equals('.'))
        {
            for(int i = 0; i<26; ++i)
            {
                TrieNode nn = cn.children[i];
                if(nn == null)
                    continue;
                
                if(currentIndex == word.length() -1)
                {
                    if(nn.exist)
                        return true;
                    continue;
                }
                
                if(search(nn, word, currentIndex + 1))
                    return true;
            }   
            return false;
        }
        else 
        {
            cn = cn.children[c-'a'];
            if(cn == null)
                return false;
            if(currentIndex == word.length() -1)
            {
                return cn.exist;
            }
            else
            {
                return search(cn, word, currentIndex + 1);
            }
        }
    }
}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

 

 
 
 
posted @ 2016-05-30 08:19  新一代的天皇巨星  阅读(178)  评论(0)    收藏  举报