Posted on

Description

Submission

class MagicDictionary {
    vector<string> words;
public:
    MagicDictionary() {
    }
    
    void buildDict(vector<string> dictionary) {
        words = dictionary;
    }
    
    bool search(string searchWord) {
        int len = searchWord.length();

        for(auto& word: words){
            if(word.length() != len) continue;
            int diff = 0;
            for(int i = 0; i < len; ++i) {
                if(word[i] != searchWord[i]) {
                    diff++;
                    if(diff > 1) break;
                }
            }
            if(diff == 1) return true;
        }

        return false;
    }
};

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary* obj = new MagicDictionary();
 * obj->buildDict(dictionary);
 * bool param_2 = obj->search(searchWord);
 */

Leave a Reply

Your email address will not be published. Required fields are marked *