首页 > 其他 > 详细

【LeetCode】211. Add and Search Word - Data structure design

时间:2015-07-02 11:42:12      阅读:221      评论:0      收藏:0      [点我收藏+]

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.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
 
Trie树的递归版。
class TrieNode
{
public:
    TrieNode* children[26];
    bool end;
    TrieNode()
    {
        for(int i = 0; i < 26; i ++)
            children[i] = NULL;
        end = false;
    }
};

class WordDictionary {
public:
    WordDictionary()
    {
        root = new TrieNode();
    }
    
    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode* cur = root;
        int i = 0;
        while(i < word.size() && cur->children[word[i]-a] != NULL)
        {
            cur = cur->children[word[i]-a];
            i ++;
        }
        if(i == word.size())
            cur->end = true;
        else
        {
            while(i < word.size())
            {
                cur->children[word[i]-a] = new TrieNode();
                cur = cur->children[word[i]-a];
                i ++;
            }
            cur->end = 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 search(word, root);
    }
    
    bool search(string word, TrieNode* cur)
    {
        if(cur == NULL)
            return false;
        else if(word == "")
            return (cur->end == true);
        else
        {
            if(word[0] != .)
            {
                if(cur->children[word[0]-a] == NULL)
                    return false;
                else
                    return search(word.substr(1), cur->children[word[0]-a]);
            }
            else
            {
                for(int i = 0; i < 26; i ++)
                {
                    if(search(word.substr(1), cur->children[i]))
                        return true;
                }
                return false;
            }
        }
    }
    
    TrieNode* root;
};

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

技术分享

【LeetCode】211. Add and Search Word - Data structure design

原文:http://www.cnblogs.com/ganganloveu/p/4615336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!