Add and Search Word - Data structure design

原创 2015年07月07日 01:53:19

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.

class TrieNode
{
public:
    bool isEnd;
    TrieNode *children[26];
    TrieNode() : isEnd(false)
    {
        for (int i = 0; i < 26; i++)
        {
            children[i] = NULL;
        }
    }
};

class WordDictionary {
public:
    WordDictionary()
    {
        root = new TrieNode();
    }
    
    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode *cur = root;
        for (int i = 0; i < word.length(); i++)
        {
            int index = word[i] - 'a';
            if (cur->children[index] == NULL)
            {
                cur->children[index] = new TrieNode();
            }

            cur = cur->children[index];
        }

        cur->isEnd = 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) {
        int n = word.length();
        return search(word, n, 0, root);
    }
    
    bool search(string &word, int n, int pos, TrieNode *cur)
    {
        if (cur == NULL)
        {
            return false;
        }
        if (pos == n)
        {
            return cur->isEnd;
        }

        if (word[pos] == '.')
        {
            for (int i = 0; i < 26; i++)
            {
                if (cur->children[i])
                {
                    if (search(word, n, pos+1, cur->children[i]))
                    {
                        return true;
                    }
                }
            }
        }
        else
        {
            int index = word[pos] - 'a';
            if (cur->children[index])
            {
                return search(word, n, pos+1, cur->children[index]);
            }
        }

        return false;
    }
private:
    TrieNode *root;
};

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode]Add and Search Word - Data structure design

字典树( Trie ) 推荐一个对字典树解释的很好的博客 Trie树:应用于统计和排序  解题思路: 阅读完题目,大家都知道是字典树。对于树的搜索算法有 深度优先搜索算法(dfs)和...

LeetCode211. Add and Search Word - Data structure design前缀树(Trie,字典树)实战

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

Leetcode211 - Add and Search Word - Data structure design

我的新博客地址:HuHaoyu's Blog,最近新增了Jekyll的paginator分页功能和Swiftype的搜索引擎。欢迎参观!

LeetCode Add and Search Word - Data structure design

Add and Search Word - Data structure design  Design a data structure that supports the followin...

Leetcode 211 Add and Search Word - Data structure design

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

LeetCode[211] Add and Search Word - Data structure design

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

56. Merge Intervals\113. Path Sum II\211. Add and Search Word - Data structure design

Merge Intervals 题目描述 代码实现 Path Sum II 题目描述 代码实现56. Merge Intervals题目描述Given a collection of interval...

Add and Search Word - Data structure design

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

Leetcode 211. Add and Search Word - Data structure design

211. Add and Search Word - Data structure design Total Accepted: 26865 Total Submissions: 132...

LeetCode 211 - Add and Search Word - Data structure design

一、问题描述 Description:Description: Design a data structure that supports the following two opera...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)