classMagicDictionary:def__init__(self):""" Initialize your data structure here. """def_patterns(self,word):return[word[:i]+'*'+word[i+1:]foriinrange(len(word))]defbuildDict(self,dictionary:List[str])->None:self.words=set(dictionary)self.counter=Counter(pforwordindictionaryforpinself._patterns(word))defsearch(self,searchWord:str)->bool:forpinself._patterns(searchWord):ifself.counter[p]>1or(self.counter[p]==1andsearchWordnotinself.words):returnTruereturnFalse# Your MagicDictionary object will be instantiated and called as such:# obj = MagicDictionary()# obj.buildDict(dictionary)# param_2 = obj.search(searchWord)
classMagicDictionary{privateSet<String>words;privateMap<String,Integer>counter;/** Initialize your data structure here. */publicMagicDictionary(){words=newHashSet<>();counter=newHashMap<>();}publicvoidbuildDict(String[]dictionary){for(Stringword:dictionary){words.add(word);for(Stringp:patterns(word)){counter.put(p,counter.getOrDefault(p,0)+1);}}}publicbooleansearch(StringsearchWord){for(Stringp:patterns(searchWord)){intcnt=counter.getOrDefault(p,0);if(cnt>1||(cnt==1&&!words.contains(searchWord))){returntrue;}}returnfalse;}privateList<String>patterns(Stringword){List<String>res=newArrayList<>();char[]chars=word.toCharArray();for(inti=0;i<chars.length;++i){charc=chars[i];chars[i]='*';res.add(newString(chars));chars[i]=c;}returnres;}}/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary obj = new MagicDictionary(); * obj.buildDict(dictionary); * boolean param_2 = obj.search(searchWord); */
classMagicDictionary{public:/** Initialize your data structure here. */MagicDictionary(){}voidbuildDict(vector<string>dictionary){for(stringword:dictionary){words.insert(word);for(stringp:patterns(word))++counter[p];}}boolsearch(stringsearchWord){for(stringp:patterns(searchWord)){if(counter[p]>1||(counter[p]==1&&!words.count(searchWord)))returntrue;}returnfalse;}private:unordered_set<string>words;unordered_map<string,int>counter;vector<string>patterns(stringword){vector<string>res;for(inti=0;i<word.size();++i){charc=word[i];word[i]='*';res.push_back(word);word[i]=c;}returnres;}};/** * Your MagicDictionary object will be instantiated and called as such: * MagicDictionary* obj = new MagicDictionary(); * obj->buildDict(dictionary); * bool param_2 = obj->search(searchWord); */
typeMagicDictionarystruct{wordsmap[string]boolcountermap[string]int}/** Initialize your data structure here. */funcConstructor()MagicDictionary{returnMagicDictionary{words:make(map[string]bool),counter:make(map[string]int),}}func(this*MagicDictionary)BuildDict(dictionary[]string){for_,word:=rangedictionary{this.words[word]=truefor_,p:=rangepatterns(word){this.counter[p]++}}}func(this*MagicDictionary)Search(searchWordstring)bool{for_,p:=rangepatterns(searchWord){ifthis.counter[p]>1||(this.counter[p]==1&&!this.words[searchWord]){returntrue}}returnfalse}funcpatterns(wordstring)[]string{varres[]stringfori:=0;i<len(word);i++{res=append(res,word[:i]+"."+word[i+1:])}returnres}/** * Your MagicDictionary object will be instantiated and called as such: * obj := Constructor(); * obj.BuildDict(dictionary); * param_2 := obj.Search(searchWord); */