unordered_map, c++


  • 0

    class MagicDictionary {
    public:
    /** Build a dictionary through a list of words */
    void buildDict(vector<string> dict) {
    for (auto x : dict)
    {
    m[x.size()].push_back(x);
    }
    }

    /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
    bool search(string word) {
    	for (auto x : m[word.size()])
    	{
    		int dif = 0;
    
    		for (int i = 0; i < word.size(); i++)
    			if (word[i] != x[i])
    				dif++;
    
    		if (dif == 1)
    			return true;
    	}
    	return false;
    }
    unordered_map<int, vector<string>> m;
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.