211. Add and Search Word

来源:互联网 发布:java替换指定字符串 编辑:程序博客网 时间:2024/05/05 16:10

  • description
  • implementation

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 any one letter.For example:addWord("bad")addWord("dad")addWord("mad")search("pad") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("b..") -> true

From the description, it is clear that it was a problem relative to trie(prefix tree).

As the range for each node is a - z, so every parent node needs 26 children.

So this problem can be write as :

Data structure:

1 node: each node has 26 children and a flag to indicate whether the current node is termination of a node or not.

2 root: constructor a new node to root.
search(): utilize BFS when meeting with ‘.’. When the char is ‘.’, store all children which is not NULL into queue. When the char c is not ‘.’, judge the child[c-‘a’] is exists or not.

If the queue is empty before we got the right result, return false; if we get the end of string end and there is no isKey == true, return false.

implementation

class TrieNode{public:    TrieNode* child[26];    bool isKey;    TrieNode():isKey(false) {        memset(child, NULL, 26*sizeof(TrieNode*));    }};class WordDictionary {private:    TrieNode* root;public:    /** Initialize your data structure here. */    WordDictionary() {        this->root = new TrieNode();        }    /** Adds a word into the data structure. */    void addWord(string word) {        int len = word.length();        TrieNode* cur = root;        for(int i = 0; i < len; i++) {            if(!cur->child[word[i]-'a'])                 cur->child[word[i]-'a'] = new TrieNode();            cur = cur->child[word[i]-'a'];        }        cur->isKey = 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 len = word.length();        TrieNode* cur = root;        queue<TrieNode*> que;        que.push(root);        for(int i = 0; i < len; i++) {            int sz = que.size();            if(!sz) return false;            for(int j = 0; j < sz; j++) {                cur = que.front();                que.pop();                if(word[i] == '.') {                    for(int k = 0; k < 26; k++) {                         if(cur->child[k]) {                            if(i == len - 1 && cur->child[k]->isKey) return true;                                         que.push(cur->child[k]);                        }                        }                    }                 else if(cur->child[word[i]-'a'])  {                    if(i == len - 1 && cur->child[word[i]-'a']->isKey) return true;                     que.push(cur->child[word[i]-'a']);                }            }            }            return false;    }};/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * bool param_2 = obj.search(word); */
0 0
原创粉丝点击