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 211: Add and Search Word - Data structure design

Add and Search Word - Data structure design leetcode 211
  • xudli
  • xudli
  • 2015年05月19日 06:12
  • 6500

[LeetCode]Add and Search Word - Data structure design,解题报告

目录目录 思路 思路 AC代码思路Design a data structure that supports the following two operations: void addWord(...

Leetcode: Add and Search Word - Data structure design (Java)

最近尝试完全脱离C++,转而只用java刷题。这是我用java做出的第一道树。题目在这里。说几点做题思路。 1. 暴力用Hashtable存单词,显然浪费空间,每个字母每个位次应该只存一次。暴力用R...

Add and Search Word - Data structure design

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

LeetCode Add and Search Word - Data structure design

题目 思路 前缀树,之前有一道LeetCode的题差不多的,我用之前的代码修改了一下即过。代码struct WordDictionary { char c; // sons for...

211. Add and Search Word - Data structure design

题目:添加和搜索单词-数据结构设计 Design a data structure that supports the following two operations: void addWord...

473.Add and Search Word-单词的添加与查找(中等题)

单词的添加与查找 题目设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词。而search(word)则...

[LeetCode]Add and Search Word - Data structure design

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

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
举报原因:
原因补充:

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