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.

答案

这个题目和Implement Trie基本是一样的,只是要在search函数上增加对通配符 "."的支持
“.” 可以match任何字符,所以马上可以想到,要用recursion
为什么?
因为原来在trie上搜索只需要搜索一条路径,可以比作一条直线,所以一个循环iterate过去就基本能找到答案了

现在要搜索未知条路径,搜索的结果是一棵树,用recursion来做是最方便了

下面的答案除了search函数的部分之外,其他代码和Implement Trie的答案是一样的

class WordDictionary {

    class TrieNode {
        // Initialize your data structure here.
        public TrieNode() {
            children = new TrieNode[26];
            bLeaf = false;
        }
        int value;
        boolean bLeaf;
        TrieNode[] children;
    }

    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 curr = root;
        for(int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if(!charExist(c, curr)) {
                TrieNode newnode = new TrieNode();
                newnode.value = c;
                insertChar(c, curr, newnode);                
            }
            // You will need to mark this node as leaf, even if it is not really a leaf in the tree
            // but it is a leaf for this particular string word
            
            curr = nextNode(c, curr);
            if(i == word.length() - 1)
                curr.bLeaf = 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_recur(word, 0, root);
    }
    
    public boolean search_recur(String word, int index, TrieNode curr) {
        
        // Base cases
        char c = word.charAt(index);
        if(index == word.length() - 1) {
            if(c == '.') {
                // if curr has any children's bleaf = true , return true
                for(int i = 0; i < 26; i++) {
                    char a = (char)('a' + i);
                    if(charExist(a, curr) && nextNode(a, curr).bLeaf == true) return true;
                }
                return false;
            }
            else {
                // if the corresponding children's bleaf = true return true
                return (charExist(c, curr) && nextNode(c, curr).bLeaf == true);
            }
        }
        
        // Recursive cases
        if(c == '.') {
            boolean ret = false;
            for(int i = 0; i < 26; i++) {
                char a = (char)('a' + i);
                if(charExist(a, curr))
                    ret = ret | search_recur(word, index + 1, nextNode(a, curr));
            }
            return ret;
        }
        else {
            if(charExist(c, curr)) {
                return search_recur(word, index + 1, nextNode(c, curr));
            }   
            else {
                return false;
            }
        }
    }
    
    private boolean charExist(char c, TrieNode curr) {
        return curr.children[c - 'a'] != null;
    }
    private TrieNode nextNode(char c, TrieNode curr) {
        return curr.children[c - 'a'];
    }
    private void insertChar(char c, TrieNode curr, TrieNode ins) {
        curr.children[c - 'a'] = ins;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

你可能感兴趣的:(Add and Search Word - Data structure design)