Add and Search Word

Description

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

Assumption

Trie tree? Yes

Lintcode_ladder

Method

  1. Trie Tree, reduce memory requirement than hash map
  2. x

Example

  1. 1
code
class TreyNode {
public:
    bool canFind;
    TreyNode* next[26];
    TreyNode() {
        canFind = false;
        for (int i = 0; i < 26; ++i) {
            next[i] = nullptr;
        }
    }
};
class WordDictionary {
public:
    TreyNode* root;
    WordDictionary() {
        root = nullptr;
    }
    // Adds a word into the data structure.
    void addWord(string word) {
        // Write your code here
        if (word.empty()) {
            return;
        }
        if (root == nullptr) {
            root = new TreyNode();
        }
        TreyNode* cur = root;
        int len = word.size();
        for (int i = 0; i < len; ++i) {
            int index = word[i] - 'a';
            if (index >= 26) {
                return;
            }
            if (cur->next[index] == nullptr) {
                cur->next[index] = new TreyNode();
            }
            cur = cur->next[index];
        }
        cur->canFind = 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) {
        // Write your code here
        if (root == nullptr) {
            return false;
        }
        if (word.empty()) {
            return false;
        }
        return find(word, 0, root);
    }
    bool find(string& word, int index, TreyNode* cur) {
        if (!cur) {
            return false;
        }
        if (index == word.size()) {
            return cur->canFind;
        }
        //int len = word.size();
        char ch = word[index];
        int chIndex = ch - 'a'; 
        if (ch == '.') {
            for (int i = 0; i < 26; ++i) {
                if (!cur->next[i]) {
                    continue;
                }
                // any one can carry '.'
                // if so and match all, we return true
                if (find(word, index + 1, cur->next[i])) {
                    return true;
                }
            }
            // if nothing can carry '.'
            return false;
        } else if ( chIndex < 26 && cur->next[chIndex]) {
            return find(word, index + 1, cur->next[chIndex]);
        } else {
            return false;
        }
    }
};
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

Similar problems

x

Tags

x

results matching ""

    No results matching ""