classWordsFrequency:def__init__(self,book:List[str]):self.cnt=Counter(book)defget(self,word:str)->int:returnself.cnt[word]# Your WordsFrequency object will be instantiated and called as such:# obj = WordsFrequency(book)# param_1 = obj.get(word)
1 2 3 4 5 6 7 8 910111213141516171819
classWordsFrequency{privateMap<String,Integer>cnt=newHashMap<>();publicWordsFrequency(String[]book){for(Stringx:book){cnt.merge(x,1,Integer::sum);}}publicintget(Stringword){returncnt.getOrDefault(word,0);}}/** * Your WordsFrequency object will be instantiated and called as such: * WordsFrequency obj = new WordsFrequency(book); * int param_1 = obj.get(word); */
1 2 3 4 5 6 7 8 9101112131415161718192021
classWordsFrequency{public:WordsFrequency(vector<string>&book){for(auto&x:book){++cnt[x];}}intget(stringword){returncnt[word];}private:unordered_map<string,int>cnt;};/** * Your WordsFrequency object will be instantiated and called as such: * WordsFrequency* obj = new WordsFrequency(book); * int param_1 = obj->get(word); */
1 2 3 4 5 6 7 8 9101112131415161718192021
typeWordsFrequencystruct{cntmap[string]int}funcConstructor(book[]string)WordsFrequency{cnt:=map[string]int{}for_,x:=rangebook{cnt[x]++}returnWordsFrequency{cnt}}func(this*WordsFrequency)Get(wordstring)int{returnthis.cnt[word]}/** * Your WordsFrequency object will be instantiated and called as such: * obj := Constructor(book); * param_1 := obj.Get(word); */
1 2 3 4 5 6 7 8 9101112131415161718192021
classWordsFrequency{privatecnt:Map<string,number>;constructor(book:string[]){constcnt=newMap<string,number>();for(constwordofbook){cnt.set(word,(cnt.get(word)??0)+1);}this.cnt=cnt;}get(word:string):number{returnthis.cnt.get(word)??0;}}/** * Your WordsFrequency object will be instantiated and called as such: * var obj = new WordsFrequency(book) * var param_1 = obj.get(word) */
1 2 3 4 5 6 7 8 910111213141516171819202122
usestd::collections::HashMap;structWordsFrequency{cnt:HashMap<String,i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implWordsFrequency{fnnew(book:Vec<String>)->Self{letmutcnt=HashMap::new();forwordinbook.into_iter(){*cnt.entry(word).or_insert(0)+=1;}Self{cnt}}fnget(&self,word:String)->i32{*self.cnt.get(&word).unwrap_or(&0)}}
1 2 3 4 5 6 7 8 91011121314151617181920212223
/** * @param {string[]} book */varWordsFrequency=function(book){this.cnt=newMap();for(constxofbook){this.cnt.set(x,(this.cnt.get(x)||0)+1);}};/** * @param {string} word * @return {number} */WordsFrequency.prototype.get=function(word){returnthis.cnt.get(word)||0;};/** * Your WordsFrequency object will be instantiated and called as such: * var obj = new WordsFrequency(book) * var param_1 = obj.get(word) */