211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

设计一个支持以下两个操作的数据结构:
void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 意味着它可以代表任何一个字母。
例如:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
注意事项:
你可以假设所有单词都是由小写字母 a-z 组成的。

详见:https://leetcode.com/problems/add-and-search-word-data-structure-design/description/

Java实现:

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

class WordDictionary {
    private 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 node = root;
        for (char ch: word.toCharArray()) {
            if (node.children[ch-'a'] == null) {
                node.children[ch-'a'] = new TrieNode();
            }
            node = node.children[ch-'a'];
        }
        node.isWord = 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 helper(word, 0, root);
    }
    
    private boolean helper(String word, int start, TrieNode node) {
        if (start == word.length()){
            return node.isWord;
        } 
        char ch = word.charAt(start);
        if (ch == '.') {
            for (int i = 0; i < 26; i++) {
                if (node.children[i] != null && helper(word, start+1, node.children[i])) {
                    return true;
                }
            }
        } else {
            return node.children[ch-'a'] != null && helper(word, start+1, node.children[ch-'a']);
        }
        return false;
    }
}

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

C++实现:

class TrieNode {
public:
    TrieNode *next[26];
    char c;
    bool isWord;
    TrieNode() : isWord(false) {
        for (auto & c: next)
        {
            c=nullptr;
        }
    }
    TrieNode(char _c):c(_c),isWord(false)
    {
        for(auto &c:next)
        {
            c=nullptr;
        }
    }
};
class WordDictionary {
public:    
    WordDictionary() {
        root = new TrieNode();
    }
    
    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode *p = root;
        for (auto &c : word) 
        {
            int i = c - 'a';
            if (!p->next[i])
            {
                p->next[i] = new TrieNode(c);
            }
            p = p->next[i];
        }
        p->isWord = true;
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    bool search(string word) {
        return searchWord(word, root, 0);
    }
    
    bool searchWord(string &word, TrieNode *p, int i) {
        if (i == word.size())
        {
            return p->isWord;
        }
        if (word[i] == '.')
        {
            for (auto &c : p->next) 
            {
                if (c && searchWord(word, c, i + 1))
                {
                    return true;
                }
            }
            return false;
        }
        else 
        {
            return p->next[word[i] - 'a'] && searchWord(word, p->next[word[i] - 'a'], i + 1);
        }
    }
    
private:
    TrieNode *root;
};

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

 参考:https://www.cnblogs.com/grandyang/p/4507286.html

posted on 2018-04-08 19:35  lina2014  阅读(160)  评论(0编辑  收藏  举报

导航