classValidWordAbbr:def__init__(self,dictionary:List[str]):self.d=defaultdict(set)forsindictionary:self.d[self.abbr(s)].add(s)defisUnique(self,word:str)->bool:s=self.abbr(word)returnsnotinself.dorall(word==tfortinself.d[s])defabbr(self,s:str)->str:returnsiflen(s)<3elses[0]+str(len(s)-2)+s[-1]# Your ValidWordAbbr object will be instantiated and called as such:# obj = ValidWordAbbr(dictionary)# param_1 = obj.isUnique(word)
1 2 3 4 5 6 7 8 910111213141516171819202122232425
classValidWordAbbr{privateMap<String,Set<String>>d=newHashMap<>();publicValidWordAbbr(String[]dictionary){for(vars:dictionary){d.computeIfAbsent(abbr(s),k->newHashSet<>()).add(s);}}publicbooleanisUnique(Stringword){varws=d.get(abbr(word));returnws==null||(ws.size()==1&&ws.contains(word));}privateStringabbr(Strings){intn=s.length();returnn<3?s:s.substring(0,1)+(n-2)+s.substring(n-1);}}/** * Your ValidWordAbbr object will be instantiated and called as such: * ValidWordAbbr obj = new ValidWordAbbr(dictionary); * boolean param_1 = obj.isUnique(word); */
classValidWordAbbr{public:ValidWordAbbr(vector<string>&dictionary){for(auto&s:dictionary){d[abbr(s)].insert(s);}}boolisUnique(stringword){strings=abbr(word);return!d.count(s)||(d[s].size()==1&&d[s].count(word));}private:unordered_map<string,unordered_set<string>>d;stringabbr(string&s){intn=s.size();returnn<3?s:s.substr(0,1)+to_string(n-2)+s.substr(n-1,1);}};/** * Your ValidWordAbbr object will be instantiated and called as such: * ValidWordAbbr* obj = new ValidWordAbbr(dictionary); * bool param_1 = obj->isUnique(word); */
typeValidWordAbbrstruct{dmap[string]map[string]bool}funcConstructor(dictionary[]string)ValidWordAbbr{d:=make(map[string]map[string]bool)for_,s:=rangedictionary{abbr:=abbr(s)if_,ok:=d[abbr];!ok{d[abbr]=make(map[string]bool)}d[abbr][s]=true}returnValidWordAbbr{d}}func(this*ValidWordAbbr)IsUnique(wordstring)bool{ws:=this.d[abbr(word)]returnws==nil||(len(ws)==1&&ws[word])}funcabbr(sstring)string{n:=len(s)ifn<3{returns}returnfmt.Sprintf("%c%d%c",s[0],n-2,s[n-1])}/** * Your ValidWordAbbr object will be instantiated and called as such: * obj := Constructor(dictionary); * param_1 := obj.IsUnique(word); */
classValidWordAbbr{privated:Map<string,Set<string>>=newMap();constructor(dictionary:string[]){for(constsofdictionary){constabbr=this.abbr(s);if(!this.d.has(abbr)){this.d.set(abbr,newSet());}this.d.get(abbr)!.add(s);}}isUnique(word:string):boolean{constws=this.d.get(this.abbr(word));returnws===undefined||(ws.size===1&&ws.has(word));}abbr(s:string):string{constn=s.length;returnn<3?s:s[0]+(n-2)+s[n-1];}}/** * Your ValidWordAbbr object will be instantiated and called as such: * var obj = new ValidWordAbbr(dictionary) * var param_1 = obj.isUnique(word) */