[LintCode] Add and Search Word 添加和查找单词

Design a data structure that supports the following two operations: addWord(word) and 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.
Notice

You may assume that all words are consist of lowercase letters a-z.
Example

addWord("bad")
addWord("dad")
addWord("mad")
search("pad")  // return false
search("bad")  // return true
search(".ad")  // return true
search("b..")  // return true

LeetCode上的原题,请参见我之前的博客Add and Search Word - Data structure design

class WordDictionary {
public:
    struct TrieNode {
        bool isLeaf;
        TrieNode *child[26];
    };

    WordDictionary() {
        root = new TrieNode();
    }

    // Adds a word into the data structure.
    void addWord(string word) {
        TrieNode *p = root;
        for (char c : word) {
            int i = c - ‘a‘;
            if (!p->child[i]) p->child[i] = new TrieNode();
            p = p->child[i];
        }
        p->isLeaf = 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) {
        search(word, root, 0);
    }

    bool search(string &word, TrieNode *p, int i) {
        if (i == word.size()) return p->isLeaf;
        if (word[i] == ‘.‘) {
            for (auto a : p->child) {
                if (a && search(word, a, i + 1)) return true;
            }
            return false;
        } else {
            return p->child[word[i] - ‘a‘] && search(word, p->child[word[i] - ‘a‘], i + 1);
        }
    }

private:
    TrieNode *root;
};
时间: 2024-11-01 21:13:04

[LintCode] Add and Search Word 添加和查找单词的相关文章

LeetCode 211. Add and Search Word - Data structure design

原题 设计一个包含下面两个操作的数据结构:addWord(word), search(word)addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词查询或是只包含. 和a-z的简易正则表达式的查询.一个 . 可以代表一个任何的字母. 样例 addWord("bad") addWord("dad") addWord("mad") search("pad") // return fals

(Data structure)Implement Trie And Add and Search Word

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z solution: class TrieNode { // Initialize your data structure here. boolean isEnd; Trie

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

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 

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

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 

[LeetCode] 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

[LeetCode] 211. 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

LeetCode——Add and Search Word - Data structure design

Description: 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 a

LeetCode211: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

LeetCode Add and Search Word - Data structure design (trie树)

题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.",能应对这三种就没有问题了.在每个单词的尾字母上标上tag=1,代表从树根到此节点有一个单词.暂时想不到更快的办法. 1 class WordDictionary { 2 public: 3 WordDictionary(){ 4 tree=create(); 5 } 6 void addWord(str