211. Design Add and Search Words Data Structure

1. Description

Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary class:

  • WordDictionary() Initializes the object.
  • void addWord(word) Adds word to the data structure, it can be matched later.
  • bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots ‘.’ where dots can be matched with any letter.

2. Example

Example 1:
Input
[“WordDictionary”,“addWord”,“addWord”,“addWord”,“search”,“search”,“search”,“search”]
[[],[“bad”],[“dad”],[“mad”],[“pad”],[“bad”],[".ad"],[“b.."]]
Output
[null,null,null,null,false,true,true,true]

Explanation
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord(“bad”);
wordDictionary.addWord(“dad”);
wordDictionary.addWord(“mad”);
wordDictionary.search(“pad”); // return False
wordDictionary.search(“bad”); // return True
wordDictionary.search(".ad”); // return True
wordDictionary.search(“b.."); // return True

3. Constraints

  • 1 <= word.length <= 500
  • word in addWord consists lower-case English letters.
  • word in search consist of ‘.’ or lower-case English letters.
  • At most 50000 calls will be made to addWord and search.

4. Solutions

My Accepted Solution

m is the number of alphabets, n is the number of dots
Time complexity(WordDictionary()): O(1)
Time complexity(addWord()): O(m)
Time complexity(search()): O($m + 26^n$)
Space complexity(WordDictionary()): O(1)
Space complexity(addWord()): O(m)
Space complexity(search()): O(m + n)

class TrieNode {
public:
    array<TrieNode*, 26> childs;  // 26 lower case letters
    bool isEnd;

    TrieNode() : isEnd(false) {
        for (int i = 0; i < childs.size(); ++i) {
            childs[i] = nullptr;
        }
    }
};

class WordDictionary {
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        head = new TrieNode();
    }

    // void addWord(string word)
    void addWord(const string& i_word) {
        auto iter = head;
        for (auto letter : i_word) {
            if (iter->childs[getLetterIndex(letter)] == nullptr) {
                iter->childs[getLetterIndex(letter)] = new TrieNode();
            }

            iter = iter->childs[getLetterIndex(letter)];
        }

        iter->isEnd = true;
    }

    // bool search(string word)
    bool search(const string& i_word) {
        return search(head, i_word, 0);
    }

private:
    TrieNode* head;

    int getLetterIndex(char letter) {
        return letter - 'a';
    }

    bool search(TrieNode* i_head, const string& i_word, int firstLetterIndex) {
        auto iter = i_head;
        for (int i = firstLetterIndex; i < i_word.size(); ++i) {
            char letter = i_word[i];
            if (letter == '.') {
                for (char leeterToTry = 'a'; leeterToTry <= 'z'; ++leeterToTry) {
                    if (iter->childs[getLetterIndex(leeterToTry)] != nullptr &&
                        search(iter->childs[getLetterIndex(leeterToTry)],
                               i_word,
                               i + 1)) {
                        return true;
                    }
                }
                return false;
            } else {
                if (iter->childs[getLetterIndex(letter)] == nullptr) {
                    return false;
                }

                iter = iter->childs[getLetterIndex(letter)];
            }
        }
        
        return iter->isEnd;
    }
};
comments powered by Disqus